Skip to content

Commit

Permalink
Closes Tencent#1643
Browse files Browse the repository at this point in the history
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_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.
  • Loading branch information
mdamle committed Feb 20, 2020
1 parent a895ce1 commit 093c988
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ class GenericValue;
template <typename Encoding, typename Allocator, typename StackAllocator>
class GenericDocument;

/*! \def RAPIDJSON_DEFAULT_CRTALLOCATOR
\ingroup RAPIDJSON_CONFIG
\brief Allows to choose default allocator.
User can define this to use CrtAllocator or MemoryPoolAllocator.
*/
#ifndef RAPIDJSON_DEFAULT_ALLOCATOR
#define RAPIDJSON_DEFAULT_ALLOCATOR MemoryPoolAllocator<CrtAllocator>
#else
#define RAPIDJSON_DEFAULT_ALLOCATOR CrtAllocator
#endif

/*! \def RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY
\ingroup RAPIDJSON_CONFIG
\brief User defined kDefaultObjectCapacity value.
User can define this as any natural number.
*/
#ifndef RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY
// number of objects that rapidjson::Value allocates memory for by default
#define RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY 16
#endif

/*! \def RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY
\ingroup RAPIDJSON_CONFIG
\brief User defined kDefaultArrayCapacity value.
User can define this as any natural number.
*/
#ifndef RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY
// number of array elements that rapidjson::Value allocates memory for by default
#define RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY 16
#endif

//! Name-value pair in a JSON object value.
/*!
This class was internal to GenericValue. It used to be a inner struct.
Expand Down Expand Up @@ -1969,8 +2003,8 @@ class GenericValue {
kTypeMask = 0x07
};

static const SizeType kDefaultArrayCapacity = 16;
static const SizeType kDefaultObjectCapacity = 16;
static const SizeType kDefaultArrayCapacity = RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY;
static const SizeType kDefaultObjectCapacity = RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY;

struct Flag {
#if RAPIDJSON_48BITPOINTER_OPTIMIZATION
Expand Down Expand Up @@ -2137,7 +2171,7 @@ class GenericValue {
};

//! GenericValue with UTF8 encoding
typedef GenericValue<UTF8<> > Value;
typedef GenericValue<UTF8<>, RAPIDJSON_DEFAULT_ALLOCATOR> Value;

///////////////////////////////////////////////////////////////////////////////
// GenericDocument
Expand Down Expand Up @@ -2533,7 +2567,8 @@ class GenericDocument : public GenericValue<Encoding, Allocator> {
};

//! GenericDocument with UTF8 encoding
typedef GenericDocument<UTF8<> > Document;
typedef GenericDocument<UTF8<>, RAPIDJSON_DEFAULT_CRTALLOCATOR> Document;


//! Helper class for accessing Value of array type.
/*!
Expand Down

0 comments on commit 093c988

Please sign in to comment.