Add a builder option to enable/disable vtables deduplication. (#4132)
* Add a builder option to enable/disable vtables deduplication. * address code review comments
This commit is contained in:
parent
b55f18649a
commit
1a21b54560
|
@ -647,7 +647,7 @@ FLATBUFFERS_FINAL_CLASS
|
||||||
const simple_allocator *allocator = nullptr)
|
const simple_allocator *allocator = nullptr)
|
||||||
: buf_(initial_size, allocator ? *allocator : default_allocator),
|
: buf_(initial_size, allocator ? *allocator : default_allocator),
|
||||||
nested(false), finished(false), minalign_(1), force_defaults_(false),
|
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.
|
offsetbuf_.reserve(16); // Avoid first few reallocs.
|
||||||
vtables_.reserve(16);
|
vtables_.reserve(16);
|
||||||
EndianCheck();
|
EndianCheck();
|
||||||
|
@ -724,6 +724,10 @@ FLATBUFFERS_FINAL_CLASS
|
||||||
/// @param[in] bool fd When set to `true`, always serializes default values.
|
/// @param[in] bool fd When set to `true`, always serializes default values.
|
||||||
void ForceDefaults(bool fd) { force_defaults_ = fd; }
|
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
|
/// @cond FLATBUFFERS_INTERNAL
|
||||||
void Pad(size_t num_bytes) { buf_.fill(num_bytes); }
|
void Pad(size_t num_bytes) { buf_.fill(num_bytes); }
|
||||||
|
|
||||||
|
@ -861,6 +865,7 @@ FLATBUFFERS_FINAL_CLASS
|
||||||
auto vt_use = GetSize();
|
auto vt_use = GetSize();
|
||||||
// See if we already have generated a vtable with this exact same
|
// 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.
|
// layout before. If so, make it point to the old one, remove this one.
|
||||||
|
if (dedup_vtables_) {
|
||||||
for (auto it = vtables_.begin(); it != vtables_.end(); ++it) {
|
for (auto it = vtables_.begin(); it != vtables_.end(); ++it) {
|
||||||
auto vt2 = reinterpret_cast<voffset_t *>(buf_.data_at(*it));
|
auto vt2 = reinterpret_cast<voffset_t *>(buf_.data_at(*it));
|
||||||
auto vt2_size = *vt2;
|
auto vt2_size = *vt2;
|
||||||
|
@ -869,6 +874,7 @@ FLATBUFFERS_FINAL_CLASS
|
||||||
buf_.pop(GetSize() - vtableoffsetloc);
|
buf_.pop(GetSize() - vtableoffsetloc);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// If this is a new vtable, remember it.
|
// If this is a new vtable, remember it.
|
||||||
if (vt_use == GetSize()) {
|
if (vt_use == GetSize()) {
|
||||||
vtables_.push_back(vt_use);
|
vtables_.push_back(vt_use);
|
||||||
|
@ -1267,6 +1273,8 @@ FLATBUFFERS_FINAL_CLASS
|
||||||
|
|
||||||
bool force_defaults_; // Serialize values equal to their defaults anyway.
|
bool force_defaults_; // Serialize values equal to their defaults anyway.
|
||||||
|
|
||||||
|
bool dedup_vtables_;
|
||||||
|
|
||||||
struct StringOffsetCompare {
|
struct StringOffsetCompare {
|
||||||
StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {}
|
StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {}
|
||||||
bool operator() (const Offset<String> &a, const Offset<String> &b) const {
|
bool operator() (const Offset<String> &a, const Offset<String> &b) const {
|
||||||
|
|
Loading…
Reference in New Issue