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_USE_CRTALLOCATOR: If defined allows you to choose
   	CrtAllocator over MemoryPoolAllocator. If it is not defined,
        chooses MemoryPoolAllocator 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.
  • Loading branch information
mdamle committed Feb 19, 2020
1 parent a895ce1 commit ad7f4db
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 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_USE_CRTALLOCATOR
\ingroup RAPIDJSON_CONFIG
\brief Allows to use CrtAllocator by default.
User can define this to use CrtAllocator for GenericValue and GenericDocument.
*/
#ifndef RAPIDJSON_USE_CRTALLOCATOR
#define RAPIDJSON_USE_CRTALLOCATOR 0
#else
#define RAPIDJSON_USE_CRTALLOCATOR 1
#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,11 @@ class GenericValue {
};

//! GenericValue with UTF8 encoding
#if RAPIDJSON_USE_CRTALLOCATOR
typedef GenericValue<UTF8<>, CrtAllocator> Value;
#else
typedef GenericValue<UTF8<> > Value;
#endif

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

//! GenericDocument with UTF8 encoding
#if RAPIDJSON_USE_CRTALLOCATOR
typedef GenericDocument<UTF8<>, CrtAllocator> Document;
#else
typedef GenericDocument<UTF8<> > Document;
#endif

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

0 comments on commit ad7f4db

Please sign in to comment.