From 1a21b545604799e75e253e700c4e48c06cbbc68d Mon Sep 17 00:00:00 2001 From: gbeili Date: Wed, 11 Jan 2017 10:36:18 -0800 Subject: [PATCH] Add a builder option to enable/disable vtables deduplication. (#4132) * Add a builder option to enable/disable vtables deduplication. * address code review comments --- include/flatbuffers/flatbuffers.h | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index efe2ab11e..1d979cc32 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -647,7 +647,7 @@ FLATBUFFERS_FINAL_CLASS const simple_allocator *allocator = nullptr) : buf_(initial_size, allocator ? *allocator : default_allocator), nested(false), finished(false), minalign_(1), force_defaults_(false), - string_pool(nullptr) { + dedup_vtables_(true), string_pool(nullptr) { offsetbuf_.reserve(16); // Avoid first few reallocs. vtables_.reserve(16); EndianCheck(); @@ -724,6 +724,10 @@ FLATBUFFERS_FINAL_CLASS /// @param[in] bool fd When set to `true`, always serializes default values. void ForceDefaults(bool fd) { force_defaults_ = fd; } + /// @brief By default vtables are deduped in order to save space. + /// @param[in] bool dedup When set to `true`, dedup vtables. + void DedupVtables(bool dedup) { dedup_vtables_ = dedup; } + /// @cond FLATBUFFERS_INTERNAL void Pad(size_t num_bytes) { buf_.fill(num_bytes); } @@ -861,13 +865,15 @@ FLATBUFFERS_FINAL_CLASS auto vt_use = GetSize(); // See if we already have generated a vtable with this exact same // layout before. If so, make it point to the old one, remove this one. - for (auto it = vtables_.begin(); it != vtables_.end(); ++it) { - auto vt2 = reinterpret_cast(buf_.data_at(*it)); - auto vt2_size = *vt2; - if (vt1_size != vt2_size || memcmp(vt2, vt1, vt1_size)) continue; - vt_use = *it; - buf_.pop(GetSize() - vtableoffsetloc); - break; + if (dedup_vtables_) { + for (auto it = vtables_.begin(); it != vtables_.end(); ++it) { + auto vt2 = reinterpret_cast(buf_.data_at(*it)); + auto vt2_size = *vt2; + if (vt1_size != vt2_size || memcmp(vt2, vt1, vt1_size)) continue; + vt_use = *it; + buf_.pop(GetSize() - vtableoffsetloc); + break; + } } // If this is a new vtable, remember it. if (vt_use == GetSize()) { @@ -1267,6 +1273,8 @@ FLATBUFFERS_FINAL_CLASS bool force_defaults_; // Serialize values equal to their defaults anyway. + bool dedup_vtables_; + struct StringOffsetCompare { StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {} bool operator() (const Offset &a, const Offset &b) const {