Skip to content

Commit 2f92a34

Browse files
authored
Turns out that passing dom::element by reference can be a performance killer. (simdjson#1086)
* Turns out that passing dom::element by reference can be a performance killer. * Tweaking.
1 parent 54ed24f commit 2f92a34

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

benchmark/parseandstatcompetition.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ struct Stat {
131131
size_t stringLength; // Number of code units in all strings
132132
};
133133

134-
static void GenStatPlus(Stat &stat, const dom::element &v) {
134+
static void GenStatPlus(Stat &stat, const dom::element v) {
135135
switch (v.type()) {
136136
case dom::element_type::ARRAY:
137137
for (dom::element child : dom::array(v)) {
@@ -142,7 +142,8 @@ static void GenStatPlus(Stat &stat, const dom::element &v) {
142142
break;
143143
case dom::element_type::OBJECT:
144144
for (dom::key_value_pair kv : dom::object(v)) {
145-
GenStatPlus(stat, dom::element(kv.value));
145+
GenStatPlus(stat, kv.value);
146+
stat.stringLength += kv.key.size();
146147
stat.memberCount++;
147148
stat.stringCount++;
148149
}

doc/performance.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ are still some scenarios where tuning can enhance performance.
1212
* [Number parsing](#number-parsing)
1313
* [Visual Studio](#visual-studio)
1414
* [Downclocking](#downclocking)
15-
15+
* [Best Use of the DOM API](#best-use-of-the-dom-api)
1616

1717
Reusing the parser for maximum efficiency
1818
-----------------------------------------
@@ -181,3 +181,9 @@ On some Intel processors, using SIMD instructions in a sustained manner on the s
181181
The simdjson library does not currently support AVX-512 instructions and it does not make use of heavy 256-bit instructions. We do use vectorized multiplications, but only using 128-bit registers. Thus there should be no downclocking due to simdjson on recent processors.
182182

183183
You may still be worried about which SIMD instruction set is used by simdjson. Thankfully, [you can always determine and change which architecture-specific implementation is used](implementation-selection.md) by simdjson. Thus even if your CPU supports AVX2, you do not need to use AVX2. You are in control.
184+
185+
Best Use of the DOM API
186+
-------------------------
187+
188+
The simdjson API provides access to the JSON DOM (document-object-model) content as a tree of `dom::element` instances, each representing an object, an array or an atomic type (null, true, false, number). These `dom::element` instances are lightweight objects (e.g., spanning 16 bytes) and it might be advantageous to pass them by value, as opposed to passing them by reference or by pointer.
189+

0 commit comments

Comments
 (0)