From ab7949dc16cf8117616782e9809c5f3653929c05 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Mon, 9 Jan 2017 19:10:11 +0100 Subject: [PATCH] Cold part of make_space() code moved to reallocate member function. (#4130) Change makes make_space() code much friendlier for the compiler to inline which makes significant (measurable) performance improvements. --- include/flatbuffers/flatbuffers.h | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index aa1d9179c..efe2ab11e 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -539,17 +539,7 @@ class vector_downward { uint8_t *make_space(size_t len) { if (len > static_cast(cur_ - buf_)) { - auto old_size = size(); - auto largest_align = AlignOf(); - reserved_ += (std::max)(len, growth_policy(reserved_)); - // Round up to avoid undefined behavior from unaligned loads and stores. - reserved_ = (reserved_ + (largest_align - 1)) & ~(largest_align - 1); - auto new_buf = allocator_.allocate(reserved_); - auto new_cur = new_buf + reserved_ - old_size; - memcpy(new_cur, cur_, old_size); - cur_ = new_cur; - allocator_.deallocate(buf_); - buf_ = new_buf; + reallocate(len); } cur_ -= len; // Beyond this, signed offsets may not have enough range: @@ -593,6 +583,20 @@ class vector_downward { uint8_t *buf_; uint8_t *cur_; // Points at location between empty (below) and used (above). const simple_allocator &allocator_; + + void reallocate(size_t len) { + auto old_size = size(); + auto largest_align = AlignOf(); + reserved_ += (std::max)(len, growth_policy(reserved_)); + // Round up to avoid undefined behavior from unaligned loads and stores. + reserved_ = (reserved_ + (largest_align - 1)) & ~(largest_align - 1); + auto new_buf = allocator_.allocate(reserved_); + auto new_cur = new_buf + reserved_ - old_size; + memcpy(new_cur, cur_, old_size); + cur_ = new_cur; + allocator_.deallocate(buf_); + buf_ = new_buf; + } }; // Converts a Field ID to a virtual table offset.