Skip to content

Commit b16cec1

Browse files
authored
This change comes up with compile time pre-processor directives to tune the behavior of rapidjson wrt memory consumption. The idea is to allow each module using this library to choose the right defaults based on how it consumes memory and what performance it expects. 1. RAPIDJSON_DEFAULT_ALLOCATOR: If defined allows you to choose CrtAllocator over MemoryPoolAllocator. If it is not defined, chooses MemoryPoolAllocator by default. 2. RAPIDJSON_DEFAULT_STACK_ALLOCATOR: If defined allows you to choose MemoryPoolAllocator over CrtAllocator. If it is not defined, chooses CrtAllocator by default. 3. RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY and RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY: If defined and set to a value, uses that value for default number of objects/array elements to be pre-allocated. If not defined, uses value of 16: the current default. Verified that all tests pass.
1 parent 98f52b6 commit b16cec1

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

include/rapidjson/document.h

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,48 @@ class GenericValue;
5656
template <typename Encoding, typename Allocator, typename StackAllocator>
5757
class GenericDocument;
5858

59+
/*! \def RAPIDJSON_DEFAULT_ALLOCATOR
60+
\ingroup RAPIDJSON_CONFIG
61+
\brief Allows to choose default allocator.
62+
63+
User can define this to use CrtAllocator or MemoryPoolAllocator.
64+
*/
65+
#ifndef RAPIDJSON_DEFAULT_ALLOCATOR
66+
#define RAPIDJSON_DEFAULT_ALLOCATOR MemoryPoolAllocator<CrtAllocator>
67+
#endif
68+
69+
/*! \def RAPIDJSON_DEFAULT_STACK_ALLOCATOR
70+
\ingroup RAPIDJSON_CONFIG
71+
\brief Allows to choose default stack allocator for Document.
72+
73+
User can define this to use CrtAllocator or MemoryPoolAllocator.
74+
*/
75+
#ifndef RAPIDJSON_DEFAULT_STACK_ALLOCATOR
76+
#define RAPIDJSON_DEFAULT_STACK_ALLOCATOR CrtAllocator
77+
#endif
78+
79+
/*! \def RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY
80+
\ingroup RAPIDJSON_CONFIG
81+
\brief User defined kDefaultObjectCapacity value.
82+
83+
User can define this as any natural number.
84+
*/
85+
#ifndef RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY
86+
// number of objects that rapidjson::Value allocates memory for by default
87+
#define RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY 16
88+
#endif
89+
90+
/*! \def RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY
91+
\ingroup RAPIDJSON_CONFIG
92+
\brief User defined kDefaultArrayCapacity value.
93+
94+
User can define this as any natural number.
95+
*/
96+
#ifndef RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY
97+
// number of array elements that rapidjson::Value allocates memory for by default
98+
#define RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY 16
99+
#endif
100+
59101
//! Name-value pair in a JSON object value.
60102
/*!
61103
This class was internal to GenericValue. It used to be a inner struct.
@@ -604,7 +646,7 @@ template <bool, typename> class GenericObject;
604646
\tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document)
605647
\tparam Allocator Allocator type for allocating memory of object, array and string.
606648
*/
607-
template <typename Encoding, typename Allocator = MemoryPoolAllocator<> >
649+
template <typename Encoding, typename Allocator = RAPIDJSON_DEFAULT_ALLOCATOR >
608650
class GenericValue {
609651
public:
610652
//! Name-value pair in an object.
@@ -1969,8 +2011,8 @@ class GenericValue {
19692011
kTypeMask = 0x07
19702012
};
19712013

1972-
static const SizeType kDefaultArrayCapacity = 16;
1973-
static const SizeType kDefaultObjectCapacity = 16;
2014+
static const SizeType kDefaultArrayCapacity = RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY;
2015+
static const SizeType kDefaultObjectCapacity = RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY;
19742016

19752017
struct Flag {
19762018
#if RAPIDJSON_48BITPOINTER_OPTIMIZATION
@@ -2150,7 +2192,7 @@ typedef GenericValue<UTF8<> > Value;
21502192
\tparam StackAllocator Allocator for allocating memory for stack during parsing.
21512193
\warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructor. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue.
21522194
*/
2153-
template <typename Encoding, typename Allocator = MemoryPoolAllocator<>, typename StackAllocator = CrtAllocator>
2195+
template <typename Encoding, typename Allocator = RAPIDJSON_DEFAULT_ALLOCATOR, typename StackAllocator = RAPIDJSON_DEFAULT_STACK_ALLOCATOR >
21542196
class GenericDocument : public GenericValue<Encoding, Allocator> {
21552197
public:
21562198
typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
@@ -2535,6 +2577,7 @@ class GenericDocument : public GenericValue<Encoding, Allocator> {
25352577
//! GenericDocument with UTF8 encoding
25362578
typedef GenericDocument<UTF8<> > Document;
25372579

2580+
25382581
//! Helper class for accessing Value of array type.
25392582
/*!
25402583
Instance of this helper class is obtained by \c GenericValue::GetArray().

0 commit comments

Comments
 (0)