Skip to content

Commit 2115596

Browse files
committed
Compile performance.md examples in tests
1 parent 0e3453f commit 2115596

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

doc/performance.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ without bound:
7777
7878
```c++
7979
dom::parser parser(0); // This parser will refuse to automatically grow capacity
80-
parser.set_capacity(1024*1024); // This allocates enough capacity to handle documents <= 1MB
80+
simdjson::error_code allocate_error = parser.set_capacity(1024*1024); // This allocates enough capacity to handle documents <= 1MB
81+
if (allocate_error) { cerr << allocate_error << endl; exit(1); }
82+
8183
for (web_request request : listen()) {
8284
auto [doc, error] = parser.parse(request.body);
8385
// If the document was above our limit, emit 413 = payload too large

tests/readme_examples.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,49 @@ void implementation_selection_4() {
9393
simdjson::active_implementation = simdjson::available_implementations["fallback"];
9494
}
9595

96+
void performance_1() {
97+
dom::parser parser;
98+
99+
// This initializes buffers and a document big enough to handle this JSON.
100+
dom::element doc = parser.parse("[ true, false ]"_padded);
101+
cout << doc << endl;
102+
103+
// This reuses the existing buffers, and reuses and *overwrites* the old document
104+
doc = parser.parse("[1, 2, 3]"_padded);
105+
cout << doc << endl;
106+
107+
// This also reuses the existing buffers, and reuses and *overwrites* the old document
108+
dom::element doc2 = parser.parse("true"_padded);
109+
// Even if you keep the old reference around, doc and doc2 refer to the same document.
110+
cout << doc << endl;
111+
cout << doc2 << endl;
112+
}
113+
114+
// The web_request part of this is aspirational, so we compile as much as we can here
115+
void performance_2() {
116+
dom::parser parser(1024*1024); // Never grow past documents > 1MB
117+
// for (web_request request : listen()) {
118+
auto [doc, error] = parser.parse("1"_padded/*request.body*/);
119+
// // If the document was above our limit, emit 413 = payload too large
120+
if (error == CAPACITY) { /* request.respond(413); continue; */ }
121+
// // ...
122+
// }
123+
}
124+
125+
// The web_request part of this is aspirational, so we compile as much as we can here
126+
void performance_3() {
127+
dom::parser parser(0); // This parser will refuse to automatically grow capacity
128+
simdjson::error_code allocate_error = parser.set_capacity(1024*1024); // This allocates enough capacity to handle documents <= 1MB
129+
if (allocate_error) { cerr << allocate_error << endl; exit(1); }
130+
131+
// for (web_request request : listen()) {
132+
auto [doc, error] = parser.parse("1"_padded/*request.body*/);
133+
// If the document was above our limit, emit 413 = payload too large
134+
if (error == CAPACITY) { /* request.respond(413); continue; */ }
135+
// ...
136+
// }
137+
}
138+
96139
int main() {
97140
return 0;
98141
}

0 commit comments

Comments
 (0)