Skip to content

Commit 49d216f

Browse files
committed
small correctness updates
1 parent 76d46e1 commit 49d216f

208 files changed

Lines changed: 200 additions & 61 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ build-iPhoneSimulator/
5959
.cache
6060
build_config.rb.lock
6161
compile_commands.json
62-
findings
62+
findings
63+
benchmark/bench_simdjson

benchmark/simdjson_bench.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include <simdjson.h>
2+
#include <chrono>
3+
#include <iostream>
4+
#include <string>
5+
6+
using Clock = std::chrono::high_resolution_clock;
7+
8+
static void print_header(const std::string &title) {
9+
std::cout << std::string(100, '=') << "\n";
10+
std::cout << title << "\n";
11+
std::cout << std::string(100, '=') << "\n\n";
12+
}
13+
14+
static void print_object(simdjson::ondemand::object obj) {
15+
std::cout << "{";
16+
bool first = true;
17+
for (auto field : obj) {
18+
if (!first) std::cout << ", ";
19+
first = false;
20+
21+
std::string_view key = field.unescaped_key();
22+
simdjson::ondemand::value val = field.value();
23+
24+
std::cout << "\"" << key << "\" => ";
25+
26+
// Print values similar to Ruby's inspect
27+
switch (val.type()) {
28+
case simdjson::ondemand::json_type::number:
29+
std::cout << double(val);
30+
break;
31+
case simdjson::ondemand::json_type::string:
32+
std::cout << "\"" << std::string(val.get_string().value()) << "\"";
33+
break;
34+
case simdjson::ondemand::json_type::boolean:
35+
std::cout << (bool(val) ? "true" : "false");
36+
break;
37+
case simdjson::ondemand::json_type::null:
38+
std::cout << "nil";
39+
break;
40+
default:
41+
std::cout << "<complex>";
42+
}
43+
}
44+
std::cout << "}";
45+
}
46+
47+
int main(int argc, char **argv) {
48+
const char *filename = "twitter.json";
49+
if (argc > 1) filename = argv[1];
50+
51+
simdjson::ondemand::parser ondemand_parser;
52+
simdjson::dom::parser dom_parser;
53+
54+
// Load file
55+
simdjson::padded_string json = simdjson::padded_string::load(filename);
56+
57+
std::cout << "File: " << filename << " (" << json.size() << " bytes)\n\n";
58+
59+
// EAGER DECODE
60+
print_header("EAGER DECODE BENCHMARK (twitter.json)");
61+
62+
{
63+
std::cout << "simdjson DOM parse\n";
64+
auto start = Clock::now();
65+
66+
simdjson::dom::element doc;
67+
dom_parser.parse(json).get(doc);
68+
69+
auto end = Clock::now();
70+
double elapsed = std::chrono::duration<double>(end - start).count();
71+
double ops = 1.0 / elapsed;
72+
73+
std::cout << " Time: " << elapsed << " sec\n";
74+
std::cout << " OPS: " << ops << "\n\n";
75+
}
76+
77+
// LAZY DECODE
78+
print_header("LAZY DECODE BENCHMARK — /search_metadata");
79+
80+
{
81+
std::cout << "simdjson On-Demand\n";
82+
auto start = Clock::now();
83+
84+
simdjson::ondemand::document doc = ondemand_parser.iterate(json);
85+
simdjson::ondemand::object root = doc.get_object();
86+
simdjson::ondemand::object sm = root["search_metadata"];
87+
88+
auto end = Clock::now();
89+
double elapsed = std::chrono::duration<double>(end - start).count();
90+
double ops = 1.0 / elapsed;
91+
92+
std::cout << " Result: ";
93+
print_object(sm);
94+
std::cout << "\n";
95+
96+
std::cout << " Time: " << elapsed << " sec\n";
97+
std::cout << " OPS: " << ops << "\n\n";
98+
}
99+
100+
{
101+
std::cout << "simdjson DOM (parse + /search_metadata)\n";
102+
auto start = Clock::now();
103+
104+
simdjson::dom::element doc;
105+
dom_parser.parse(json).get(doc);
106+
107+
simdjson::dom::object root = doc.get_object();
108+
simdjson::dom::object sm = root["search_metadata"];
109+
110+
auto end = Clock::now();
111+
double elapsed = std::chrono::duration<double>(end - start).count();
112+
double ops = 1.0 / elapsed;
113+
114+
std::cout << " Result: {";
115+
bool first = true;
116+
for (auto field : sm) {
117+
if (!first) std::cout << ", ";
118+
first = false;
119+
std::cout << "\"" << std::string(field.key) << "\" => " << field.value;
120+
}
121+
std::cout << "}\n";
122+
123+
std::cout << " Time: " << elapsed << " sec\n";
124+
std::cout << " OPS: " << ops << "\n\n";
125+
}
126+
127+
std::cout << std::string(100, '=') << "\n";
128+
std::cout << "Done.\n";
129+
std::cout << std::string(100, '=') << "\n";
130+
131+
return 0;
132+
}

build_config.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def for_windows?
99
#conf.cxx.flags << '-fno-omit-frame-pointer' << '-g3' << '-ggdb3' << '-Og'
1010
#conf.cc.flags << '-fno-omit-frame-pointer' << '-g3' << '-ggdb3' << '-Og'
1111
#conf.enable_debug
12-
conf.cc.defines << 'MRB_UTF8_STRING'
13-
conf.cxx.defines << 'MRB_UTF8_STRING'
12+
conf.cc.defines << 'MRB_UTF8_STRING' << 'MRB_HIGH_PROFILE'
13+
conf.cxx.defines << 'MRB_UTF8_STRING' << 'MRB_HIGH_PROFILE'
1414
conf.enable_test
1515
conf.gembox 'default'
1616
conf.cc.flags << '-O3' << '-march=native'

fuzz/build_config.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
conf.enable_debug
77
conf.enable_bintest
8-
conf.cc.defines << 'MRB_UTF8_STRING' << 'MRB_PROFILE_HIGH'
9-
conf.cxx.defines << 'MRB_UTF8_STRING' << 'MRB_PROFILE_HIGH'
8+
conf.cc.defines << 'MRB_UTF8_STRING' << 'MRB_HIGH_PROFILE'
9+
conf.cxx.defines << 'MRB_UTF8_STRING' << 'MRB_HIGH_PROFILE'
1010
conf.gem '../'
1111
conf.gem File.expand_path(__dir__)
1212
end

fuzz/corpus/0186ef0a8de65336266f1cfe5931314edafa65ff

Lines changed: 0 additions & 1 deletion
This file was deleted.
-46 Bytes
Binary file not shown.

fuzz/corpus/05bcfd21f2874f6ac323567af8a9b8683c375602

Lines changed: 0 additions & 1 deletion
This file was deleted.
-10 Bytes
Binary file not shown.
-33 Bytes
Binary file not shown.
-19 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)