Skip to content

Commit 622d9c9

Browse files
committed
Replace as_X and is_X with get<T> and is<T>
1 parent 62da98a commit 622d9c9

17 files changed

+280
-365
lines changed

benchmark/bench_dom_api.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ static void twitter_count(State& state) {
2525
}
2626
BENCHMARK(twitter_count);
2727

28+
SIMDJSON_PUSH_DISABLE_WARNINGS
29+
SIMDJSON_DISABLE_DEPRECATED_WARNING
2830
static void iterator_twitter_count(State& state) {
2931
// Prints the number of results in twitter.json
3032
padded_string json = padded_string::load(JSON_TEST_PATH);
@@ -41,14 +43,15 @@ static void iterator_twitter_count(State& state) {
4143
}
4244
}
4345
BENCHMARK(iterator_twitter_count);
46+
SIMDJSON_POP_DISABLE_WARNINGS
4447

4548
static void twitter_default_profile(State& state) {
4649
// Count unique users with a default profile.
4750
dom::parser parser;
4851
dom::element doc = parser.load(JSON_TEST_PATH);
4952
for (auto _ : state) {
5053
set<string_view> default_users;
51-
for (dom::object tweet : doc["statuses"].as_array()) {
54+
for (dom::object tweet : doc["statuses"].get<dom::array>()) {
5255
dom::object user = tweet["user"];
5356
if (user["default_profile"]) {
5457
default_users.insert(user["screen_name"]);
@@ -65,11 +68,11 @@ static void twitter_image_sizes(State& state) {
6568
dom::element doc = parser.load(JSON_TEST_PATH);
6669
for (auto _ : state) {
6770
set<tuple<uint64_t, uint64_t>> image_sizes;
68-
for (dom::object tweet : doc["statuses"].as_array()) {
71+
for (dom::object tweet : doc["statuses"].get<dom::array>()) {
6972
auto [media, not_found] = tweet["entities"]["media"];
7073
if (!not_found) {
71-
for (dom::object image : media.as_array()) {
72-
for (auto [key, size] : image["sizes"].as_object()) {
74+
for (dom::object image : media.get<dom::array>()) {
75+
for (auto [key, size] : image["sizes"].get<dom::object>()) {
7376
image_sizes.insert({ size["w"], size["h"] });
7477
}
7578
}
@@ -87,7 +90,7 @@ static void error_code_twitter_count(State& state) noexcept {
8790
dom::parser parser;
8891
dom::element doc = parser.load(JSON_TEST_PATH);
8992
for (auto _ : state) {
90-
auto [value, error] = doc["search_metadata"]["count"].as_uint64_t();
93+
auto [value, error] = doc["search_metadata"]["count"].get<uint64_t>();
9194
if (error) { return; }
9295
if (value != 100) { return; }
9396
}
@@ -101,15 +104,15 @@ static void error_code_twitter_default_profile(State& state) noexcept {
101104
for (auto _ : state) {
102105
set<string_view> default_users;
103106

104-
auto [tweets, error] = doc["statuses"].as_array();
107+
auto [tweets, error] = doc["statuses"].get<dom::array>();
105108
if (error) { return; }
106109
for (dom::element tweet : tweets) {
107-
auto [user, error2] = tweet["user"].as_object();
110+
auto [user, error2] = tweet["user"].get<dom::object>();
108111
if (error2) { return; }
109-
auto [default_profile, error3] = user["default_profile"].as_bool();
112+
auto [default_profile, error3] = user["default_profile"].get<bool>();
110113
if (error3) { return; }
111114
if (default_profile) {
112-
auto [screen_name, error4] = user["screen_name"].as_string();
115+
auto [screen_name, error4] = user["screen_name"].get<std::string_view>();
113116
if (error4) { return; }
114117
default_users.insert(screen_name);
115118
}
@@ -120,6 +123,8 @@ static void error_code_twitter_default_profile(State& state) noexcept {
120123
}
121124
BENCHMARK(error_code_twitter_default_profile);
122125

126+
SIMDJSON_PUSH_DISABLE_WARNINGS
127+
SIMDJSON_DISABLE_DEPRECATED_WARNING
123128
static void iterator_twitter_default_profile(State& state) {
124129
// Count unique users with a default profile.
125130
padded_string json = padded_string::load(JSON_TEST_PATH);
@@ -128,7 +133,7 @@ static void iterator_twitter_default_profile(State& state) {
128133
set<string_view> default_users;
129134
ParsedJson::Iterator iter(pj);
130135

131-
// for (dom::object tweet : doc["statuses"].as_array()) {
136+
// for (dom::object tweet : doc["statuses"].get<dom::array>()) {
132137
if (!(iter.move_to_key("statuses") && iter.is_array())) { return; }
133138
if (iter.down()) { // first status
134139
do {
@@ -156,6 +161,7 @@ static void iterator_twitter_default_profile(State& state) {
156161
if (default_users.size() != 86) { return; }
157162
}
158163
}
164+
SIMDJSON_POP_DISABLE_WARNINGS
159165
BENCHMARK(iterator_twitter_default_profile);
160166

161167
static void error_code_twitter_image_sizes(State& state) noexcept {
@@ -164,17 +170,17 @@ static void error_code_twitter_image_sizes(State& state) noexcept {
164170
dom::element doc = parser.load(JSON_TEST_PATH);
165171
for (auto _ : state) {
166172
set<tuple<uint64_t, uint64_t>> image_sizes;
167-
auto [statuses, error] = doc["statuses"].as_array();
173+
auto [statuses, error] = doc["statuses"].get<dom::array>();
168174
if (error) { return; }
169175
for (dom::element tweet : statuses) {
170-
auto [images, not_found] = tweet["entities"]["media"].as_array();
176+
auto [images, not_found] = tweet["entities"]["media"].get<dom::array>();
171177
if (!not_found) {
172178
for (dom::element image : images) {
173-
auto [sizes, error2] = image["sizes"].as_object();
179+
auto [sizes, error2] = image["sizes"].get<dom::object>();
174180
if (error2) { return; }
175181
for (auto [key, size] : sizes) {
176-
auto [width, error3] = size["w"].as_uint64_t();
177-
auto [height, error4] = size["h"].as_uint64_t();
182+
auto [width, error3] = size["w"].get<uint64_t>();
183+
auto [height, error4] = size["h"].get<uint64_t>();
178184
if (error3 || error4) { return; }
179185
image_sizes.insert({ width, height });
180186
}
@@ -186,6 +192,8 @@ static void error_code_twitter_image_sizes(State& state) noexcept {
186192
}
187193
BENCHMARK(error_code_twitter_image_sizes);
188194

195+
SIMDJSON_PUSH_DISABLE_WARNINGS
196+
SIMDJSON_DISABLE_DEPRECATED_WARNING
189197
static void iterator_twitter_image_sizes(State& state) {
190198
// Count unique image sizes
191199
padded_string json = padded_string::load(JSON_TEST_PATH);
@@ -194,7 +202,7 @@ static void iterator_twitter_image_sizes(State& state) {
194202
set<tuple<uint64_t, uint64_t>> image_sizes;
195203
ParsedJson::Iterator iter(pj);
196204

197-
// for (dom::object tweet : doc["statuses"].as_array()) {
205+
// for (dom::object tweet : doc["statuses"].get<dom::array>()) {
198206
if (!(iter.move_to_key("statuses") && iter.is_array())) { return; }
199207
if (iter.down()) { // first status
200208
do {
@@ -206,11 +214,11 @@ static void iterator_twitter_image_sizes(State& state) {
206214
if (iter.move_to_key("media")) {
207215
if (!iter.is_array()) { return; }
208216

209-
// for (dom::object image : media.as_array()) {
217+
// for (dom::object image : media.get<dom::array>()) {
210218
if (iter.down()) { // first media
211219
do {
212220

213-
// for (auto [key, size] : image["sizes"].as_object()) {
221+
// for (auto [key, size] : image["sizes"].get<dom::object>()) {
214222
if (!(iter.move_to_key("sizes") && iter.is_object())) { return; }
215223
if (iter.down()) { // first size
216224
do {
@@ -256,5 +264,6 @@ static void print_json(State& state) noexcept {
256264
}
257265
}
258266
BENCHMARK(print_json);
267+
SIMDJSON_POP_DISABLE_WARNINGS
259268

260269
BENCHMARK_MAIN();

benchmark/bench_parse_call.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ using namespace std;
66

77
const padded_string EMPTY_ARRAY("[]", 2);
88

9+
SIMDJSON_PUSH_DISABLE_WARNINGS
10+
SIMDJSON_DISABLE_DEPRECATED_WARNING
911
static void json_parse(State& state) {
1012
dom::parser parser;
1113
if (parser.set_capacity(EMPTY_ARRAY.length())) { return; }
@@ -14,6 +16,7 @@ static void json_parse(State& state) {
1416
if (error) { return; }
1517
}
1618
}
19+
SIMDJSON_POP_DISABLE_WARNINGS
1720
BENCHMARK(json_parse);
1821
static void parser_parse_error_code(State& state) {
1922
dom::parser parser;
@@ -37,12 +40,15 @@ static void parser_parse_exception(State& state) {
3740
}
3841
BENCHMARK(parser_parse_exception);
3942

43+
SIMDJSON_PUSH_DISABLE_WARNINGS
44+
SIMDJSON_DISABLE_DEPRECATED_WARNING
4045
static void build_parsed_json(State& state) {
4146
for (auto _ : state) {
4247
dom::parser parser = simdjson::build_parsed_json(EMPTY_ARRAY);
4348
if (!parser.valid) { return; }
4449
}
4550
}
51+
SIMDJSON_POP_DISABLE_WARNINGS
4652
BENCHMARK(build_parsed_json);
4753
static void document_parse_error_code(State& state) {
4854
for (auto _ : state) {

benchmark/distinctuseridcompetition.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ void print_vec(const std::vector<int64_t> &v) {
3939

4040
// simdjson_recurse below come be implemented like so but it is slow:
4141
/*void simdjson_recurse(std::vector<int64_t> & v, simdjson::dom::element element) {
42-
if (element.is_array()) {
43-
auto [array, array_error] = element.as_array();
42+
if (element.is<simdjson::dom::array>()) {
43+
auto [array, array_error] = element.get<simdjson::dom::array>();
4444
for (auto child : array) {
45-
if (child.is_array() || child.is_object()) {
45+
if (child.is<simdjson::dom::array>() || child.is<simdjson::dom::object>()) {
4646
simdjson_recurse(v, child);
4747
}
4848
}
49-
} else if (element.is_object()) {
50-
auto [object, error] = element.as_object();
49+
} else if (element.is<simdjson::dom::object>()) {
50+
auto [object, error] = element.get<simdjson::dom::object>();
5151
int64_t id;
52-
object["user"]["id"].as_int64_t().tie(id,error);
52+
object["user"]["id"].get<int64_t>().tie(id,error);
5353
if(!error) {
5454
v.push_back(id);
5555
}
5656
for (auto [key, value] : object) {
57-
if (value.is_array() || value.is_object()) {
57+
if (value.is<simdjson::dom::array>() || value.is<simdjson::dom::object>()) {
5858
simdjson_recurse(v, value);
5959
}
6060
}
@@ -64,35 +64,35 @@ void print_vec(const std::vector<int64_t> &v) {
6464

6565

6666
void simdjson_recurse(std::vector<int64_t> & v, simdjson::dom::element element) {
67-
if (element.is_array()) {
68-
auto array = element.as_array();
67+
if (element.is<simdjson::dom::array>()) {
68+
auto array = element.get<simdjson::dom::array>();
6969
for (auto child : array) {
70-
if (child.is_array() || child.is_object()) {
70+
if (child.is<simdjson::dom::array>() || child.is<simdjson::dom::object>()) {
7171
simdjson_recurse(v, child);
7272
}
7373
}
74-
} else if (element.is_object()) {
75-
auto object = element.as_object();
74+
} else if (element.is<simdjson::dom::object>()) {
75+
auto object = element.get<simdjson::dom::object>();
7676
for (auto [key, value] : object) {
7777
if((key.size() == 4) && (memcmp(key.data(), "user", 4) == 0)) {
7878
// we are in an object under the key "user"
79-
if(value.is_object()) {
80-
auto child_object = value.as_object();
79+
if(value.is<simdjson::dom::object>()) {
80+
auto child_object = value.get<simdjson::dom::object>();
8181
for (auto [child_key, child_value] : child_object) {
8282
if((child_key.size() == 2) && (memcmp(child_key.data(), "id", 2) == 0)) {
83-
if(child_value.is_integer()) {
84-
v.push_back(child_value.as_int64_t());
83+
if(child_value.is<int64_t>()) {
84+
v.push_back(child_value.get<int64_t>());
8585
}
8686
}
87-
if (child_value.is_array() || child_value.is_object()) {
87+
if (child_value.is<simdjson::dom::array>() || child_value.is<simdjson::dom::object>()) {
8888
simdjson_recurse(v, child_value);
8989
}
9090
}
91-
} else if (value.is_array()) {
91+
} else if (value.is<simdjson::dom::array>()) {
9292
simdjson_recurse(v, value);
9393
}
9494
// end of: we are in an object under the key "user"
95-
} else if (value.is_array() || value.is_object()) {
95+
} else if (value.is<simdjson::dom::array>() || value.is<simdjson::dom::object>()) {
9696
simdjson_recurse(v, value);
9797
}
9898
}

benchmark/parseandstatcompetition.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ void print_stat(const stat_t &s) {
5151

5252
really_inline void simdjson_process_atom(stat_t &s,
5353
simdjson::dom::element element) {
54-
if (element.is_number()) {
54+
if (element.is<double>()) {
5555
s.number_count++;
56-
} else if (element.is_bool()) {
57-
if (element.as_bool()) {
56+
} else if (element.is<bool>()) {
57+
if (element.get<bool>()) {
5858
s.true_count++;
5959
} else {
6060
s.false_count++;
@@ -65,21 +65,21 @@ really_inline void simdjson_process_atom(stat_t &s,
6565
}
6666

6767
void simdjson_recurse(stat_t &s, simdjson::dom::element element) {
68-
if (element.is_array()) {
68+
if (element.is<simdjson::dom::array>()) {
6969
s.array_count++;
70-
auto [array, array_error] = element.as_array();
70+
auto [array, array_error] = element.get<simdjson::dom::array>();
7171
for (auto child : array) {
72-
if (child.is_array() || child.is_object()) {
72+
if (child.is<simdjson::dom::array>() || child.is<simdjson::dom::object>()) {
7373
simdjson_recurse(s, child);
7474
} else {
7575
simdjson_process_atom(s, child);
7676
}
7777
}
78-
} else if (element.is_object()) {
78+
} else if (element.is<simdjson::dom::object>()) {
7979
s.object_count++;
80-
auto [object, object_error] = element.as_object();
80+
auto [object, object_error] = element.get<simdjson::dom::object>();
8181
for (auto [key, value] : object) {
82-
if (value.is_array() || value.is_object()) {
82+
if (value.is<simdjson::dom::array>() || value.is<simdjson::dom::object>()) {
8383
simdjson_recurse(s, value);
8484
} else {
8585
simdjson_process_atom(s, value);

benchmark/parsingcompetition.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ bool bench(const char *filename, bool verbose, bool just_data, int repeat_multip
109109
}
110110

111111
if (!just_data)
112-
BEST_TIME("simdjson (dynamic mem) ", !simdjson::dom::parser().parse(p).error(), true,
112+
BEST_TIME("simdjson (dynamic mem) ", simdjson::dom::parser().parse(p).error(), simdjson::SUCCESS,
113113
, repeat, volume, !just_data);
114114
// (static alloc)
115115
simdjson::dom::parser parser;
@@ -197,14 +197,14 @@ bool bench(const char *filename, bool verbose, bool just_data, int repeat_multip
197197
{
198198
std::unique_ptr<jsmntok_t[]> tokens =
199199
std::make_unique<jsmntok_t[]>(p.size());
200-
jsmn_parser parser;
201-
jsmn_init(&parser);
200+
jsmn_parser jparser;
201+
jsmn_init(&jparser);
202202
memcpy(buffer, p.data(), p.size());
203203
buffer[p.size()] = '\0';
204204
BEST_TIME(
205205
"jsmn ",
206-
(jsmn_parse(&parser, buffer, p.size(), tokens.get(), p.size()) > 0),
207-
true, jsmn_init(&parser), repeat, volume, !just_data);
206+
(jsmn_parse(&jparser, buffer, p.size(), tokens.get(), p.size()) > 0),
207+
true, jsmn_init(&jparser), repeat, volume, !just_data);
208208
}
209209
memcpy(buffer, p.data(), p.size());
210210
buffer[p.size()] = '\0';
@@ -244,8 +244,8 @@ bool bench(const char *filename, bool verbose, bool just_data, int repeat_multip
244244
std::fill(stats.begin(), stats.end(), 0); // unnecessary
245245
for (int i = 0; i < repeat; i++) {
246246
unified.start();
247-
auto [doc, error] = parser.parse(p);
248-
if (error)
247+
auto [doc, parse_error] = parser.parse(p);
248+
if (parse_error)
249249
printf("bug\n");
250250
unified.end(results);
251251
std::transform(stats.begin(), stats.end(), results.begin(), stats.begin(),

benchmark/statisticalmodel.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ using stat_t = struct stat_s;
4545

4646
really_inline void simdjson_process_atom(stat_t &s,
4747
simdjson::dom::element element) {
48-
if (element.is_integer()) {
48+
if (element.is<int64_t>()) {
4949
s.integer_count++;
50-
} else if(element.is_string()) {
50+
} else if(element.is<std::string_view>()) {
5151
s.string_count++;
52-
} else if(element.is_float()) {
52+
} else if(element.is<double>()) {
5353
s.float_count++;
54-
} else if (element.is_bool()) {
55-
if (element.as_bool()) {
54+
} else if (element.is<bool>()) {
55+
if (element.get<bool>()) {
5656
s.true_count++;
5757
} else {
5858
s.false_count++;
@@ -63,22 +63,22 @@ really_inline void simdjson_process_atom(stat_t &s,
6363
}
6464

6565
void simdjson_recurse(stat_t &s, simdjson::dom::element element) {
66-
if (element.is_array()) {
66+
if (element.is<simdjson::dom::array>()) {
6767
s.array_count++;
68-
auto [array, array_error] = element.as_array();
68+
auto [array, array_error] = element.get<simdjson::dom::array>();
6969
for (auto child : array) {
70-
if (child.is_array() || child.is_object()) {
70+
if (child.is<simdjson::dom::array>() || child.is<simdjson::dom::object>()) {
7171
simdjson_recurse(s, child);
7272
} else {
7373
simdjson_process_atom(s, child);
7474
}
7575
}
76-
} else if (element.is_object()) {
76+
} else if (element.is<simdjson::dom::object>()) {
7777
s.object_count++;
78-
auto [object, object_error] = element.as_object();
78+
auto [object, object_error] = element.get<simdjson::dom::object>();
7979
for (auto [key, value] : object) {
8080
s.string_count++; // for key
81-
if (value.is_array() || value.is_object()) {
81+
if (value.is<simdjson::dom::array>() || value.is<simdjson::dom::object>()) {
8282
simdjson_recurse(s, value);
8383
} else {
8484
simdjson_process_atom(s, value);

0 commit comments

Comments
 (0)