Skip to content

Commit 71f0fa7

Browse files
committed
Set RAPIDJSON_USE_MEMBERSMAP to use a (std::multi)map for object members.
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.
1 parent 7d801bb commit 71f0fa7

File tree

4 files changed

+365
-59
lines changed

4 files changed

+365
-59
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ if(RAPIDJSON_HAS_STDSTRING)
5252
add_definitions(-DRAPIDJSON_HAS_STDSTRING)
5353
endif()
5454

55+
option(RAPIDJSON_USE_MEMBERSMAP "" OFF)
56+
if(RAPIDJSON_USE_MEMBERSMAP)
57+
add_definitions(-DRAPIDJSON_USE_MEMBERSMAP=1)
58+
endif()
59+
5560
find_program(CCACHE_FOUND ccache)
5661
if(CCACHE_FOUND)
5762
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)

include/rapidjson/allocators.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define RAPIDJSON_ALLOCATORS_H_
1717

1818
#include "rapidjson.h"
19+
#include "internal/meta.h"
1920

2021
#include <memory>
2122

@@ -158,6 +159,7 @@ class MemoryPoolAllocator {
158159

159160
public:
160161
static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator)
162+
static const bool kRefCounted = true; //!< Tell users that this allocator is reference counted on copy
161163

162164
//! Constructor with chunkSize.
163165
/*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
@@ -417,6 +419,16 @@ class MemoryPoolAllocator {
417419
SharedData *shared_; //!< The shared data of the allocator
418420
};
419421

422+
namespace internal {
423+
template<typename, typename = void>
424+
struct IsRefCounted :
425+
public FalseType
426+
{ };
427+
template<typename T>
428+
struct IsRefCounted<T, typename internal::EnableIfCond<T::kRefCounted>::Type> :
429+
public TrueType
430+
{ };
431+
}
420432

421433
template<typename T, typename A>
422434
inline T* Realloc(A& a, T* old_p, size_t old_n, size_t new_n)
@@ -437,7 +449,6 @@ inline void Free(A& a, T *p, size_t n = 1)
437449
static_cast<void>(Realloc<T, A>(a, p, n, 0));
438450
}
439451

440-
441452
#ifdef __GNUC__
442453
RAPIDJSON_DIAG_PUSH
443454
RAPIDJSON_DIAG_OFF(effc++) // std::allocator can safely be inherited
@@ -601,6 +612,7 @@ class StdAllocator :
601612

602613
//! rapidjson Allocator concept
603614
static const bool kNeedFree = BaseAllocator::kNeedFree;
615+
static const bool kRefCounted = internal::IsRefCounted<BaseAllocator>::Value;
604616
void* Malloc(size_t size)
605617
{
606618
return baseAllocator_.Malloc(size);

0 commit comments

Comments
 (0)