mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
Optimize CreateVector for types > 1 byte on little endian (#4355)
This gives a 10x speed up in my test, when creating a Vector of floats
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
f52f848b95
commit
57f3752d5e
@@ -1120,16 +1120,28 @@ class FlatBufferBuilder
|
||||
/// where the vector is stored.
|
||||
template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
|
||||
StartVector(len, sizeof(T));
|
||||
if (sizeof(T) == 1) {
|
||||
PushBytes(reinterpret_cast<const uint8_t *>(v), len);
|
||||
} else {
|
||||
for (auto i = len; i > 0; ) {
|
||||
PushElement(v[--i]);
|
||||
#if FLATBUFFERS_LITTLEENDIAN
|
||||
PushBytes(reinterpret_cast<const uint8_t *>(v), len * sizeof(T));
|
||||
#else
|
||||
if (sizeof(T) == 1) {
|
||||
PushBytes(reinterpret_cast<const uint8_t *>(v), len);
|
||||
} else {
|
||||
for (auto i = len; i > 0; ) {
|
||||
PushElement(v[--i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return Offset<Vector<T>>(EndVector(len));
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<Offset<T>>> CreateVector(const Offset<T> *v, size_t len) {
|
||||
StartVector(len, sizeof(Offset<T>));
|
||||
for (auto i = len; i > 0; ) {
|
||||
PushElement(v[--i]);
|
||||
}
|
||||
return Offset<Vector<Offset<T>>>(EndVector(len));
|
||||
}
|
||||
|
||||
/// @brief Serialize a `std::vector` into a FlatBuffer `vector`.
|
||||
/// @tparam T The data type of the `std::vector` elements.
|
||||
/// @param v A const reference to the `std::vector` to serialize into the
|
||||
|
||||
Reference in New Issue
Block a user