From 640b525e830f84060f0d43923e4fffdb40d5676a Mon Sep 17 00:00:00 2001 From: xiaohaoliang Date: Sat, 4 Mar 2017 01:40:43 +0800 Subject: [PATCH] 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." --- include/flatbuffers/flatbuffers.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 5ca38d8be..c3531d411 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -1057,8 +1057,12 @@ FLATBUFFERS_FINAL_CLASS /// where the vector is stored. template Offset> 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(v), len); + } else { + for (auto i = len; i > 0; ) { + PushElement(v[--i]); + } } return Offset>(EndVector(len)); }