-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object members stored in std::multimap #1485
Conversation
Some numbers:
|
50d0bd8
to
5c454a0
Compare
This seems promising but it really needs time to review. |
a778622
to
514af25
Compare
e4ab391
to
70815d8
Compare
d5498df
to
858a9b3
Compare
dcfdc22
to
a8cf249
Compare
@miloyip this PR needs 7546eb2 from #1503 to pass (see https://travis-ci.org/github/Tencent/rapidjson/jobs/765633543#L2086 for instance), the allocator cannot be destroyed before the Values using it. I'll merge PR #1503 here until it's in master.. |
Actually the whole PR #1503 is needed (not only 7546eb2, but also c033292), otherwise with
Currently |
c090ee8
to
a2343c6
Compare
…ers. When RAPIDJSON_USE_MEMBERSMAP is defined, an object Value will store its members in an (re)allocated array of Members like before, but also in an std::multimap<GenericValue::Data,SizeType> where the key and value reference the corresponding Member by its Data and index in the array, respectively, and in a relocatable manner. The layout of the members map/array is now: {multimap*}<>{capacity}<>{Member[capacity]}<>{multimap::iterator[capacity]} where <> stands for the RAPIDJSON_ALIGN-ment of each part, if needed. This layout needs to be reallocated when the current capacity is exhausted, which requires to take care of the multimap and its iterators explicitely. The multimap is allocated separately and only its pointer is saved in this layout, so it can easily be restored in its new position. As for the old/alive iterators, they must move to their new offset according to the new capacity. With this in place, it's immediate to get the multimap::iterator from a MemberIterator and vice versa, thus the same complexity applies for the operations with MemberIterator or MapIterator. For FindMember() and RemoveMember(), the complexity drops from O(n) to the multimap/rbtree's O(log n). For EraseMember() it drops from O(n-m) to O((log n)-m), m representing the move/copy of the trailing members. For AddMember() though, the complexity grows from O(1) to O(log n) due to the insertion in the multimap too. Consequently parsing will be slower, up to ~20% measured in perftests on my laptop (since it's mainly composed of insertions). But later work on the Document (usually the goal of parsing...) will be much faster; the new DocumentFind perftest included in this commit is 8 times faster with RAPIDJSON_USE_MEMBERSMAP (still on my laptop). Overall the tests are 4% slower (mainly composed of parsing), and notably 15% slower for schemas parsing/validation (which supposedly comes from the larger JSON files parsing, still). As a side note, when RAPIDJSON_USE_MEMBERSMAP is not defined, this commit does nothing (same results for perftest with regard to previous versions). Finally, the multimap is allocated and constructed using StdAllocator, so they will use the same Allocator than for any other Value allocation, and thus will benefit from the same performance/safety/security/whatever provided by the user given Allocator.
Set RAPIDJSON_USE_MEMBERSMAP to use a (std::multi)map for object members.
When
RAPIDJSON_USE_MEMBERSMAP
is defined, an objectValue
will storeits members in an (re)allocated array of Members like before, but also
in an
std::multimap<GenericValue::Data,SizeType>
where the key and valuereference the corresponding
Member
by its Data and index in the array,respectively, and in a relocatable manner.
The layout of the members map/array is now:
{multimap*}<>{capacity}<>{Member[capacity]}<>{multimap::iterator[capacity]}
where
<>
stands for theRAPIDJSON_ALIGN
-ment of each part, if needed.This layout needs to be reallocated when the current capacity is
exhausted, which requires to take care of the
multimap
and its iteratorsexplicitely. The
multimap
is allocated separately and only its pointer issaved in this layout, so it can easily be restored in its new position.
As for the old/alive iterators, they must move to their new offset according
to the new capacity.
With this in place, it's immediate to get the
multimap::iterator
from aMemberIterator and vice versa, thus the same complexity applies for the
operations with MemberIterator or MapIterator.
For
FindMember()
andRemoveMember()
, the complexity drops fromO(n)
tothe
multimap/rbtree
'sO(log n)
.For
EraseMember()
it drops fromO(n-m)
toO((log n)-m)
,m
representingthe move/copy of the trailing members.
For
AddMember()
though, the complexity grows fromO(1)
toO(log n)
due tothe insertion in the
multimap
too.Consequently parsing will be slower, up to ~20% measured in perftests on
my laptop (since it's mainly composed of insertions). But later work on
the
Document
(usually the goal of parsing...) will be much faster; thenew
DocumentFind
perftest included in this commit is 8 times faster withRAPIDJSON_USE_MEMBERSMAP
(still on my laptop). Overall the tests are 4%slower (mainly composed of parsing), and notably 15% slower for schemas
parsing/validation (which supposedly comes from the larger JSON files
parsing, still). As a side note, when
RAPIDJSON_USE_MEMBERSMAP
is notdefined, this commit does nothing (same results for perftest with regard
to previous versions).
Finally, the
multimap
is allocated and constructed using StdAllocator,so they will use the same Allocator than for any other
Value
allocation,and thus will benefit from the same performance/safety/security/whatever
provided by the user given Allocator.