Commit ef6957c1 authored by Milo Yip's avatar Milo Yip

PutReserve() and PutUnsafe() optimisation for Writer

parent 1a21379e
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
!/bin/data !/bin/data
!/bin/encodings !/bin/encodings
!/bin/jsonchecker !/bin/jsonchecker
!/bin/types
/build /build
/doc/html /doc/html
/doc/doxygen_*.db /doc/doxygen_*.db
......
[
true,
true,
false,
false,
true,
true,
true,
false,
false,
true,
false,
false,
true,
false,
false,
false,
true,
false,
false,
true,
true,
false,
true,
true,
true,
false,
false,
false,
true,
false,
true,
false,
false,
true,
true,
true,
true,
true,
true,
false,
false,
true,
false,
false,
false,
true,
true,
false,
true,
true,
false,
true,
false,
true,
true,
true,
false,
false,
false,
true,
false,
false,
false,
true,
true,
false,
true,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false,
true,
false,
false,
false,
true,
true,
true,
false,
false,
true,
false
]
\ No newline at end of file
[
135.747111636,
123.377054008,
140.527504552,
-72.299143906,
-23.851678949,
73.586193519,
-158.299382442,
177.477876032,
32.268518982,
-139.560009969,
115.203105183,
-106.025823607,
167.224138231,
103.378383732,
-97.498486285,
18.184723416,
69.137075711,
33.849002681,
-120.185228215,
-20.841408615,
-172.659492727,
-2.691464061,
22.426164066,
-98.416909437,
-31.603082708,
-85.072296561,
108.620987395,
-43.127078238,
-126.473562057,
-158.595489097,
-57.890678254,
-13.254016573,
-85.024504709,
171.663552644,
-146.495558248,
-10.606748276,
-118.786969354,
153.352057804,
-45.215545083,
37.038725288,
106.344071897,
-64.607402031,
85.148030911,
28.897784566,
39.51082061,
20.450382102,
-113.174943618,
71.60785784,
-168.202648062,
-157.338200017,
10.879588527,
-114.261694831,
-5.622927072,
-173.330830616,
-29.47002003,
-39.829034201,
50.031545162,
82.815735508,
-119.188760828,
-48.455928081,
163.964263034,
46.30378861,
-26.248889762,
-47.354615322,
155.388677633,
-166.710356904,
42.987233558,
144.275297374,
37.394383186,
-122.550388725,
177.469945914,
101.104677413,
109.429869885,
-104.919625624,
147.522756541,
-81.294703727,
122.744731363,
81.803603684,
26.321556167,
147.045441354,
147.256895816,
-174.211095908,
52.518769316,
-78.58250334,
-173.356685435,
-107.728209264,
-69.982325771,
-113.776095893,
-35.785267074,
-105.748545976,
-30.206523864,
-76.185311723,
-126.400112781,
-26.864958639,
56.840053629,
93.781553535,
-116.002949803,
-46.617140948,
176.846840093,
-144.24821335
]
[
"d35bf0d4-8d8f-4e17-a5c3-ad9bfd675266",
"db402774-eeb6-463b-9986-c458c44d8b5a",
"2a2e4101-b5f2-40b8-8750-e03f01661e60",
"76787cfa-f4eb-4d62-aaad-e1d588d00ad5",
"fd73894b-b500-4a7c-888c-06b5bd9cec65",
"cce1862a-cf31-4ef2-9e23-f1d23b4e6163",
"00a98bb0-2b6e-4368-8512-71c21aa87db7",
"ab9a8d69-cec7-4550-bd35-3ed678e22782",
"f18b48e1-5114-4fbe-9652-579e8d66950e",
"4efe3baa-7ac5-4d6a-a839-6b9cfe825764",
"b4aec119-5b0a-434c-b388-109816c482a5",
"e0ef0cbb-127a-4a28-9831-5741b4295275",
"d50286a5-cb7b-4c9e-be99-f214439bae8c",
"a981094c-f1ac-42ed-a9fa-86404c7210ff",
"2a34ee57-5815-4829-b77b-eeebaa8fe340",
"a0530d44-48f8-4eff-b9ea-8810c4308351",
"c6f91509-83e1-4ea1-9680-e667fbfd56ee",
"cab11402-dcdd-4454-b190-6da124947395",
"283d159c-2b18-4856-b4c7-5059252eaa15",
"146157c6-72a8-4051-9991-cb6ea6743d81",
"aef6f269-7306-4bd2-83f7-6d5605b5dc9a",
"37fe6027-d638-4017-80a9-e7b0567b278e",
"5003d731-33fb-4159-af61-d76348a44079",
"e0e06979-5f80-4713-9fe0-8a4d60dc89f8",
"7e85bdc3-0345-4cb6-9398-ccab06e79976",
"f2ebf5af-6568-4ffe-a46d-403863fd4b66",
"e0b5bb1c-b4dd-4535-9a9e-3c73f1167d46",
"c852d20b-6bcb-4b12-bd57-308296c64c5a",
"7ac3ae82-1818-49cd-a8a4-5ac77dfafd46",
"138004a9-76e2-4ad7-bd42-e74dabdbb803",
"ab25b5be-96be-45b0-b765-947b40ec36a6",
"08404734-fd57-499e-a4cf-71e9ec782ede",
"8dfdeb16-248b-4a21-bf89-2e22b11a4101",
"a0e44ef0-3b09-41e8-ad5d-ed8e6a1a2a67",
"a7981e49-188d-414a-9779-b1ad91e599d1",
"329186c0-bf27-4208-baf7-c0a0a5a2d5b7",
"cb5f3381-d33e-4b30-b1a9-f482623cad33",
"15031262-ca73-4e3c-bd0a-fcf89bdf0caf",
"6d7333d1-2e8c-4d78-bfde-5be47e70eb13",
"acaa160c-670a-4e8f-ac45-49416e77d5f9",
"228f87eb-cde4-4106-808b-2dbf3c7b6d2e",
"2ff830a3-5445-4d8e-b161-bddd30666697",
"f488bedd-ff6e-4108-b9a7-07f6da62f476",
"2e12b846-0a34-478e-adf7-a438493803e6",
"6686b8ef-7446-4d86-bd8c-df24119e3bfe",
"e474a5c5-5793-4d41-b4ab-5423acc56ef1",
"ac046573-e718-44dc-a0dc-9037eeaba6a9",
"6b0e9099-cf53-4d5a-8a71-977528628fcf",
"d51a3f22-0ff9-4087-ba9b-fcee2a2d8ade",
"bdc01286-3511-4d22-bfb8-76d01203d366",
"ca44eb84-17ff-4f27-8f1e-1bd25f4e8725",
"4e9a8c2f-be0b-4913-92d2-c801b9a50d04",
"7685d231-dadd-4041-9165-898397438ab7",
"86f0bf26-d66a-44d8-99f5-d6768addae3b",
"2ca1167c-72ba-45a0-aa42-faf033db0d0b",
"199a1182-ea55-49ff-ba51-71c29cdd0aac",
"be6a4dd2-c821-4aa0-8b83-d64d6644b5b2",
"4c5f4781-7f80-4daa-9c20-76b183000514",
"513b31bd-54fb-4d12-a427-42a7c13ff8e1",
"8e211bcb-d76c-4012-83ad-74dd7d23b687",
"44d5807e-0501-4f66-8779-e244d4fdca0a",
"db8cd555-0563-4b7b-b00c-eada300a7065",
"cb14d0c9-46cc-4797-bd3a-752b05629f07",
"4f68b3ef-ac9b-47a0-b6d7-57f398a5c6a5",
"77221aae-1bcf-471c-be45-7f31f733f9d6",
"42a7cac8-9e80-4c45-8c71-511d863c98ea",
"f9018d22-b82c-468c-bdb5-8864d5964801",
"75f4e9b8-62a2-4f21-ad8a-e19eff0419bc",
"9b7385c8-8653-4184-951c-b0ac1b36b42e",
"571018aa-ffbf-4b42-a16d-07b57a7f5f0e",
"35de4a2f-6bf1-45aa-b820-2a27ea833e44",
"0b8edb20-3bb4-4cb4-b089-31957466dbab",
"97da4778-9a7b-4140-a545-968148c81fb7",
"969f326c-8f2a-47c5-b41c-d9c2f06c9b9d",
"ae211037-8b53-4b17-bfc8-c06fc7774409",
"12c5c3c4-0bd5-45d3-bc1d-d04a3c65d3e6",
"ec02024f-ce43-4dd3-8169-a59f7baee043",
"5b6afe77-ce48-47ca-90a0-25cd10ca5ffd",
"2e3a61d4-6b8f-4d2f-ba86-878b4012efd8",
"19a88a67-a5d3-4647-898f-1cde07bce040",
"6db6f420-b5c8-48b9-bbb2-8864fe6fed65",
"5a45dbde-7b53-4f6b-b864-e3b63be3708a",
"c878321b-8a02-4239-9981-15760c2e7d15",
"4e36687f-8bf6-4b12-b496-3a8e382d067e",
"a59a63cd-43c0-4c6e-b208-6dbca86f8176",
"303308c4-2e4a-45b5-8bf3-3e66e9ad05a1",
"8b58fdf1-43a6-4c98-9547-6361b50791af",
"a3563591-72ed-42b5-8e41-bac1d76d70cf",
"38db8c78-3739-4f6e-8313-de4138082114",
"86615bea-7e73-4daf-95da-ae6b9eee1bbb",
"35d38e3e-076e-40dd-9aa8-05be2603bd59",
"9f84c62d-b454-4ba3-8c19-a01878985cdc",
"6721bbae-d765-4a06-8289-6fe46a1bf943",
"0837796f-d0dd-4e50-9b7c-1983e6cc7c48",
"021eb7d7-e869-49b9-80c3-9dd16ce2d981",
"819c56f8-e040-475d-aad5-c6d5e98b20aa",
"3a61ef02-735e-4229-937d-b3777a3f4e1f",
"79dfab84-12e6-4ec8-bfc8-460ae71e4eca",
"a106fabf-e149-476c-8053-b62388b6eb57",
"9a3900a5-bfb4-4de0-baa5-253a8bd0b634"
]
\ No newline at end of file
[
8125686,
8958709,
5976222,
1889524,
7968493,
1357486,
118415,
7081097,
4635968,
7555332,
2270233,
3428352,
8699968,
2087333,
7861337,
7554440,
2017031,
7981692,
6060687,
1877715,
3297474,
8373177,
6158629,
7853641,
3004441,
9650406,
2695251,
1180761,
4988426,
6043805,
8063373,
6103218,
2848339,
8188690,
9235573,
5949816,
6116081,
6471138,
3354531,
4787414,
9660600,
942529,
7278535,
7967399,
554292,
1436493,
267319,
2606657,
7900601,
4276634,
7996757,
8544466,
7266469,
3301373,
4005350,
6437652,
7717672,
7126292,
8588394,
2127902,
7410190,
1517806,
4583602,
3123440,
7747613,
5029464,
9834390,
3087227,
4913822,
7550487,
4518144,
5862588,
1778599,
9493290,
5588455,
3638706,
7394293,
4294719,
3837830,
6381878,
7175866,
8575492,
1415229,
1453733,
6972404,
9782571,
4234063,
7117418,
7293130,
8057071,
9345285,
7626648,
3358911,
4574537,
9371826,
7627107,
6154093,
5392367,
5398105,
6956377
]
\ No newline at end of file
This diff is collapsed.
[
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
]
\ No newline at end of file
This diff is collapsed.
Test data obtained from https://github.com/xpol/lua-rapidjson/tree/master/performance
...@@ -120,6 +120,28 @@ struct UTF8 { ...@@ -120,6 +120,28 @@ struct UTF8 {
} }
} }
template<typename OutputStream>
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
if (codepoint <= 0x7F)
PutUnsafe(os, static_cast<Ch>(codepoint & 0xFF));
else if (codepoint <= 0x7FF) {
PutUnsafe(os, static_cast<Ch>(0xC0 | ((codepoint >> 6) & 0xFF)));
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint & 0x3F))));
}
else if (codepoint <= 0xFFFF) {
PutUnsafe(os, static_cast<Ch>(0xE0 | ((codepoint >> 12) & 0xFF)));
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F)));
PutUnsafe(os, static_cast<Ch>(0x80 | (codepoint & 0x3F)));
}
else {
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
PutUnsafe(os, static_cast<Ch>(0xF0 | ((codepoint >> 18) & 0xFF)));
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 12) & 0x3F)));
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F)));
PutUnsafe(os, static_cast<Ch>(0x80 | (codepoint & 0x3F)));
}
}
template <typename InputStream> template <typename InputStream>
static bool Decode(InputStream& is, unsigned* codepoint) { static bool Decode(InputStream& is, unsigned* codepoint) {
#define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast<unsigned char>(c) & 0x3Fu) #define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast<unsigned char>(c) & 0x3Fu)
...@@ -261,6 +283,22 @@ struct UTF16 { ...@@ -261,6 +283,22 @@ struct UTF16 {
} }
} }
template<typename OutputStream>
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2);
if (codepoint <= 0xFFFF) {
RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair
PutUnsafe(os, static_cast<typename OutputStream::Ch>(codepoint));
}
else {
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
unsigned v = codepoint - 0x10000;
PutUnsafe(os, static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
PutUnsafe(os, (v & 0x3FF) | 0xDC00);
}
}
template <typename InputStream> template <typename InputStream>
static bool Decode(InputStream& is, unsigned* codepoint) { static bool Decode(InputStream& is, unsigned* codepoint) {
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2);
...@@ -386,6 +424,13 @@ struct UTF32 { ...@@ -386,6 +424,13 @@ struct UTF32 {
os.Put(codepoint); os.Put(codepoint);
} }
template<typename OutputStream>
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4);
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
PutUnsafe(os, codepoint);
}
template <typename InputStream> template <typename InputStream>
static bool Decode(InputStream& is, unsigned* codepoint) { static bool Decode(InputStream& is, unsigned* codepoint) {
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4);
...@@ -501,6 +546,12 @@ struct ASCII { ...@@ -501,6 +546,12 @@ struct ASCII {
os.Put(static_cast<Ch>(codepoint & 0xFF)); os.Put(static_cast<Ch>(codepoint & 0xFF));
} }
template<typename OutputStream>
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
RAPIDJSON_ASSERT(codepoint <= 0x7F);
PutUnsafe(os, static_cast<Ch>(codepoint & 0xFF));
}
template <typename InputStream> template <typename InputStream>
static bool Decode(InputStream& is, unsigned* codepoint) { static bool Decode(InputStream& is, unsigned* codepoint) {
uint8_t c = static_cast<uint8_t>(is.Take()); uint8_t c = static_cast<uint8_t>(is.Take());
...@@ -571,6 +622,13 @@ struct AutoUTF { ...@@ -571,6 +622,13 @@ struct AutoUTF {
(*f[os.GetType()])(os, codepoint); (*f[os.GetType()])(os, codepoint);
} }
template<typename OutputStream>
RAPIDJSON_FORCEINLINE static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
typedef void (*EncodeFunc)(OutputStream&, unsigned);
static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) };
(*f[os.GetType()])(os, codepoint);
}
template <typename InputStream> template <typename InputStream>
RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) {
typedef bool (*DecodeFunc)(InputStream&, unsigned*); typedef bool (*DecodeFunc)(InputStream&, unsigned*);
...@@ -604,6 +662,15 @@ struct Transcoder { ...@@ -604,6 +662,15 @@ struct Transcoder {
return true; return true;
} }
template<typename InputStream, typename OutputStream>
RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
unsigned codepoint;
if (!SourceEncoding::Decode(is, &codepoint))
return false;
TargetEncoding::EncodeUnsafe(os, codepoint);
return true;
}
//! Validate one Unicode codepoint from an encoded stream. //! Validate one Unicode codepoint from an encoded stream.
template<typename InputStream, typename OutputStream> template<typename InputStream, typename OutputStream>
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
...@@ -611,6 +678,10 @@ struct Transcoder { ...@@ -611,6 +678,10 @@ struct Transcoder {
} }
}; };
// Forward declaration.
template<typename Stream>
inline void PutUnsafe(Stream& stream, typename Stream::Ch c);
//! Specialization of Transcoder with same source and target encoding. //! Specialization of Transcoder with same source and target encoding.
template<typename Encoding> template<typename Encoding>
struct Transcoder<Encoding, Encoding> { struct Transcoder<Encoding, Encoding> {
...@@ -620,6 +691,12 @@ struct Transcoder<Encoding, Encoding> { ...@@ -620,6 +691,12 @@ struct Transcoder<Encoding, Encoding> {
return true; return true;
} }
template<typename InputStream, typename OutputStream>
RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is different from primary template class.
return true;
}
template<typename InputStream, typename OutputStream> template<typename InputStream, typename OutputStream>
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
return Encoding::Validate(is, os); // source/target encoding are the same return Encoding::Validate(is, os); // source/target encoding are the same
......
...@@ -108,11 +108,21 @@ public: ...@@ -108,11 +108,21 @@ public:
// Optimization note: try to minimize the size of this function for force inline. // Optimization note: try to minimize the size of this function for force inline.
// Expansion is run very infrequently, so it is moved to another (probably non-inline) function. // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
template<typename T> template<typename T>
RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) {
// Expand the stack if needed // Expand the stack if needed
if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count >= stackEnd_)) if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count >= stackEnd_))
Expand<T>(count); Expand<T>(count);
}
template<typename T>
RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
Reserve<T>(count);
return PushUnsafe<T>(count);
}
template<typename T>
RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) {
RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count < stackEnd_);
T* ret = reinterpret_cast<T*>(stackTop_); T* ret = reinterpret_cast<T*>(stackTop_);
stackTop_ += sizeof(T) * count; stackTop_ += sizeof(T) * count;
return ret; return ret;
......
...@@ -614,11 +614,25 @@ struct StreamTraits { ...@@ -614,11 +614,25 @@ struct StreamTraits {
enum { copyOptimization = 0 }; enum { copyOptimization = 0 };
}; };
//! Reserve n characters for writing to a stream.
template<typename Stream>
inline void PutReserve(Stream& stream, size_t count) {
(void)stream;
(void)count;
}
//! Write character to a stream, presuming buffer is reserved.
template<typename Stream>
inline void PutUnsafe(Stream& stream, typename Stream::Ch c) {
stream.Put(c);
}
//! Put N copies of a character to a stream. //! Put N copies of a character to a stream.
template<typename Stream, typename Ch> template<typename Stream, typename Ch>
inline void PutN(Stream& stream, Ch c, size_t n) { inline void PutN(Stream& stream, Ch c, size_t n) {
PutReserve<Stream>(stream, n);
for (size_t i = 0; i < n; i++) for (size_t i = 0; i < n; i++)
stream.Put(c); PutUnsafe(stream, c);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
......
...@@ -48,6 +48,7 @@ public: ...@@ -48,6 +48,7 @@ public:
#endif #endif
void Put(Ch c) { *stack_.template Push<Ch>() = c; } void Put(Ch c) { *stack_.template Push<Ch>() = c; }
void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; }
void Flush() {} void Flush() {}
void Clear() { stack_.Clear(); } void Clear() { stack_.Clear(); }
...@@ -57,6 +58,8 @@ public: ...@@ -57,6 +58,8 @@ public:
stack_.ShrinkToFit(); stack_.ShrinkToFit();
stack_.template Pop<Ch>(1); stack_.template Pop<Ch>(1);
} }
void Reserve(size_t count) { stack_.template Reserve<Ch>(count); }
Ch* Push(size_t count) { return stack_.template Push<Ch>(count); } Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
void Pop(size_t count) { stack_.template Pop<Ch>(count); } void Pop(size_t count) { stack_.template Pop<Ch>(count); }
...@@ -82,6 +85,16 @@ private: ...@@ -82,6 +85,16 @@ private:
//! String buffer with UTF8 encoding //! String buffer with UTF8 encoding
typedef GenericStringBuffer<UTF8<> > StringBuffer; typedef GenericStringBuffer<UTF8<> > StringBuffer;
template<typename Encoding, typename Allocator>
inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream, size_t count) {
stream.Reserve(count);
}
template<typename Encoding, typename Allocator>
inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c) {
stream.PutUnsafe(c);
}
//! Implement specialized version of PutN() with memset() for better performance. //! Implement specialized version of PutN() with memset() for better performance.
template<> template<>
inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) { inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
......
...@@ -189,15 +189,18 @@ protected: ...@@ -189,15 +189,18 @@ protected:
static const size_t kDefaultLevelDepth = 32; static const size_t kDefaultLevelDepth = 32;
bool WriteNull() { bool WriteNull() {
os_->Put('n'); os_->Put('u'); os_->Put('l'); os_->Put('l'); return true; PutReserve(*os_, 4);
PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 'l'); return true;
} }
bool WriteBool(bool b) { bool WriteBool(bool b) {
if (b) { if (b) {
os_->Put('t'); os_->Put('r'); os_->Put('u'); os_->Put('e'); PutReserve(*os_, 4);
PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'r'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'e');
} }
else { else {
os_->Put('f'); os_->Put('a'); os_->Put('l'); os_->Put('s'); os_->Put('e'); PutReserve(*os_, 5);
PutUnsafe(*os_, 'f'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 's'); PutUnsafe(*os_, 'e');
} }
return true; return true;
} }
...@@ -205,40 +208,45 @@ protected: ...@@ -205,40 +208,45 @@ protected:
bool WriteInt(int i) { bool WriteInt(int i) {
char buffer[11]; char buffer[11];
const char* end = internal::i32toa(i, buffer); const char* end = internal::i32toa(i, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (const char* p = buffer; p != end; ++p) for (const char* p = buffer; p != end; ++p)
os_->Put(static_cast<typename TargetEncoding::Ch>(*p)); PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
return true; return true;
} }
bool WriteUint(unsigned u) { bool WriteUint(unsigned u) {
char buffer[10]; char buffer[10];
const char* end = internal::u32toa(u, buffer); const char* end = internal::u32toa(u, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (const char* p = buffer; p != end; ++p) for (const char* p = buffer; p != end; ++p)
os_->Put(static_cast<typename TargetEncoding::Ch>(*p)); PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
return true; return true;
} }
bool WriteInt64(int64_t i64) { bool WriteInt64(int64_t i64) {
char buffer[21]; char buffer[21];
const char* end = internal::i64toa(i64, buffer); const char* end = internal::i64toa(i64, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (const char* p = buffer; p != end; ++p) for (const char* p = buffer; p != end; ++p)
os_->Put(static_cast<typename TargetEncoding::Ch>(*p)); PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
return true; return true;
} }
bool WriteUint64(uint64_t u64) { bool WriteUint64(uint64_t u64) {
char buffer[20]; char buffer[20];
char* end = internal::u64toa(u64, buffer); char* end = internal::u64toa(u64, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (char* p = buffer; p != end; ++p) for (char* p = buffer; p != end; ++p)
os_->Put(static_cast<typename TargetEncoding::Ch>(*p)); PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
return true; return true;
} }
bool WriteDouble(double d) { bool WriteDouble(double d) {
char buffer[25]; char buffer[25];
char* end = internal::dtoa(d, buffer); char* end = internal::dtoa(d, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (char* p = buffer; p != end; ++p) for (char* p = buffer; p != end; ++p)
os_->Put(static_cast<typename TargetEncoding::Ch>(*p)); PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
return true; return true;
} }
...@@ -256,7 +264,12 @@ protected: ...@@ -256,7 +264,12 @@ protected:
#undef Z16 #undef Z16
}; };
os_->Put('\"'); if (TargetEncoding::supportUnicode)
PutReserve(*os_, 2 + length * 6); // "\uxxxx..."
else
PutReserve(*os_, 2 + length * 12); // "\uxxxx\uyyyy..."
PutUnsafe(*os_, '\"');
GenericStringStream<SourceEncoding> is(str); GenericStringStream<SourceEncoding> is(str);
while (is.Tell() < length) { while (is.Tell() < length) {
const Ch c = is.Peek(); const Ch c = is.Peek();
...@@ -265,13 +278,13 @@ protected: ...@@ -265,13 +278,13 @@ protected:
unsigned codepoint; unsigned codepoint;
if (!SourceEncoding::Decode(is, &codepoint)) if (!SourceEncoding::Decode(is, &codepoint))
return false; return false;
os_->Put('\\'); PutUnsafe(*os_, '\\');
os_->Put('u'); PutUnsafe(*os_, 'u');
if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) {
os_->Put(hexDigits[(codepoint >> 12) & 15]); PutUnsafe(*os_, hexDigits[(codepoint >> 12) & 15]);
os_->Put(hexDigits[(codepoint >> 8) & 15]); PutUnsafe(*os_, hexDigits[(codepoint >> 8) & 15]);
os_->Put(hexDigits[(codepoint >> 4) & 15]); PutUnsafe(*os_, hexDigits[(codepoint >> 4) & 15]);
os_->Put(hexDigits[(codepoint ) & 15]); PutUnsafe(*os_, hexDigits[(codepoint ) & 15]);
} }
else { else {
RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF); RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF);
...@@ -279,34 +292,34 @@ protected: ...@@ -279,34 +292,34 @@ protected:
unsigned s = codepoint - 0x010000; unsigned s = codepoint - 0x010000;
unsigned lead = (s >> 10) + 0xD800; unsigned lead = (s >> 10) + 0xD800;
unsigned trail = (s & 0x3FF) + 0xDC00; unsigned trail = (s & 0x3FF) + 0xDC00;
os_->Put(hexDigits[(lead >> 12) & 15]); PutUnsafe(*os_, hexDigits[(lead >> 12) & 15]);
os_->Put(hexDigits[(lead >> 8) & 15]); PutUnsafe(*os_, hexDigits[(lead >> 8) & 15]);
os_->Put(hexDigits[(lead >> 4) & 15]); PutUnsafe(*os_, hexDigits[(lead >> 4) & 15]);
os_->Put(hexDigits[(lead ) & 15]); PutUnsafe(*os_, hexDigits[(lead ) & 15]);
os_->Put('\\'); PutUnsafe(*os_, '\\');
os_->Put('u'); PutUnsafe(*os_, 'u');
os_->Put(hexDigits[(trail >> 12) & 15]); PutUnsafe(*os_, hexDigits[(trail >> 12) & 15]);
os_->Put(hexDigits[(trail >> 8) & 15]); PutUnsafe(*os_, hexDigits[(trail >> 8) & 15]);
os_->Put(hexDigits[(trail >> 4) & 15]); PutUnsafe(*os_, hexDigits[(trail >> 4) & 15]);
os_->Put(hexDigits[(trail ) & 15]); PutUnsafe(*os_, hexDigits[(trail ) & 15]);
} }
} }
else if ((sizeof(Ch) == 1 || static_cast<unsigned>(c) < 256) && escape[static_cast<unsigned char>(c)]) { else if ((sizeof(Ch) == 1 || static_cast<unsigned>(c) < 256) && escape[static_cast<unsigned char>(c)]) {
is.Take(); is.Take();
os_->Put('\\'); PutUnsafe(*os_, '\\');
os_->Put(static_cast<typename TargetEncoding::Ch>(escape[static_cast<unsigned char>(c)])); PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(escape[static_cast<unsigned char>(c)]));
if (escape[static_cast<unsigned char>(c)] == 'u') { if (escape[static_cast<unsigned char>(c)] == 'u') {
os_->Put('0'); PutUnsafe(*os_, '0');
os_->Put('0'); PutUnsafe(*os_, '0');
os_->Put(hexDigits[static_cast<unsigned char>(c) >> 4]); PutUnsafe(*os_, hexDigits[static_cast<unsigned char>(c) >> 4]);
os_->Put(hexDigits[static_cast<unsigned char>(c) & 0xF]); PutUnsafe(*os_, hexDigits[static_cast<unsigned char>(c) & 0xF]);
} }
} }
else else
if (!Transcoder<SourceEncoding, TargetEncoding>::Transcode(is, *os_)) if (!Transcoder<SourceEncoding, TargetEncoding>::TranscodeUnsafe(is, *os_))
return false; return false;
} }
os_->Put('\"'); PutUnsafe(*os_, '\"');
return true; return true;
} }
......
...@@ -65,7 +65,7 @@ public: ...@@ -65,7 +65,7 @@ public:
PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {} PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {}
virtual void SetUp() { virtual void SetUp() {
{
const char *paths[] = { const char *paths[] = {
"data/sample.json", "data/sample.json",
"bin/data/sample.json", "bin/data/sample.json",
...@@ -73,6 +73,7 @@ public: ...@@ -73,6 +73,7 @@ public:
"../../bin/data/sample.json", "../../bin/data/sample.json",
"../../../bin/data/sample.json" "../../../bin/data/sample.json"
}; };
FILE *fp = 0; FILE *fp = 0;
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
fp = fopen(filename_ = paths[i], "rb"); fp = fopen(filename_ = paths[i], "rb");
...@@ -88,8 +89,10 @@ public: ...@@ -88,8 +89,10 @@ public:
ASSERT_EQ(length_, fread(json_, 1, length_, fp)); ASSERT_EQ(length_, fread(json_, 1, length_, fp));
json_[length_] = '\0'; json_[length_] = '\0';
fclose(fp); fclose(fp);
}
// whitespace test // whitespace test
{
whitespace_length_ = 1024 * 1024; whitespace_length_ = 1024 * 1024;
whitespace_ = (char *)malloc(whitespace_length_ + 4); whitespace_ = (char *)malloc(whitespace_length_ + 4);
char *p = whitespace_; char *p = whitespace_;
...@@ -105,11 +108,55 @@ public: ...@@ -105,11 +108,55 @@ public:
*p++ = '\0'; *p++ = '\0';
} }
// types test
{
const char *typespaths[] = {
"data/types",
"bin/types",
"../bin/types",
"../../bin/types/",
"../../../bin/types"
};
const char* typesfilenames[] = {
"booleans.json",
"floats.json",
"guids.json",
"integers.json",
"mixed.json",
"nulls.json",
"paragraphs.json"
};
for (size_t j = 0; j < sizeof(typesfilenames) / sizeof(typesfilenames[0]); j++) {
types_[j] = 0;
for (size_t i = 0; i < sizeof(typespaths) / sizeof(typespaths[0]); i++) {
char filename[256];
sprintf(filename, "%s/%s", typespaths[i], typesfilenames[j]);
if (FILE* fp = fopen(filename, "rb")) {
fseek(fp, 0, SEEK_END);
size_t length = (size_t)ftell(fp);
fseek(fp, 0, SEEK_SET);
types_[j] = (char*)malloc(length + 1);
ASSERT_EQ(length, fread(types_[j], 1, length, fp));
types_[j][length] = '\0';
fclose(fp);
break;
}
}
}
}
}
virtual void TearDown() { virtual void TearDown() {
free(json_); free(json_);
free(whitespace_); free(whitespace_);
json_ = 0; json_ = 0;
whitespace_ = 0; whitespace_ = 0;
for (size_t i = 0; i < 7; i++) {
free(types_[i]);
types_[i] = 0;
}
} }
private: private:
...@@ -122,6 +169,7 @@ protected: ...@@ -122,6 +169,7 @@ protected:
size_t length_; size_t length_;
char *whitespace_; char *whitespace_;
size_t whitespace_length_; size_t whitespace_length_;
char *types_[7];
static const size_t kTrialCount = 1000; static const size_t kTrialCount = 1000;
}; };
......
...@@ -45,7 +45,10 @@ public: ...@@ -45,7 +45,10 @@ public:
temp_ = (char *)malloc(length_ + 1); temp_ = (char *)malloc(length_ + 1);
// Parse as a document // Parse as a document
EXPECT_FALSE(doc_.Parse(json_).IsNull()); EXPECT_FALSE(doc_.Parse(json_).HasParseError());
for (size_t i = 0; i < 7; i++)
EXPECT_FALSE(typesDoc_[i].Parse(types_[i]).HasParseError());
} }
virtual void TearDown() { virtual void TearDown() {
...@@ -60,6 +63,7 @@ private: ...@@ -60,6 +63,7 @@ private:
protected: protected:
char *temp_; char *temp_;
Document doc_; Document doc_;
Document typesDoc_[7];
}; };
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler)) {
...@@ -250,8 +254,10 @@ TEST_F(RapidJson, DocumentAccept) { ...@@ -250,8 +254,10 @@ TEST_F(RapidJson, DocumentAccept) {
} }
struct NullStream { struct NullStream {
typedef char Ch;
NullStream() /*: length_(0)*/ {} NullStream() /*: length_(0)*/ {}
void Put(char) { /*++length_;*/ } void Put(Ch) { /*++length_;*/ }
void Flush() {} void Flush() {}
//size_t length_; //size_t length_;
}; };
...@@ -278,6 +284,25 @@ TEST_F(RapidJson, Writer_StringBuffer) { ...@@ -278,6 +284,25 @@ TEST_F(RapidJson, Writer_StringBuffer) {
} }
} }
#define TEST_TYPED(index, Name)\
TEST_F(RapidJson, Writer_StringBuffer_##Name) {\
for (size_t i = 0; i < kTrialCount * 10; i++) {\
StringBuffer s(0, 1024 * 1024);\
Writer<StringBuffer> writer(s);\
typesDoc_[index].Accept(writer);\
const char* str = s.GetString();\
(void)str;\
}\
}\
TEST_TYPED(0, Booleans)
TEST_TYPED(1, Floats)
TEST_TYPED(2, Guids)
TEST_TYPED(3, Integers)
TEST_TYPED(4, Mixed)
TEST_TYPED(5, Nulls)
TEST_TYPED(6, Paragraphs)
TEST_F(RapidJson, PrettyWriter_StringBuffer) { TEST_F(RapidJson, PrettyWriter_StringBuffer) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
StringBuffer s(0, 2048 * 1024); StringBuffer s(0, 2048 * 1024);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment