simdjson_wrapper.cpp

filter: Statementstmt 80.2% 398 / 496 Branchbran 56.2% 224 / 398 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.5% 622 / 894 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« simdjson.cpp ↑ Summary simdjson.h »
1 #include <iostream>
2 #include <mutex>
3 #include "simdjson.h"
4 #define PERL_NO_GET_CONTEXT
5 #include "simdjson_wrapper.h"
6 #include "ppport.h"
7  
8 using namespace simdjson;
9  
10 // global simdjson parser instance to be used in for all decodes
11 // (we initialize this at module load time and keep it around because
12 // initializing the parser is expensive)
13 static ondemand::parser *global_parser_instance;
14 // we protect it with a mutex, because using Perl's thread-local storage
15 // would be slower - at the cost of making this module effectively single-threaded
16 static std::mutex global_mutex;
17  
18 // 256 errors ought to be enough for anybody - Bill Gates
19 #define CUSTOM_ERROR_BASE 256
20  
21 #define CUSTOM_ERROR_LIST \
22 ENTRY(0, CERR_PATH_IN_SCALAR, "only the empty path is allowed for scalar documents") \
23 ENTRY(1, CERR_NUMBER_LEADING_ZERO, "malformed number (leading zero must not be followed by another digit)") \
24 ENTRY(2, CERR_NUMBER_INITIAL_MINUS, "malformed number (no digits after initial minus)") \
25 ENTRY(3, CERR_NUMBER_DECIMAL, "malformed number (no digits after decimal point)") \
26 ENTRY(4, CERR_NUMBER_EXP, "malformed number (no digits after exp sign)")
27  
28 enum {
29 #define ENTRY(a,b,c) b = a + CUSTOM_ERROR_BASE,
30 CUSTOM_ERROR_LIST
31 #undef ENTRY
32 NUM_CUSTOM_ERRORS
33 };
34  
35 typedef struct {
36 int code;
37 const char *message;
38 } custom_error_message_t;
39  
40 const custom_error_message_t custom_error_messages[] {
41 #define ENTRY(a,b,c) {b, c},
42 CUSTOM_ERROR_LIST
43 #undef ENTRY
44 };
45  
46 118 static inline const char* custom_error_message(int code) {
47 118 ▶ if (code >= CUSTOM_ERROR_BASE && code < NUM_CUSTOM_ERRORS && custom_error_messages[code - CUSTOM_ERROR_BASE].code == code) {
Branch: 3 branches
BranchTrueFalseTotal
if (code >= CUSTOM_ERROR_BASE && code < NUM_CUSTOM_ERRORS && custom_error_messages[code - CUSTOM_ERROR_BASE].code == code) { 15 103 118
if (code >= CUSTOM_ERROR_BASE && code < NUM_CUSTOM_ERRORS && custom_error_messages[code - CUSTOM_ERROR_BASE].code == code) { 15 0 15
if (code >= CUSTOM_ERROR_BASE && code < NUM_CUSTOM_ERRORS && custom_error_messages[code - CUSTOM_ERROR_BASE].code == code) { 15 0 15
48 15 return custom_error_messages[code - CUSTOM_ERROR_BASE].message;
49 103 ▶ } else if (code < NUM_ERROR_CODES) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (code < NUM_ERROR_CODES) { 103 0 103
50 103 return error_message((simdjson::error_code) code);
51 }
52 0 return "Something went wrong, you should never see this message";
53 }
54  
55 // nonsense required because raw_json_token() behaves differently for values and documents
56 28 static std::string_view get_raw_json_token_from(ondemand::document& doc) {
57 28 std::string_view str;
58 28 if (doc.raw_json_token().get(str)) { /* error ignored */ }
59 28 return str;
60 }
61  
62 6 static std::string_view get_raw_json_token_from(ondemand::value val) {
63 6 return val.raw_json_token();
64 }
65  
66 // Check if a string encodes a number, that is,
67 // it matches /[+-]?[1-9][0-9]*\.?[0-9]*(?:[eE][+-]?[0-9]+)?/, clumsily and slowly.
68 // Returns an error code, or 0 if it appears to be a valid number.
69 // This should be a rare special case.
70 29 static inline int validate_large_number(std::string_view& s) {
71 29 ▶ if (s.size() == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (s.size() == 0) 0 29 29
72 0 return (int)NUMBER_ERROR;
73  
74 29 unsigned long i = 0;
75 29 ▶ if (s[0] == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] == '-') 8 21 29
76 8 i = 1;
77  
78 29 bool got_decimal = false;
79 29 bool got_exp = false;
80  
81 29 ▶ if (s[0] == '-' && (1 == s.size() || ! isdigit(s[i])))
Branch: 4 branches
BranchTrueFalseTotal
if (s[0] == '-' && (1 == s.size() || ! isdigit(s[i]))) 8 21 29
if (s[0] == '-' && (1 == s.size() || ! isdigit(s[i]))) 8 0 8
if (s[0] == '-' && (1 == s.size() || ! isdigit(s[i]))) 4 4 8
if (s[0] == '-' && (1 == s.size() || ! isdigit(s[i]))) 4 25 29
82 4 return CERR_NUMBER_INITIAL_MINUS;
83 25 ▶ if (s[i] == '0' && (i+1 == s.size() || isdigit(s[i+1])))
Branch: 4 branches
BranchTrueFalseTotal
if (s[i] == '0' && (i+1 == s.size() || isdigit(s[i+1]))) 13 12 25
if (s[i] == '0' && (i+1 == s.size() || isdigit(s[i+1]))) 13 0 13
if (s[i] == '0' && (i+1 == s.size() || isdigit(s[i+1]))) 5 8 13
if (s[i] == '0' && (i+1 == s.size() || isdigit(s[i+1]))) 5 20 25
84 5 return CERR_NUMBER_LEADING_ZERO;
85  
86 57 ▶ for (; i < s.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < s.size(); i++) { 57 0 57
87 57 ▶ if ( !( isdigit(s[i]) || (!got_decimal && s[i] == '.') || (!got_exp && (s[i] == 'e' || s[i] == 'E') ) ) )
Branch: 7 branches
BranchTrueFalseTotal
if ( !( isdigit(s[i]) || (!got_decimal && s[i] == '.') || (!got_exp && (s[i] == 'e' || s[i] == 'E') ) ) ) 27 30 57
if ( !( isdigit(s[i]) || (!got_decimal && s[i] == '.') || (!got_exp && (s[i] == 'e' || s[i] == 'E') ) ) ) 20 7 27
if ( !( isdigit(s[i]) || (!got_decimal && s[i] == '.') || (!got_exp && (s[i] == 'e' || s[i] == 'E') ) ) ) 10 10 20
if ( !( isdigit(s[i]) || (!got_decimal && s[i] == '.') || (!got_exp && (s[i] == 'e' || s[i] == 'E') ) ) ) 16 1 17
if ( !( isdigit(s[i]) || (!got_decimal && s[i] == '.') || (!got_exp && (s[i] == 'e' || s[i] == 'E') ) ) ) 5 11 16
if ( !( isdigit(s[i]) || (!got_decimal && s[i] == '.') || (!got_exp && (s[i] == 'e' || s[i] == 'E') ) ) ) 5 0 5
if ( !( isdigit(s[i]) || (!got_decimal && s[i] == '.') || (!got_exp && (s[i] == 'e' || s[i] == 'E') ) ) ) 6 51 57
88 6 return NUMBER_ERROR;
89 51 ▶ if (s[i] == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '.') { 10 41 51
90 10 got_decimal = true;
91 10 ▶ if (i+1 == s.length())
Branch: 1 branch
BranchTrueFalseTotal
if (i+1 == s.length()) 2 8 10
92 2 return CERR_NUMBER_DECIMAL;
93 // peek ahead for illegal exponent after dot
94 8 ▶ if (s[i+1] == 'e' || s[i+1] == 'E')
Branch: 3 branches
BranchTrueFalseTotal
if (s[i+1] == 'e' || s[i+1] == 'E') 6 2 8
if (s[i+1] == 'e' || s[i+1] == 'E') 0 6 6
if (s[i+1] == 'e' || s[i+1] == 'E') 2 6 8
95 2 return CERR_NUMBER_DECIMAL;
96 }
97 47 ▶ if (s[i] == 'e' || s[i] == 'E') {
Branch: 3 branches
BranchTrueFalseTotal
if (s[i] == 'e' || s[i] == 'E') { 36 11 47
if (s[i] == 'e' || s[i] == 'E') { 0 36 36
if (s[i] == 'e' || s[i] == 'E') { 11 36 47
98 11 got_exp = true;
99 11 got_decimal = true; // dot also not allowed after exponent
100 11 ▶ if (i+1 == s.length())
Branch: 1 branch
BranchTrueFalseTotal
if (i+1 == s.length()) 5 6 11
101 5 return CERR_NUMBER_EXP;
102 // peek ahead and consume exponent sign if present
103 6 ▶ if (s[i+1] == '-' || s[i+1] == '+') {
Branch: 3 branches
BranchTrueFalseTotal
if (s[i+1] == '-' || s[i+1] == '+') { 4 2 6
if (s[i+1] == '-' || s[i+1] == '+') { 4 0 4
if (s[i+1] == '-' || s[i+1] == '+') { 6 0 6
104 6 i++;
105 6 ▶ if (i+1 == s.length())
Branch: 1 branch
BranchTrueFalseTotal
if (i+1 == s.length()) 5 1 6
106 5 return CERR_NUMBER_INITIAL_MINUS;
107 }
108 }
109 }
110 0 return 0;
111 }
112  
113 // taken from simdutf8
114 1101 static inline bool validate_ascii(const char *buf, size_t len) noexcept {
115 1101 const uint8_t *data = reinterpret_cast<const uint8_t *>(buf);
116 1101 uint64_t pos = 0;
117 // process in blocks of 16 bytes when possible
118 1109 ▶ for (;pos + 16 < len; pos += 16) {
Branch: 1 branch
BranchTrueFalseTotal
for (;pos + 16 < len; pos += 16) { 10 1099 1109
119 uint64_t v1;
120 10 std::memcpy(&v1, data + pos, sizeof(uint64_t));
121 uint64_t v2;
122 10 std::memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
123 10 uint64_t v{v1 | v2};
124 10 ▶ if ((v & 0x8080808080808080) != 0) { return false; }
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0x8080808080808080) != 0) { return false; } 2 8 10
125 }
126 // process the tail byte-by-byte
127 1972 ▶ for (;pos < len; pos ++) {
Branch: 1 branch
BranchTrueFalseTotal
for (;pos < len; pos ++) { 873 1099 1972
128 873 ▶ if (data[pos] >= 0b10000000) { return false; }
Branch: 1 branch
BranchTrueFalseTotal
if (data[pos] >= 0b10000000) { return false; } 0 873 873
129 }
130 1099 return true;
131 }
132  
133 #define DEC_INC_DEPTH \
134 do { \
135 if (++dec->depth > dec->json.max_depth) { \
136 err = DEPTH_ERROR; \
137 --dec->depth; \
138 } \
139 } while (0)
140  
141 #define DEC_DEC_DEPTH --dec->depth
142  
143 #define ERROR_RETURN \
144 do { \
145 if (simdjson_unlikely(err)) { \
146 dec->error_code = err; \
147 dec->error_line_number = __LINE__; \
148 return NULL; \
149 } \
150 } while (0)
151  
152 #define ERROR_RETURN_IF(expected_err) \
153 do { \
154 if (simdjson_unlikely(err == expected_err)) { \
155 dec->error_code = err; \
156 dec->error_line_number = __LINE__; \
157 return NULL; \
158 } \
159 } while (0)
160  
161 #define ERROR_RETURN_CLEANUP(var) \
162 do { \
163 if (simdjson_unlikely(err)) { \
164 dec->error_code = err; \
165 dec->error_line_number = __LINE__; \
166 SvREFCNT_dec (var); \
167 return NULL; \
168 } \
169 } while (0)
170  
171 #define NULL_RETURN_CLEANUP(sv, var) \
172 do { \
173 if (simdjson_unlikely(!sv)) { \
174 SvREFCNT_dec (var); \
175 return NULL; \
176 } \
177 } while (0)
178  
179 // There is some special kind of perversion going on that both C++'s advanced template magic
180 // and threaded Perl's THX_ voodoo are present, yet the whole thing still works.
181 template<typename T>
182 66063 static SV* recursive_parse_json(pTHX_ dec_t *dec, T element) {
183 66063 SV* res = NULL;
184  
185 ondemand::json_type t;
186 66063 auto err = element.type().get(t);
187 66063 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 0 65875 65875
ERROR_RETURN; 0 188 188
188  
189 66063 switch (t) {
190 0 case ondemand::json_type::array:
191 {
192 0 ▶ DEC_INC_DEPTH;
Branch: 2 branches
BranchTrueFalseTotal
DEC_INC_DEPTH; 3 32248 32251
DEC_INC_DEPTH; 0 0 0
193 0 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 3 32248 32251
ERROR_RETURN; 0 0 0
194  
195 0 ▶ AV *av = newAV();
Branch: 2 branches
BranchTrueFalseTotal
AV *av = newAV(); 32248 0 32248
AV *av = newAV(); 0 0 0
196  
197 0 ▶ for (auto child : element.get_array()) {
Branch: 2 branches
BranchTrueFalseTotal
for (auto child : element.get_array()) { 33010 31687 64697
for (auto child : element.get_array()) { 0 0 0
198 0 ondemand::value val;
199 0 auto err = child.get(val);
200 0 ▶ ERROR_RETURN_CLEANUP(av);
Branch: 4 branches
BranchTrueFalseTotal
ERROR_RETURN_CLEANUP(av); 3 33007 33010
ERROR_RETURN_CLEANUP(av); 3 0 3
ERROR_RETURN_CLEANUP(av); 0 0 0
ERROR_RETURN_CLEANUP(av); 0 0 0
201  
202 0 ▶ SV *elem = recursive_parse_json(aTHX_ dec, val);
Branch: 2 branches
BranchTrueFalseTotal
SV *elem = recursive_parse_json(aTHX_ dec, val); 33007 0 33007
SV *elem = recursive_parse_json(aTHX_ dec, val); 0 0 0
203 0 ▶ NULL_RETURN_CLEANUP(elem, av);
Branch: 4 branches
BranchTrueFalseTotal
NULL_RETURN_CLEANUP(elem, av); 558 32449 33007
NULL_RETURN_CLEANUP(elem, av); 558 0 558
NULL_RETURN_CLEANUP(elem, av); 0 0 0
NULL_RETURN_CLEANUP(elem, av); 0 0 0
204  
205 0 ▶ av_push(av, elem);
Branch: 2 branches
BranchTrueFalseTotal
av_push(av, elem); 32449 0 32449
av_push(av, elem); 0 0 0
206 }
207  
208 0 DEC_DEC_DEPTH;
209 0 ▶ res = newRV_noinc ((SV *)av);
Branch: 2 branches
BranchTrueFalseTotal
res = newRV_noinc ((SV *)av); 31687 0 31687
res = newRV_noinc ((SV *)av); 0 0 0
210 0 break;
211 }
212 0 case ondemand::json_type::object:
213 {
214 0 ▶ DEC_INC_DEPTH;
Branch: 2 branches
BranchTrueFalseTotal
DEC_INC_DEPTH; 0 1359 1359
DEC_INC_DEPTH; 0 0 0
215 0 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 0 1359 1359
ERROR_RETURN; 0 0 0
216  
217 0 ▶ HV *hv = newHV();
Branch: 2 branches
BranchTrueFalseTotal
HV *hv = newHV(); 1359 0 1359
HV *hv = newHV(); 0 0 0
218  
219 0 ▶ for (auto field : element.get_object()) {
Branch: 2 branches
BranchTrueFalseTotal
for (auto field : element.get_object()) { 1624 836 2460
for (auto field : element.get_object()) { 0 0 0
220 0 U32 flags = 0;
221 0 std::string_view key;
222 0 auto err = field.unescaped_key().get(key);
223 0 ▶ ERROR_RETURN_CLEANUP(hv);
Branch: 4 branches
BranchTrueFalseTotal
ERROR_RETURN_CLEANUP(hv); 7 1617 1624
ERROR_RETURN_CLEANUP(hv); 7 0 7
ERROR_RETURN_CLEANUP(hv); 0 0 0
ERROR_RETURN_CLEANUP(hv); 0 0 0
224  
225 0 ondemand::value val;
226 0 err = field.value().get(val);
227 0 ▶ ERROR_RETURN_CLEANUP(hv);
Branch: 4 branches
BranchTrueFalseTotal
ERROR_RETURN_CLEANUP(hv); 0 1617 1617
ERROR_RETURN_CLEANUP(hv); 0 0 0
ERROR_RETURN_CLEANUP(hv); 0 0 0
ERROR_RETURN_CLEANUP(hv); 0 0 0
228  
229 0 ▶ SV *sv_value = recursive_parse_json(aTHX_ dec, val);
Branch: 2 branches
BranchTrueFalseTotal
SV *sv_value = recursive_parse_json(aTHX_ dec, val); 1617 0 1617
SV *sv_value = recursive_parse_json(aTHX_ dec, val); 0 0 0
230 0 ▶ NULL_RETURN_CLEANUP(sv_value, hv);
Branch: 4 branches
BranchTrueFalseTotal
NULL_RETURN_CLEANUP(sv_value, hv); 516 1101 1617
NULL_RETURN_CLEANUP(sv_value, hv); 516 0 516
NULL_RETURN_CLEANUP(sv_value, hv); 0 0 0
NULL_RETURN_CLEANUP(sv_value, hv); 0 0 0
231  
232 // simdjson always returns the key as an UTF-8-encoded string
233 // (it has already checked and unescaped it).
234 // However, for Perl hash keys we have to specify whether they are UTF-8.
235 // If yes, we have to supply the appropriate flag.
236 // This is necessary to avoid double-encoded mojibake keys.
237 // The downside: UTF-8 keys are terribly slow, they are reallocated,
238 // scanned and downgraded in a very inefficient way.
239 // Always passing the key as UTF-8 would in fact eat all the speedups we would gain by using simdjson.
240 // Most real-life hash keys are expected to be short ASCII strings,
241 // so we try to salvage the situation by scanning the key for non-ASCII characters
242 // and pass the key as UTF-8 only when necessary.
243 0 flags = HVhek_UTF8 * !validate_ascii(key.data(), key.size());
244 0 ▶ hv_common(hv, NULL, key.data(), key.size(), flags, HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, sv_value, 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_common(hv, NULL, key.data(), key.size(), flags, HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, sv_value, 0); 1101 0 1101
hv_common(hv, NULL, key.data(), key.size(), flags, HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, sv_value, 0); 0 0 0
245 }
246  
247 0 DEC_DEC_DEPTH;
248 0 ▶ res = newRV_noinc ((SV *)hv);
Branch: 2 branches
BranchTrueFalseTotal
res = newRV_noinc ((SV *)hv); 836 0 836
res = newRV_noinc ((SV *)hv); 0 0 0
249  
250 0 break;
251 }
252 849 case ondemand::json_type::number:
253 {
254 849 ondemand::number num;
255 849 int err = element.get_number().get(num);
256 849 ▶ if (simdjson_unlikely(err)) {
Branch: 2 branches
BranchTrueFalseTotal
if (simdjson_unlikely(err)) { 5 759 764
if (simdjson_unlikely(err)) { 30 55 85
257 // for scalar documents it can detect trailing content
258 64 ▶ ERROR_RETURN_IF(TRAILING_CONTENT);
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN_IF(TRAILING_CONTENT); 0 5 5
ERROR_RETURN_IF(TRAILING_CONTENT); 6 24 30
259  
260 // simdjson 3.13+ natively handles big integers
261 // - or actually, it doesn't quite
262 29 ▶ if (err == BIGINT_ERROR) {
Branch: 2 branches
BranchTrueFalseTotal
if (err == BIGINT_ERROR) { 0 5 5
if (err == BIGINT_ERROR) { 0 24 24
263 // we save it as a string, and at this point simdjson has hopefully validated it for us
264 0 auto str = get_raw_json_token_from(element);
265 0 ▶ res = newSVpvn_utf8(str.data(), str.size(), 1);
Branch: 2 branches
BranchTrueFalseTotal
res = newSVpvn_utf8(str.data(), str.size(), 1); 0 0 0
res = newSVpvn_utf8(str.data(), str.size(), 1); 0 0 0
266 0 break;
267 }
268  
269 // old way of handling case of large numbers:
270 // we save it as a string, but try to validate if it looks like a number at least
271 // (and if it is a small but invalid number, get a more precise error code)
272 29 auto str = get_raw_json_token_from(element);
273 29 err = validate_large_number(str);
274 29 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 5 0 5
ERROR_RETURN; 24 0 24
275  
276 0 ▶ res = newSVpvn_utf8(str.data(), str.size(), 1);
Branch: 2 branches
BranchTrueFalseTotal
res = newSVpvn_utf8(str.data(), str.size(), 1); 0 0 0
res = newSVpvn_utf8(str.data(), str.size(), 1); 0 0 0
277 0 break;
278 }
279  
280 814 ondemand::number_type nt = num.get_number_type();
281 814 switch (nt) {
282 68 case ondemand::number_type::floating_point_number:
283 {
284 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
285 // simdjson only gets us IEEE-754 doubles, which is fine if Perl's
286 // NV happens to be a double (which is the most common case),
287 // but we'd lose precision if Perl was compiled with a larger numeric type.
288 // So in this case we fall back to the slower but tried and true
289 // legacy number parser.
290 auto str = get_raw_json_token_from(element);
291 const char *s = str.data();
292 NV nv = json_atof(s);
293 res = newSVnv(nv);
294 #else
295 68 double d = 0.0;
296 68 auto err = element.get_double().get(d);
297 68 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 0 29 29
ERROR_RETURN; 0 39 39
298  
299 68 ▶ res = newSVnv((NV) d);
Branch: 2 branches
BranchTrueFalseTotal
res = newSVnv((NV) d); 29 0 29
res = newSVnv((NV) d); 39 0 39
300 #endif
301  
302 68 break;
303 }
304 746 case ondemand::number_type::signed_integer:
305 {
306 746 int64_t i = 0;
307 746 auto err = element.get_int64().get(i);
308 746 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 0 730 730
ERROR_RETURN; 0 16 16
309  
310 746 ▶ res = newSViv((IV)i);
Branch: 2 branches
BranchTrueFalseTotal
res = newSViv((IV)i); 730 0 730
res = newSViv((IV)i); 16 0 16
311 746 break;
312 }
313 0 case ondemand::number_type::unsigned_integer:
314 {
315 0 uint64_t u = 0;
316 0 auto err = element.get_uint64().get(u);
317 0 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 0 0 0
ERROR_RETURN; 0 0 0
318  
319 0 ▶ res = newSVuv((UV)u);
Branch: 2 branches
BranchTrueFalseTotal
res = newSVuv((UV)u); 0 0 0
res = newSVuv((UV)u); 0 0 0
320 0 break;
321 }
322 0 case ondemand::number_type::big_integer:
323 {
324 // XXX we never get here
325 // handle case of large numbers:
326 // we save it as a string, and at this point simdjson has hopefully validated it for us
327 0 auto str = get_raw_json_token_from(element);
328 0 ▶ res = newSVpvn_utf8(str.data(), str.size(), 1);
Branch: 2 branches
BranchTrueFalseTotal
res = newSVpvn_utf8(str.data(), str.size(), 1); 0 0 0
res = newSVpvn_utf8(str.data(), str.size(), 1); 0 0 0
329 0 break;
330 }
331 }
332 814 break;
333 }
334 31475 case ondemand::json_type::string:
335 {
336 31475 std::string_view str;
337 31475 auto err = element.get_string().get(str);
338 31475 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 3 31418 31421
ERROR_RETURN; 11 43 54
339  
340 31461 ▶ res = newSVpvn_utf8(str.data(), str.size(), 1);
Branch: 2 branches
BranchTrueFalseTotal
res = newSVpvn_utf8(str.data(), str.size(), 1); 31418 0 31418
res = newSVpvn_utf8(str.data(), str.size(), 1); 43 0 43
341 31461 break;
342 }
343 51 case ondemand::json_type::boolean:
344 {
345 51 bool b = false;
346 51 auto err = element.get_bool().get(b);
347 51 ▶ if (err) {
Branch: 2 branches
BranchTrueFalseTotal
if (err) { 1 30 31
if (err) { 8 12 20
348 // for scalar documents it can detect trailing content
349 14 ▶ ERROR_RETURN_IF(TRAILING_CONTENT);
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN_IF(TRAILING_CONTENT); 0 1 1
ERROR_RETURN_IF(TRAILING_CONTENT); 4 4 8
350  
351 // try to forge a more informative error message
352 5 auto str = get_raw_json_token_from(element);
353 5 ▶ if (str.size() && str[0]) {
Branch: 6 branches
BranchTrueFalseTotal
if (str.size() && str[0]) { 1 0 1
if (str.size() && str[0]) { 1 0 1
if (str.size() && str[0]) { 1 0 1
if (str.size() && str[0]) { 4 0 4
if (str.size() && str[0]) { 4 0 4
if (str.size() && str[0]) { 4 0 4
354 5 ▶ err = (str[0] == 't') ? T_ATOM_ERROR : (str[0] == 'f') ? F_ATOM_ERROR : err;
Branch: 4 branches
BranchTrueFalseTotal
err = (str[0] == 't') ? T_ATOM_ERROR : (str[0] == 'f') ? F_ATOM_ERROR : err; 0 1 1
err = (str[0] == 't') ? T_ATOM_ERROR : (str[0] == 'f') ? F_ATOM_ERROR : err; 0 0 0
err = (str[0] == 't') ? T_ATOM_ERROR : (str[0] == 'f') ? F_ATOM_ERROR : err; 2 2 4
err = (str[0] == 't') ? T_ATOM_ERROR : (str[0] == 'f') ? F_ATOM_ERROR : err; 0 2 2
355 }
356 5 ▶ ERROR_RETURN;
Branch: 2 branches
BranchTrueFalseTotal
ERROR_RETURN; 1 0 1
ERROR_RETURN; 4 0 4
357 }
358  
359 42 ▶ res = newSVsv(b ? dec->json.v_true : dec->json.v_false);
Branch: 4 branches
BranchTrueFalseTotal
res = newSVsv(b ? dec->json.v_true : dec->json.v_false); 16 14 30
res = newSVsv(b ? dec->json.v_true : dec->json.v_false); 30 0 30
res = newSVsv(b ? dec->json.v_true : dec->json.v_false); 6 6 12
res = newSVsv(b ? dec->json.v_true : dec->json.v_false); 12 0 12
360 42 break;
361 }
362 52 case ondemand::json_type::null:
363 {
364 bool is_null;
365 52 auto err = element.is_null().get(is_null);
366 52 ▶ if(simdjson_unlikely(!is_null || err)) {
Branch: 6 branches
BranchTrueFalseTotal
if(simdjson_unlikely(!is_null || err)) { 40 0 40
if(simdjson_unlikely(!is_null || err)) { 0 40 40
if(simdjson_unlikely(!is_null || err)) { 0 40 40
if(simdjson_unlikely(!is_null || err)) { 6 6 12
if(simdjson_unlikely(!is_null || err)) { 0 6 6
if(simdjson_unlikely(!is_null || err)) { 6 6 12
367 // we falsify the error, it would be either nothing or INCORRECT_TYPE, which is less informative
368 0 ▶ if (err == 0 || err == INCORRECT_TYPE) {
Branch: 4 branches
BranchTrueFalseTotal
if (err == 0 || err == INCORRECT_TYPE) { 0 0 0
if (err == 0 || err == INCORRECT_TYPE) { 0 0 0
if (err == 0 || err == INCORRECT_TYPE) { 6 0 6
if (err == 0 || err == INCORRECT_TYPE) { 4 2 6
369 0 dec->error_code = N_ATOM_ERROR;
370 } else {
371 // for scalar documents it can detect trailing content
372 0 dec->error_code = err;
373 }
374 0 dec->error_line_number = __LINE__;
375 0 return NULL;
376 }
377 46 ▶ res = newSVsv (&PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
res = newSVsv (&PL_sv_undef); 40 0 40
res = newSVsv (&PL_sv_undef); 6 0 6
378 46 break;
379 }
380 26 case ondemand::json_type::unknown:
381 {
382 // do we ever want to report anything else from here?
383 26 dec->error_code = TAPE_ERROR;
384 26 dec->error_line_number = __LINE__;
385 26 return NULL;
386 }
387 }
388 64886 return res;
389 }
390  
391 // Try to find the end of a potentially valid scalar value that is followed by trailing garbage, clumsily and slowly.
392 // This should be a rare special case.
393 50 static size_t find_end_of_scalar(char *s) {
394 50 ▶ if (!s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s) { 0 50 50
395 0 return 0;
396 }
397 50 char *start = s;
398 50 ▶ while (*s == 0x20 || *s == 0x0d ||*s == 0x0a ||*s == 0x09) {
Branch: 4 branches
BranchTrueFalseTotal
while (*s == 0x20 || *s == 0x0d ||*s == 0x0a ||*s == 0x09) { 0 50 50
while (*s == 0x20 || *s == 0x0d ||*s == 0x0a ||*s == 0x09) { 0 50 50
while (*s == 0x20 || *s == 0x0d ||*s == 0x0a ||*s == 0x09) { 0 50 50
while (*s == 0x20 || *s == 0x0d ||*s == 0x0a ||*s == 0x09) { 0 50 50
399 0 s++;
400 }
401  
402 50 bool in_quotes = false;
403 50 bool in_escape = false;
404 312 ▶ while (*s) {
Branch: 1 branch
BranchTrueFalseTotal
while (*s) { 280 32 312
405 280 switch (*s) {
406 5 case '\\':
407 5 in_escape = !in_escape;
408 5 break;
409 16 case '"':
410 16 ▶ if (in_escape) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_escape) { 0 16 16
411 0 in_escape = false;
412 } else {
413 16 in_quotes = !in_quotes;
414 }
415 16 break;
416 17 case 0x20:
417 case 0x0d:
418 case 0x0a:
419 case 0x09:
420 case '}':
421 case '{':
422 case '[':
423 case ']':
424 17 ▶ if (!in_quotes) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in_quotes) { 17 0 17
425 17 return s - start;
426 }
427 0 break;
428 242 default:
429 242 ▶ if (in_escape) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_escape) { 5 237 242
430 5 in_escape = false;
431 }
432 242 ▶ if (*s < 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if (*s < 0x20) { 1 241 242
433 1 return s - start;
434 }
435 }
436 262 s++;
437 }
438 32 return s - start;
439 }
440  
441 31340 static char *get_location(ondemand::document& doc) {
442 31340 const char *location = NULL;
443 31340 auto err = doc.current_location().get(location);
444 31340 ▶ if (err) { // out of bounds, i.e. end of document
Branch: 1 branch
BranchTrueFalseTotal
if (err) { // out of bounds, i.e. end of document 31260 80 31340
445 31260 return NULL;
446 } else {
447 80 return const_cast<char*>(location);
448 }
449 }
450  
451 31427 static void save_errormsg_location(dec_t *dec, char *location) {
452 31427 ▶ if (dec->error_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->error_code) { 118 31309 31427
453 118 dec->err = custom_error_message(dec->error_code);
454 }
455 31427 ▶ if (location) {
Branch: 1 branch
BranchTrueFalseTotal
if (location) { 138 31289 31427
456 138 dec->cur = location;
457 } else {
458 31289 dec->cur = SvEND(dec->input);
459 }
460 // std::cerr << "DEBUG error " << dec->err << " at line " << dec->error_line_number << " near " << dec->cur << std::endl;
461  
462 31427 dec->end = SvEND(dec->input);
463 31427 }
464  
465 45 void simdjson_global_init() {
466 45 global_parser_instance = new ondemand::parser;
467 45 }
468  
469 #define ERROR_RETURN_SAVE_MSG \
470 do { \
471 if (simdjson_unlikely(err)) { \
472 dec->error_code = err; \
473 dec->error_line_number = __LINE__; \
474 save_errormsg_location(dec, location); \
475 return NULL; \
476 } \
477 } while (0)
478  
479  
480 31427 SV * simdjson_decode(dec_t *dec) {
481 31427 SV *sv = NULL;
482  
483 dTHX;
484  
485 31427 ▶ SvGROW(dec->input, SvCUR (dec->input) + SIMDJSON_PADDING);
Branch: 3 branches
BranchTrueFalseTotal
SvGROW(dec->input, SvCUR (dec->input) + SIMDJSON_PADDING); 31427 0 31427
SvGROW(dec->input, SvCUR (dec->input) + SIMDJSON_PADDING); 17781 13646 31427
SvGROW(dec->input, SvCUR (dec->input) + SIMDJSON_PADDING); 17781 0 17781
486  
487 31427 ▶ std::lock_guard<std::mutex> guard(global_mutex);
Branch: 1 branch
BranchTrueFalseTotal
std::lock_guard<std::mutex> guard(global_mutex); 31427 0 31427
488 31427 ondemand::parser* parser = global_parser_instance;
489 31427 char *location = NULL;
490 31427 bool is_scalar = false;
491 31427 bool problem_retry = false;
492  
493 {
494 // doc will be destroyed at the end of the scope, so that we can call parser->iterate again
495 // if we have to retry (to catch certain error cases)
496 31427 ondemand::document doc;
497  
498 31427 auto err = parser->iterate(SvPVX(dec->input), SvCUR(dec->input), SvLEN(dec->input)).get(doc);
499 31446 ▶ ERROR_RETURN_SAVE_MSG;
Branch: 1 branch
BranchTrueFalseTotal
ERROR_RETURN_SAVE_MSG; 10 31417 31427
500  
501 31417 err = doc.is_scalar().get(is_scalar);
502 31417 ▶ ERROR_RETURN_SAVE_MSG;
Branch: 1 branch
BranchTrueFalseTotal
ERROR_RETURN_SAVE_MSG; 0 31417 31417
503  
504 31417 ▶ if (simdjson_unlikely(is_scalar)) {
Branch: 1 branch
BranchTrueFalseTotal
if (simdjson_unlikely(is_scalar)) { 141 31276 31417
505 141 ▶ if (dec->path && *(dec->path)) {
Branch: 2 branches
BranchTrueFalseTotal
if (dec->path && *(dec->path)) { 4 137 141
if (dec->path && *(dec->path)) { 2 2 4
506 // don't parse anything, non-root path doesn't make sense with scalar values
507 2 dec->error_code = CERR_PATH_IN_SCALAR;
508 2 dec->error_line_number = __LINE__;
509 2 ▶ ERROR_RETURN_SAVE_MSG;
Branch: 1 branch
BranchTrueFalseTotal
ERROR_RETURN_SAVE_MSG; 0 2 2
510 } else {
511 139 ▶ sv = recursive_parse_json<ondemand::document&>(aTHX_ dec, doc);
Branch: 1 branch
BranchTrueFalseTotal
sv = recursive_parse_json<ondemand::document&>(aTHX_ dec, doc); 139 0 139
512  
513 139 ▶ if (simdjson_unlikely(dec->error_code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (simdjson_unlikely(dec->error_code)) { 50 89 139
514 50 problem_retry = true;
515 } else {
516 89 location = get_location(doc);
517 }
518 }
519 } else {
520 31276 ondemand::value val;
521 31276 ▶ if (simdjson_unlikely(dec->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (simdjson_unlikely(dec->path)) { 39 31237 31276
522 39 err = doc.at_pointer(dec->path).get(val);
523 } else {
524 31237 err = doc.get_value().get(val);
525 }
526 31276 ▶ if (simdjson_unlikely(err == INCOMPLETE_ARRAY_OR_OBJECT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (simdjson_unlikely(err == INCOMPLETE_ARRAY_OR_OBJECT)) { 31 31245 31276
527 31 problem_retry = 1;
528 } else {
529 31245 ▶ ERROR_RETURN_SAVE_MSG;
Branch: 1 branch
BranchTrueFalseTotal
ERROR_RETURN_SAVE_MSG; 19 31226 31245
530  
531 31226 ▶ sv = recursive_parse_json<ondemand::value>(aTHX_ dec, val);
Branch: 1 branch
BranchTrueFalseTotal
sv = recursive_parse_json<ondemand::value>(aTHX_ dec, val); 31226 0 31226
532 31226 location = get_location(doc);
533 }
534 }
535 }
536  
537 31398 ▶ if (simdjson_unlikely(problem_retry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (simdjson_unlikely(problem_retry)) { 81 31317 31398
538 81 ▶ if (is_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_scalar) { 50 31 81
539 // For scalar documents it can detect trailing content _most of the time_,
540 // but re-parsing the content with iterate_many doesn't work for some reason,
541 // and even if it worked, there are cases where iterate_many fails (e.g. '1111 }', it says empty json (as of simdjson 3.1.6).
542 // So we have to find the end of the valid content ourselves and re-parse a truncated document, yet another desperate hack.
543 50 ondemand::document doc2;
544 50 size_t size = find_end_of_scalar(SvPVX(dec->input));
545  
546 50 ▶ if (size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > 0) { 49 1 50
547 49 auto err = parser->iterate(SvPVX(dec->input), size, SvLEN(dec->input)).get(doc2);
548 49 ▶ ERROR_RETURN_SAVE_MSG;
Branch: 1 branch
BranchTrueFalseTotal
ERROR_RETURN_SAVE_MSG; 0 49 49
549 49 ▶ sv = recursive_parse_json<ondemand::document&>(aTHX_ dec, doc2);
Branch: 1 branch
BranchTrueFalseTotal
sv = recursive_parse_json<ondemand::document&>(aTHX_ dec, doc2); 49 0 49
550 }
551 50 location = SvPVX(dec->input) + size;
552 } else {
553 // desperate, gnarly hack:
554 // simdjson has fundamental limitations, because parse.iterate (in on demand mode)
555 // can not distinguish between an incomplete document and a valid document with trailing garbage.
556 // So try to re-parse with iterate_many to decide.
557 31 ondemand::document_stream stream;
558 31 auto err = parser->iterate_many(SvPVX(dec->input), SvCUR(dec->input), SvLEN(dec->input)).get(stream);
559 31 ▶ ERROR_RETURN_SAVE_MSG;
Branch: 1 branch
BranchTrueFalseTotal
ERROR_RETURN_SAVE_MSG; 0 31 31
560  
561 31 ondemand::document_reference doc_ref;
562 31 auto iter = stream.begin();
563 31 err = (*iter).get(doc_ref);
564 31 ▶ if (err == EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == EMPTY) { 6 25 31
565 // iterate_many hasn't found a valid document, so we go with truncated
566 6 err = INCOMPLETE_ARRAY_OR_OBJECT;
567 6 ▶ ERROR_RETURN_SAVE_MSG;
Branch: 1 branch
BranchTrueFalseTotal
ERROR_RETURN_SAVE_MSG; 6 0 6
568 }
569  
570 25 ondemand::value val;
571 25 ▶ if (dec->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->path) { 0 25 25
572 0 err = doc_ref.at_pointer(dec->path).get(val);
573 } else {
574 25 err = doc_ref.get_value().get(val);
575 }
576 25 ▶ ERROR_RETURN_SAVE_MSG;
Branch: 1 branch
BranchTrueFalseTotal
ERROR_RETURN_SAVE_MSG; 0 25 25
577  
578 25 ▶ sv = recursive_parse_json<ondemand::value>(aTHX_ dec, val);
Branch: 1 branch
BranchTrueFalseTotal
sv = recursive_parse_json<ondemand::value>(aTHX_ dec, val); 25 0 25
579 25 location = get_location(doc_ref);
580  
581 // doc_ref.current_location may be null
582 // (if we parsed a complete first document, and the trailing garbage looks like a (partial) valid document)
583 // so we need to ask the stream
584 25 ▶ if (location == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (location == NULL) { 8 17 25
585 8 location = SvEND(dec->input) - stream.truncated_bytes();
586 }
587 31 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 25 6 31
588 }
589  
590 31392 save_errormsg_location(dec, location);
591 31392 return sv;
592 31427 }
593  
594 0 SV * simdjson_get_version() {
595 dTHX;
596  
597 0 SV *version_info = newSVpvs("v" SIMDJSON_VERSION " ");
598 0 ▶ sv_catpv(version_info, simdjson::get_active_implementation()->name().c_str());
Branch: 3 branches
BranchTrueFalseTotal
sv_catpv(version_info, simdjson::get_active_implementation()->name().c_str()); 0 0 0
sv_catpv(version_info, simdjson::get_active_implementation()->name().c_str()); 0 0 0
sv_catpv(version_info, simdjson::get_active_implementation()->name().c_str()); 0 0 0
599 0 sv_catpv(version_info, "(");
600 0 ▶ sv_catpv(version_info, simdjson::get_active_implementation()->description().c_str());
Branch: 3 branches
BranchTrueFalseTotal
sv_catpv(version_info, simdjson::get_active_implementation()->description().c_str()); 0 0 0
sv_catpv(version_info, simdjson::get_active_implementation()->description().c_str()); 0 0 0
sv_catpv(version_info, simdjson::get_active_implementation()->description().c_str()); 0 0 0
601 0 sv_catpv(version_info, ")");
602 0 return version_info;
603 }
604  
« simdjson.cpp ↑ Summary simdjson.h »