Skip to content

Commit

Permalink
Allocate array to known size
Browse files Browse the repository at this point in the history
This avoids multiple reallocation when adding new elements to array
  • Loading branch information
JakubOnderka committed Nov 26, 2024
1 parent 9a27456 commit 3ba7c40
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/simdjson_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ extern "C" {
#define zend_string_release_ex(s, persistent) zend_string_release((s))
#endif

#ifndef ZVAL_EMPTY_ARRAY
#define ZVAL_EMPTY_ARRAY(value) array_init(value)
#endif

#define SIMDJSON_DEPTH_CHECK_THRESHOLD 100000

PHP_SIMDJSON_API const char* php_simdjson_error_msg(simdjson_php_error_code error)
Expand Down Expand Up @@ -54,6 +58,15 @@ get_key_with_optional_prefix(simdjson::dom::element &doc, std::string_view json_
return doc.at_pointer(std_pointer);
}

/** Init packed array with expected size */
static zend_always_inline zend_array* simdjson_init_packed_array(zval *zv, uint32_t size) {
zend_array *arr;
array_init_size(zv, size);
arr = Z_ARR_P(zv);
zend_hash_real_init_packed(arr);
return arr;
}

static simdjson::error_code
build_parsed_json_cust(simdjson_php_parser* parser, simdjson::dom::element &doc, const char *buf, size_t len, bool realloc_if_needed,
size_t depth = simdjson::DEFAULT_MAX_DEPTH) {
Expand Down Expand Up @@ -163,16 +176,13 @@ static simdjson_php_error_code create_array(simdjson::dom::element element, zval
break;
case simdjson::dom::element_type::ARRAY : {
const auto json_array = element.get_array().value_unsafe();
#if PHP_VERSION_ID >= 70300
if (json_array.size() == 0) {
/* Reuse the immutable empty array to save memory */
ZVAL_EMPTY_ARRAY(return_value);
break;
}
#endif
zend_array *arr;
array_init(return_value);
arr = Z_ARR_P(return_value);

zend_array *arr = simdjson_init_packed_array(return_value, json_array.size());

for (simdjson::dom::element child : json_array) {
zval array_element;
Expand All @@ -189,15 +199,14 @@ static simdjson_php_error_code create_array(simdjson::dom::element element, zval
}
case simdjson::dom::element_type::OBJECT : {
const auto json_object = element.get_object().value_unsafe();
#if PHP_VERSION_ID >= 70300
if (json_object.size() == 0) {
/* Reuse the immutable empty array to save memory */
ZVAL_EMPTY_ARRAY(return_value);
break;
}
#endif

zend_array *arr;
array_init(return_value);
array_init_size(return_value, json_object.size());
arr = Z_ARR_P(return_value);

for (simdjson::dom::key_value_pair field : json_object) {
Expand Down Expand Up @@ -243,16 +252,13 @@ static simdjson_php_error_code create_object(simdjson::dom::element element, zva
break;
case simdjson::dom::element_type::ARRAY : {
const auto json_array = element.get_array().value_unsafe();
#if PHP_VERSION_ID >= 70300
if (json_array.size() == 0) {
/* Reuse the immutable empty array to save memory */
ZVAL_EMPTY_ARRAY(return_value);
return simdjson::SUCCESS;
}
#endif
zend_array *arr;
array_init(return_value);
arr = Z_ARR_P(return_value);

zend_array *arr = simdjson_init_packed_array(return_value, json_array.size());

for (simdjson::dom::element child : json_array) {
zval value;
Expand Down

0 comments on commit 3ba7c40

Please sign in to comment.