diff options
author | Kevin Rose <kevin+gh@maypark.com> | 2017-08-01 10:34:00 -0500 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2017-08-01 08:34:00 -0700 |
commit | 3282a84e3068d2ff0ded5a683ceea0806da21ed6 (patch) | |
tree | 5ca756f0045bad32e24a6130dfd1985ec86ee9a2 /src | |
parent | 89a68942acdeeb51ceb102d19153dcc23ba8c0dd (diff) | |
download | flatbuffers-3282a84e3068d2ff0ded5a683ceea0806da21ed6.tar.gz flatbuffers-3282a84e3068d2ff0ded5a683ceea0806da21ed6.tar.bz2 flatbuffers-3282a84e3068d2ff0ded5a683ceea0806da21ed6.zip |
[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
Diffstat (limited to 'src')
-rw-r--r-- | src/idl_gen_python.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index c16a0117..257515b7 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -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; } |