optimization of FlatBufferBuilder::CreateVector() (#4198)

optimization of FlatBufferBuilder::CreateVector()
1. optimization of FlatBufferBuilder::CreateVector() for "1 == sizeof(T)" ( such as [byte], [ubyte]).
2. For my project, it was about 10x improvement on flatbuffers serialization.
3. why not "string": "string, which may only hold UTF-8 or 7-bit ASCII. For other text encodings or general binary data use vectors ([byte] or [ubyte]) instead."
This commit is contained in:
xiaohaoliang 2017-03-04 01:40:43 +08:00 committed by Wouter van Oortmerssen
parent 0b379211dc
commit 640b525e83
1 changed files with 6 additions and 2 deletions

View File

@ -1057,8 +1057,12 @@ FLATBUFFERS_FINAL_CLASS
/// where the vector is stored.
template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
StartVector(len, sizeof(T));
for (auto i = len; i > 0; ) {
PushElement(v[--i]);
if (sizeof(T) == 1) {
PushBytes(reinterpret_cast<const uint8_t *>(v), len);
} else {
for (auto i = len; i > 0; ) {
PushElement(v[--i]);
}
}
return Offset<Vector<T>>(EndVector(len));
}