@@ -93,6 +93,49 @@ void implementation_selection_4() {
93
93
simdjson::active_implementation = simdjson::available_implementations[" fallback" ];
94
94
}
95
95
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
+
96
139
int main () {
97
140
return 0 ;
98
141
}
0 commit comments