Skip to content

Commit f6ee4da

Browse files
committed
Add a "sum" benchmark with no appending to vector
1 parent f7a95bd commit f6ee4da

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

benchmark/largerandom/dom.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class Dom {
2323
simdjson_really_inline bool Dom::Run(const padded_string &json) {
2424
container.clear();
2525

26-
dom::element doc = parser.parse(json);
27-
for (auto point : doc) {
26+
for (auto point : parser.parse(json)) {
2827
container.emplace_back(my_point{point["x"], point["y"], point["z"]});
2928
}
3029

@@ -33,6 +32,38 @@ simdjson_really_inline bool Dom::Run(const padded_string &json) {
3332

3433
BENCHMARK_TEMPLATE(LargeRandom, Dom);
3534

35+
namespace sum {
36+
37+
class Dom {
38+
public:
39+
simdjson_really_inline bool Run(const padded_string &json);
40+
41+
simdjson_really_inline my_point &Result() { return sum; }
42+
simdjson_really_inline size_t ItemCount() { return count; }
43+
44+
private:
45+
dom::parser parser{};
46+
my_point sum{};
47+
size_t count{};
48+
};
49+
50+
simdjson_really_inline bool Dom::Run(const padded_string &json) {
51+
sum = { 0, 0, 0 };
52+
count = 0;
53+
54+
for (auto coord : parser.parse(json)) {
55+
sum.x += double(coord["x"]);
56+
sum.y += double(coord["y"]);
57+
sum.z += double(coord["z"]);
58+
count++;
59+
}
60+
61+
return true;
62+
}
63+
64+
BENCHMARK_TEMPLATE(LargeRandomSum, Dom);
65+
66+
} // namespace sum
3667
} // namespace largerandom
3768

3869
#endif // SIMDJSON_EXCEPTIONS

benchmark/largerandom/iter.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,48 @@ simdjson_really_inline bool Iter::Run(const padded_string &json) {
5151

5252
BENCHMARK_TEMPLATE(LargeRandom, Iter);
5353

54+
} // unnamed namespace
55+
56+
namespace sum {
57+
namespace {
58+
59+
class Iter {
60+
public:
61+
simdjson_really_inline bool Run(const padded_string &json);
62+
63+
simdjson_really_inline my_point &Result() { return sum; }
64+
simdjson_really_inline size_t ItemCount() { return count; }
65+
66+
private:
67+
ondemand::parser parser{};
68+
my_point sum{};
69+
size_t count{};
70+
};
71+
72+
simdjson_really_inline bool Iter::Run(const padded_string &json) {
73+
sum = {0,0,0};
74+
count = 0;
75+
76+
auto iter = parser.iterate_raw(json).value();
77+
if (!iter.start_array()) { return false; }
78+
do {
79+
if (!iter.start_object() || !iter.find_field_raw("x")) { return false; }
80+
sum.x += iter.get_double();
81+
if (!iter.has_next_field() || !iter.find_field_raw("y")) { return false; }
82+
sum.y += iter.get_double();
83+
if (!iter.has_next_field() || !iter.find_field_raw("z")) { return false; }
84+
sum.z += iter.get_double();
85+
if (iter.skip_container()) { return false; } // Skip the rest of the tweet object
86+
count++;
87+
} while (iter.has_next_element());
88+
89+
return true;
5490
}
91+
92+
BENCHMARK_TEMPLATE(LargeRandomSum, Iter);
93+
94+
} // unnamed namespace
95+
} // namespace sum
5596
} // namespace largerandom
5697

5798
#endif // SIMDJSON_EXCEPTIONS

benchmark/largerandom/largerandom.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace largerandom {
1010
template<typename T> static void LargeRandom(benchmark::State &state);
11+
namespace sum {
12+
template<typename T> static void LargeRandomSum(benchmark::State &state);
13+
}
1114

1215
using namespace simdjson;
1316

@@ -65,6 +68,13 @@ template<typename T> static void LargeRandom(benchmark::State &state) {
6568
JsonBenchmark<T, Dom>(state, get_built_json_array());
6669
}
6770

71+
namespace sum {
72+
73+
template<typename T> static void LargeRandomSum(benchmark::State &state) {
74+
JsonBenchmark<T, Dom>(state, get_built_json_array());
75+
}
76+
77+
}
6878
} // namespace largerandom
6979

7080
#endif // SIMDJSON_EXCEPTIONS

benchmark/largerandom/ondemand.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,45 @@ simdjson_really_inline bool OnDemand::Run(const padded_string &json) {
3939

4040
BENCHMARK_TEMPLATE(LargeRandom, OnDemand);
4141

42+
} // unnamed namespace
43+
44+
namespace sum {
45+
namespace {
46+
47+
class OnDemand {
48+
public:
49+
simdjson_really_inline bool Run(const padded_string &json);
50+
51+
simdjson_really_inline my_point &Result() { return sum; }
52+
simdjson_really_inline size_t ItemCount() { return count; }
53+
54+
private:
55+
ondemand::parser parser{};
56+
my_point sum{};
57+
size_t count{};
58+
};
59+
60+
simdjson_really_inline bool OnDemand::Run(const padded_string &json) {
61+
sum = {0,0,0};
62+
count = 0;
63+
64+
auto doc = parser.iterate(json);
65+
// TODO this sucks, you should be able to just say for ( ... : doc)
66+
auto array = doc.get_array();
67+
for (ondemand::object coord : array) {
68+
sum.x += double(coord["x"]);
69+
sum.y += double(coord["y"]);
70+
sum.z += double(coord["z"]);
71+
count++;
72+
}
73+
74+
return true;
4275
}
76+
77+
BENCHMARK_TEMPLATE(LargeRandomSum, OnDemand);
78+
79+
} // unnamed namespace
80+
} // namespace sum
4381
} // namespace largerandom
4482

4583
#endif // SIMDJSON_EXCEPTIONS

0 commit comments

Comments
 (0)