Skip to content

Commit

Permalink
GenericValue: reduce growth factor for array/object reallocations
Browse files Browse the repository at this point in the history
As mentioned by @Kosta-Github in http://git.io/0gkYSg, the currently
used growth factor of 2 is suboptimal for memory performance.  An
extensive discussion can be found at [1].

This patch reduces the array/object capacity growth factor to 1.5, as
many C++ implementations have chosen to use.  In order to avoid
floating-point arithmetics for computing the new capacity, I did not
add any customization parameter for the factor and used a shift+add
instead.

[1] https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md
  • Loading branch information
pah committed Sep 1, 2014
1 parent 2a4e055 commit f4f4328
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ class GenericValue {
}
else {
SizeType oldCapacity = o.capacity;
o.capacity *= 2;
o.capacity += (oldCapacity >> 1); // grow by factor 1.5
o.members = reinterpret_cast<Member*>(allocator.Realloc(o.members, oldCapacity * sizeof(Member), o.capacity * sizeof(Member)));
}
}
Expand Down Expand Up @@ -1130,7 +1130,7 @@ int z = a[0u].GetInt(); // This works too.
GenericValue& PushBack(GenericValue& value, Allocator& allocator) {
RAPIDJSON_ASSERT(IsArray());
if (data_.a.size >= data_.a.capacity)
Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : data_.a.capacity * 2, allocator);
Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity >> 1)), allocator);
data_.a.elements[data_.a.size++].RawAssign(value);
return *this;
}
Expand Down

0 comments on commit f4f4328

Please sign in to comment.