Skip to content

Commit 4a8d175

Browse files
committed
decoder: Use faster ondemand parser for simdjson_key_exists method
1 parent 92700cf commit 4a8d175

File tree

6 files changed

+20
-27
lines changed

6 files changed

+20
-27
lines changed

php_simdjson.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,10 @@ PHP_FUNCTION(simdjson_key_count) {
326326

327327
simdjson_php_error_code error;
328328
if (SIMDJSON_SHOULD_REUSE_PARSER(ZSTR_LEN(json))) {
329-
error = php_simdjson_key_count(simdjson_get_reused_parser(), json, ZSTR_VAL(key), return_value, depth);
329+
error = php_simdjson_key_count(simdjson_get_reused_parser(), json, ZSTR_VAL(key), return_value);
330330
} else {
331331
simdjson_php_parser *simdjson_php_parser = php_simdjson_create_parser();
332-
error = php_simdjson_key_count(simdjson_php_parser, json, ZSTR_VAL(key), return_value, depth);
332+
error = php_simdjson_key_count(simdjson_php_parser, json, ZSTR_VAL(key), return_value);
333333
php_simdjson_free_parser(simdjson_php_parser);
334334
}
335335

@@ -354,16 +354,12 @@ PHP_FUNCTION(simdjson_key_exists) {
354354
Z_PARAM_LONG(depth)
355355
ZEND_PARSE_PARAMETERS_END();
356356

357-
if (!simdjson_validate_depth(depth, 3)) {
358-
RETURN_THROWS();
359-
}
360-
361357
simdjson_php_error_code error;
362358
if (SIMDJSON_SHOULD_REUSE_PARSER(ZSTR_LEN(json))) {
363-
error = php_simdjson_key_exists(simdjson_get_reused_parser(), json, ZSTR_VAL(key), depth);
359+
error = php_simdjson_key_exists(simdjson_get_reused_parser(), json, ZSTR_VAL(key));
364360
} else {
365361
simdjson_php_parser *simdjson_php_parser = php_simdjson_create_parser();
366-
error = php_simdjson_key_exists(simdjson_php_parser, json, ZSTR_VAL(key), depth);
362+
error = php_simdjson_key_exists(simdjson_php_parser, json, ZSTR_VAL(key));
367363
php_simdjson_free_parser(simdjson_php_parser);
368364
}
369365

php_simdjson.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_key_value(struct simdjson_
206206
*
207207
* @see https://www.rfc-editor.org/rfc/rfc6901.html
208208
*/
209-
PHP_SIMDJSON_API uint8_t php_simdjson_key_exists(struct simdjson_php_parser* parser, const zend_string *json, const char *key, size_t depth);
209+
PHP_SIMDJSON_API uint8_t php_simdjson_key_exists(struct simdjson_php_parser* parser, const zend_string *json, const char *key);
210210
/**
211211
* Count the keys of the given array/object at json pointer 'key' exists in the given json string.
212212
*
@@ -217,7 +217,7 @@ PHP_SIMDJSON_API uint8_t php_simdjson_key_exists(struct simdjson_php_parser* par
217217
*
218218
* @see https://www.rfc-editor.org/rfc/rfc6901.html
219219
*/
220-
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_key_count(struct simdjson_php_parser* parser, const zend_string *json, const char *key, zval *return_value, size_t depth);
220+
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_key_count(struct simdjson_php_parser* parser, const zend_string *json, const char *key, zval *return_value);
221221

222222
END_EXTERN_C()
223223

simdjson.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,10 @@ function simdjson_key_count(string $json, string $key, int $depth = 512, bool $t
302302
*
303303
* @param string $json The JSON string being decoded
304304
* @param string $key The JSON pointer being requested
305-
* @param int $depth the maximum nesting depth of the structure being decoded.
305+
* @param int $depth Not used anymore
306306
* @return bool (false if key is not found)
307307
* @throws SimdJsonDecoderException for invalid JSON or invalid JSON pointer
308308
* (or document over 4GB, or out of range integer/float)
309-
* @throws ValueError for invalid $depth
310309
* @see https://www.rfc-editor.org/rfc/rfc6901.html
311310
*/
312311
function simdjson_key_exists(string $json, string $key, int $depth = 512): bool {}

simdjson_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: d30184e8da243c7681539c2f70cb0afbd97ca02d */
2+
* Stub hash: 1593e3c33138c464eaf0df92264ac9ee21bf0278 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_simdjson_validate, 0, 1, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)

src/simdjson_decoder.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ static zend_always_inline bool simdjson_realloc_needed(const zend_string *json)
156156
return free_space < simdjson::SIMDJSON_PADDING;
157157
}
158158

159-
static simdjson::padded_string_view simdjson_padded_string_view(const zend_string *json) {
159+
static simdjson::padded_string_view simdjson_padded_string_view(const zend_string *json, simdjson::padded_string &jsonbuffer) {
160160
if (simdjson_realloc_needed(json)) {
161-
return simdjson::padded_string(ZSTR_VAL(json), ZSTR_LEN(json));
161+
jsonbuffer = simdjson::padded_string(ZSTR_VAL(json), ZSTR_LEN(json));
162+
return jsonbuffer;
162163
} else {
163164
return simdjson::padded_string_view(ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_LEN(json) + simdjson::SIMDJSON_PADDING);
164165
}
@@ -659,18 +660,21 @@ PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_key_value(simdjson_php_par
659660

660661
/* }}} */
661662

662-
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_key_exists(simdjson_php_parser* parser, const zend_string *json, const char *key, size_t depth) /* {{{ */ {
663-
simdjson::dom::element doc;
664-
SIMDJSON_PHP_TRY(build_parsed_json_cust(parser, doc, ZSTR_VAL(json), ZSTR_LEN(json), simdjson_realloc_needed(json), depth));
665-
return get_key_with_optional_prefix(doc, key).error();
663+
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_key_exists(simdjson_php_parser* parser, const zend_string *json, const char *key) {
664+
simdjson::padded_string jsonbuffer;
665+
simdjson::ondemand::document doc;
666+
667+
SIMDJSON_PHP_TRY(parser->ondemand_parser.iterate(simdjson_padded_string_view(json, jsonbuffer)).get(doc));
668+
return get_key_with_optional_prefix_ondemand(doc, key).error();
666669
}
667670

668-
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_key_count(simdjson_php_parser* parser, const zend_string *json, const char *key, zval *return_value, size_t depth) {
671+
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_key_count(simdjson_php_parser* parser, const zend_string *json, const char *key, zval *return_value) {
672+
simdjson::padded_string jsonbuffer;
669673
simdjson::ondemand::document doc;
670674
simdjson::ondemand::value value;
671675
simdjson::ondemand::json_type type;
672676

673-
SIMDJSON_PHP_TRY(parser->ondemand_parser.iterate(simdjson_padded_string_view(json)).get(doc));
677+
SIMDJSON_PHP_TRY(parser->ondemand_parser.iterate(simdjson_padded_string_view(json, jsonbuffer)).get(doc));
674678
SIMDJSON_PHP_TRY(get_key_with_optional_prefix_ondemand(doc, key).get(value));
675679
SIMDJSON_PHP_TRY(value.type().get(type));
676680

tests/decode_max_depth.phpt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,24 @@ require_once __DIR__ . '/dump.inc';
1010
foreach ([0, PHP_INT_MIN, 1024, PHP_INT_MAX >> 1, PHP_INT_MAX] as $depth) {
1111
dump(function () use ($depth) { return simdjson_decode('[]', true, $depth); });
1212
dump(function () use ($depth) { return simdjson_key_value('{"a":{}}', 'a', true, $depth); });
13-
dump(function () use ($depth) { return simdjson_key_exists('{"a":{}}', 'a', $depth); });
1413
dump(function () use ($depth) { return simdjson_is_valid('{}', $depth); });
1514
}
1615
?>
1716
--EXPECTF--
1817
Caught ValueError: simdjson_decode(): Argument #3 ($depth) must be greater than zero
1918
Caught ValueError: simdjson_key_value(): Argument #4 ($depth) must be greater than zero
20-
Caught ValueError: simdjson_key_exists(): Argument #3 ($depth) must be greater than zero
2119
Caught ValueError: simdjson_is_valid(): Argument #2 ($depth) must be greater than zero
2220
Caught ValueError: simdjson_decode(): Argument #3 ($depth) must be greater than zero
2321
Caught ValueError: simdjson_key_value(): Argument #4 ($depth) must be greater than zero
24-
Caught ValueError: simdjson_key_exists(): Argument #3 ($depth) must be greater than zero
2522
Caught ValueError: simdjson_is_valid(): Argument #2 ($depth) must be greater than zero
2623
array(0) {
2724
}
2825
array(0) {
2926
}
3027
bool(true)
31-
bool(true)
3228
Caught ValueError: simdjson_decode(): Argument #3 ($depth) exceeds maximum allowed value of %d
3329
Caught ValueError: simdjson_key_value(): Argument #4 ($depth) exceeds maximum allowed value of %d
34-
Caught ValueError: simdjson_key_exists(): Argument #3 ($depth) exceeds maximum allowed value of %d
3530
Caught ValueError: simdjson_is_valid(): Argument #2 ($depth) exceeds maximum allowed value of %d
3631
Caught ValueError: simdjson_decode(): Argument #3 ($depth) exceeds maximum allowed value of %d
3732
Caught ValueError: simdjson_key_value(): Argument #4 ($depth) exceeds maximum allowed value of %d
38-
Caught ValueError: simdjson_key_exists(): Argument #3 ($depth) exceeds maximum allowed value of %d
3933
Caught ValueError: simdjson_is_valid(): Argument #2 ($depth) exceeds maximum allowed value of %d

0 commit comments

Comments
 (0)