mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 01:41:38 +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.
|
/// where the vector is stored.
|
||||||
template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
|
template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
|
||||||
StartVector(len, sizeof(T));
|
StartVector(len, sizeof(T));
|
||||||
if (sizeof(T) == 1) {
|
#if FLATBUFFERS_LITTLEENDIAN
|
||||||
PushBytes(reinterpret_cast<const uint8_t *>(v), len);
|
PushBytes(reinterpret_cast<const uint8_t *>(v), len * sizeof(T));
|
||||||
} else {
|
#else
|
||||||
for (auto i = len; i > 0; ) {
|
if (sizeof(T) == 1) {
|
||||||
PushElement(v[--i]);
|
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));
|
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`.
|
/// @brief Serialize a `std::vector` into a FlatBuffer `vector`.
|
||||||
/// @tparam T The data type of the `std::vector` elements.
|
/// @tparam T The data type of the `std::vector` elements.
|
||||||
/// @param v A const reference to the `std::vector` to serialize into the
|
/// @param v A const reference to the `std::vector` to serialize into the
|
||||||
|
|||||||
Reference in New Issue
Block a user