[Python] (scalar) vector reading speedup via numpy (#4390)

* Add numpy accessor to python flatbuffers scalar vectors

* Update python tests to test numpy vector accessor

* Update appveyor CI to run Python tests, save generated code as artifact

* Update example generated python code

* Add numpy info to python usage docs

* Update test schema and python tests w/ multi-byte vector

* did not mean to push profiling code

* adding float64 numpy tests
This commit is contained in:
Kevin Rose
2017-08-01 10:34:00 -05:00
committed by Wouter van Oortmerssen
parent 89a68942ac
commit 3282a84e30
21 changed files with 666 additions and 32 deletions

View File

@@ -274,6 +274,38 @@ static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def,
code += "\n";
}
// Returns a non-struct vector as a numpy array. Much faster
// than iterating over the vector element by element.
static void GetVectorOfNonStructAsNumpy(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
auto vectortype = field.value.type.VectorType();
// Currently, we only support accessing as numpy array if
// the vector type is a scalar.
if (!(IsScalar(vectortype.base_type))) {
return;
}
GenReceiver(struct_def, code_ptr);
code += MakeCamel(field.name) + "AsNumpy(self):";
code += OffsetPrefix(field);
code += Indent + Indent + Indent;
code += "return ";
code += "self._tab.GetVectorAsNumpy(flatbuffers.number_types.";
code += MakeCamel(GenTypeGet(field.value.type));
code += "Flags, o)\n";
if (vectortype.base_type == BASE_TYPE_STRING) {
code += Indent + Indent + "return \"\"\n";
} else {
code += Indent + Indent + "return 0\n";
}
code += "\n";
}
// Begin the creator function signature.
static void BeginBuilderArgs(const StructDef &struct_def,
std::string *code_ptr) {
@@ -440,6 +472,7 @@ static void GenStructAccessor(const StructDef &struct_def,
GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
} else {
GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr);
GetVectorOfNonStructAsNumpy(struct_def, field, code_ptr);
}
break;
}