Skip to content

Commit d151dcb

Browse files
authoredMay 19, 2021
Revert "[C++] Removed most heap allocations in builder (#6620)" (#6659)
This reverts commit 72730ec.
1 parent f1884c6 commit d151dcb

File tree

2 files changed

+19
-58
lines changed

2 files changed

+19
-58
lines changed
 

‎include/flatbuffers/base.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,6 @@ namespace flatbuffers {
247247
#endif // __has_include
248248
#endif // !FLATBUFFERS_HAS_STRING_VIEW
249249

250-
#ifndef FLATBUFFERS_GENERAL_HEAP_ALLOC_OK
251-
// Allow heap allocations to be used
252-
#define FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 1
253-
#endif // !FLATBUFFERS_GENERAL_HEAP_ALLOC_OK
254-
255250
#ifndef FLATBUFFERS_HAS_NEW_STRTOD
256251
// Modern (C++11) strtod and strtof functions are available for use.
257252
// 1) nan/inf strings as argument of strtod;

‎include/flatbuffers/flatbuffers.h

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,8 +1356,9 @@ class FlatBufferBuilder {
13561356
// Write a single aligned scalar to the buffer
13571357
template<typename T> uoffset_t PushElement(T element) {
13581358
AssertScalarT<T>();
1359+
T litle_endian_element = EndianScalar(element);
13591360
Align(sizeof(T));
1360-
buf_.push_small(EndianScalar(element));
1361+
buf_.push_small(litle_endian_element);
13611362
return GetSize();
13621363
}
13631364

@@ -1600,13 +1601,11 @@ class FlatBufferBuilder {
16001601

16011602
/// @brief Store a string in the buffer, which can contain any binary data.
16021603
/// If a string with this exact contents has already been serialized before,
1603-
/// instead simply returns the offset of the existing string. This uses a map
1604-
/// stored on the heap, but only stores the numerical offsets.
1604+
/// instead simply returns the offset of the existing string.
16051605
/// @param[in] str A const char pointer to the data to be stored as a string.
16061606
/// @param[in] len The number of bytes that should be stored from `str`.
16071607
/// @return Returns the offset in the buffer where the string starts.
16081608
Offset<String> CreateSharedString(const char *str, size_t len) {
1609-
FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK);
16101609
if (!string_pool)
16111610
string_pool = new StringOffsetMap(StringOffsetCompare(buf_));
16121611
auto size_before_string = buf_.size();
@@ -1628,8 +1627,7 @@ class FlatBufferBuilder {
16281627
#ifdef FLATBUFFERS_HAS_STRING_VIEW
16291628
/// @brief Store a string in the buffer, which can contain any binary data.
16301629
/// If a string with this exact contents has already been serialized before,
1631-
/// instead simply returns the offset of the existing string. This uses a map
1632-
/// stored on the heap, but only stores the numerical offsets.
1630+
/// instead simply returns the offset of the existing string.
16331631
/// @param[in] str A const std::string_view to store in the buffer.
16341632
/// @return Returns the offset in the buffer where the string starts
16351633
Offset<String> CreateSharedString(const flatbuffers::string_view str) {
@@ -1638,8 +1636,7 @@ class FlatBufferBuilder {
16381636
#else
16391637
/// @brief Store a string in the buffer, which null-terminated.
16401638
/// If a string with this exact contents has already been serialized before,
1641-
/// instead simply returns the offset of the existing string. This uses a map
1642-
/// stored on the heap, but only stores the numerical offsets.
1639+
/// instead simply returns the offset of the existing string.
16431640
/// @param[in] str A const char pointer to a C-string to add to the buffer.
16441641
/// @return Returns the offset in the buffer where the string starts.
16451642
Offset<String> CreateSharedString(const char *str) {
@@ -1648,8 +1645,7 @@ class FlatBufferBuilder {
16481645

16491646
/// @brief Store a string in the buffer, which can contain any binary data.
16501647
/// If a string with this exact contents has already been serialized before,
1651-
/// instead simply returns the offset of the existing string. This uses a map
1652-
/// stored on the heap, but only stores the numerical offsets.
1648+
/// instead simply returns the offset of the existing string.
16531649
/// @param[in] str A const reference to a std::string to store in the buffer.
16541650
/// @return Returns the offset in the buffer where the string starts.
16551651
Offset<String> CreateSharedString(const std::string &str) {
@@ -1659,8 +1655,7 @@ class FlatBufferBuilder {
16591655

16601656
/// @brief Store a string in the buffer, which can contain any binary data.
16611657
/// If a string with this exact contents has already been serialized before,
1662-
/// instead simply returns the offset of the existing string. This uses a map
1663-
/// stored on the heap, but only stores the numerical offsets.
1658+
/// instead simply returns the offset of the existing string.
16641659
/// @param[in] str A const pointer to a `String` struct to add to the buffer.
16651660
/// @return Returns the offset in the buffer where the string starts
16661661
Offset<String> CreateSharedString(const String *str) {
@@ -1767,18 +1762,15 @@ class FlatBufferBuilder {
17671762
/// where the vector is stored.
17681763
template<typename T> Offset<Vector<T>> CreateVector(size_t vector_size,
17691764
const std::function<T (size_t i)> &f) {
1770-
FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK);
17711765
std::vector<T> elems(vector_size);
17721766
for (size_t i = 0; i < vector_size; i++) elems[i] = f(i);
17731767
return CreateVector(elems);
17741768
}
1775-
#endif // FLATBUFFERS_CPP98_STL
1769+
#endif
17761770
// clang-format on
17771771

17781772
/// @brief Serialize values returned by a function into a FlatBuffer `vector`.
1779-
/// This is a convenience function that takes care of iteration for you. This
1780-
/// uses a vector stored on the heap to store the intermediate results of the
1781-
/// iteration.
1773+
/// This is a convenience function that takes care of iteration for you.
17821774
/// @tparam T The data type of the `std::vector` elements.
17831775
/// @param f A function that takes the current iteration 0..vector_size-1,
17841776
/// and the state parameter returning any type that you can construct a
@@ -1788,7 +1780,6 @@ class FlatBufferBuilder {
17881780
/// where the vector is stored.
17891781
template<typename T, typename F, typename S>
17901782
Offset<Vector<T>> CreateVector(size_t vector_size, F f, S *state) {
1791-
FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK);
17921783
std::vector<T> elems(vector_size);
17931784
for (size_t i = 0; i < vector_size; i++) elems[i] = f(i, state);
17941785
return CreateVector(elems);
@@ -1802,34 +1793,9 @@ class FlatBufferBuilder {
18021793
/// where the vector is stored.
18031794
Offset<Vector<Offset<String>>> CreateVectorOfStrings(
18041795
const std::vector<std::string> &v) {
1805-
return CreateVectorOfStrings(v.cbegin(), v.cend());
1806-
}
1807-
1808-
/// @brief Serialize a collection of Strings into a FlatBuffer `vector`.
1809-
/// This is a convenience function for a common case.
1810-
/// @param begin The begining iterator of the collection
1811-
/// @param end The ending iterator of the collection
1812-
/// @return Returns a typed `Offset` into the serialized data indicating
1813-
/// where the vector is stored.
1814-
template<class It>
1815-
Offset<Vector<Offset<String>>> CreateVectorOfStrings(It begin, It end) {
1816-
auto size = std::distance(begin, end);
1817-
auto scratch_buffer_usage = size * sizeof(Offset<String>);
1818-
// If there is not enough space to store the offsets, there definitely won't
1819-
// be enough space to store all the strings. So ensuring space for the
1820-
// scratch region is OK, for it it fails, it would have failed later.
1821-
buf_.ensure_space(scratch_buffer_usage);
1822-
for (auto it = begin; it != end; ++it) {
1823-
buf_.scratch_push_small(CreateString(*it));
1824-
}
1825-
StartVector(size, sizeof(Offset<String>));
1826-
for (auto it = buf_.scratch_end();
1827-
it > buf_.scratch_end() - scratch_buffer_usage;) {
1828-
it -= sizeof(Offset<String>);
1829-
PushElement(*reinterpret_cast<Offset<String> *>(it));
1830-
}
1831-
buf_.scratch_pop(scratch_buffer_usage);
1832-
return Offset<Vector<Offset<String>>>(EndVector(size));
1796+
std::vector<Offset<String>> offsets(v.size());
1797+
for (size_t i = 0; i < v.size(); i++) offsets[i] = CreateString(v[i]);
1798+
return CreateVector(offsets);
18331799
}
18341800

18351801
/// @brief Serialize an array of structs into a FlatBuffer `vector`.
@@ -1860,9 +1826,9 @@ class FlatBufferBuilder {
18601826
Offset<Vector<const T *>> CreateVectorOfNativeStructs(
18611827
const S *v, size_t len, T((*const pack_func)(const S &))) {
18621828
FLATBUFFERS_ASSERT(pack_func);
1863-
auto structs = StartVectorOfStructs<T>(len);
1864-
for (size_t i = 0; i < len; i++) { structs[i] = pack_func(v[i]); }
1865-
return EndVectorOfStructs<T>(len);
1829+
std::vector<T> vv(len);
1830+
std::transform(v, v + len, vv.begin(), pack_func);
1831+
return CreateVectorOfStructs<T>(data(vv), vv.size());
18661832
}
18671833

18681834
/// @brief Serialize an array of native structs into a FlatBuffer `vector`.
@@ -2028,10 +1994,10 @@ class FlatBufferBuilder {
20281994
Offset<Vector<const T *>> CreateVectorOfSortedNativeStructs(S *v,
20291995
size_t len) {
20301996
extern T Pack(const S &);
2031-
auto structs = StartVectorOfStructs<T>(len);
2032-
for (size_t i = 0; i < len; i++) { structs[i] = Pack(v[i]); }
2033-
std::sort(structs, structs + len, StructKeyComparator<T>());
2034-
return EndVectorOfStructs<T>(len);
1997+
typedef T (*Pack_t)(const S &);
1998+
std::vector<T> vv(len);
1999+
std::transform(v, v + len, vv.begin(), static_cast<Pack_t &>(Pack));
2000+
return CreateVectorOfSortedStructs<T>(vv, len);
20352001
}
20362002

20372003
/// @cond FLATBUFFERS_INTERNAL

0 commit comments

Comments
 (0)
Please sign in to comment.