Skip to content

Commit 4e82ead

Browse files
committed
encoder: jsonSerialize as interned string
1 parent c051b33 commit 4e82ead

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

php_simdjson.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ PHP_SIMDJSON_API zend_class_entry *simdjson_base64_encode_ce;
4747
#include "simdjson_arginfo.h"
4848

4949
static zend_string *simdjson_json_empty_array;
50+
zend_string *simdjson_json_serialize;
5051

5152
ZEND_DECLARE_MODULE_GLOBALS(simdjson);
5253

@@ -617,19 +618,23 @@ PHP_METHOD(SimdJsonBase64Encode, jsonSerialize) {
617618
*/
618619
PHP_GINIT_FUNCTION (simdjson) {
619620
#if defined(COMPILE_DL_SIMDJSON) && defined(ZTS)
620-
ZEND_TSRMLS_CACHE_UPDATE();
621+
ZEND_TSRMLS_CACHE_UPDATE();
621622
#endif
622623
}
623624
/* }}} */
624625

626+
#define SIMDJSON_NEW_INTERNED_STRING(_dest, _string) do { \
627+
_dest = zend_string_init_interned(_string, strlen(_string), 1); \
628+
GC_ADD_FLAGS(_dest, IS_STR_VALID_UTF8); \
629+
} while(0); \
630+
625631
/** {{{ PHP_MINIT_FUNCTION
626632
*/
627633
PHP_MINIT_FUNCTION (simdjson) {
628634
#if PHP_VERSION_ID >= 80200
629-
// Interned string for empty array
630-
simdjson_json_empty_array = zend_new_interned_string(zend_string_init("[]", 2, 1));
631-
GC_ADD_FLAGS(simdjson_json_empty_array, IS_STR_VALID_UTF8);
635+
SIMDJSON_NEW_INTERNED_STRING(simdjson_json_empty_array, "[]");
632636
#endif
637+
SIMDJSON_NEW_INTERNED_STRING(simdjson_json_serialize, "jsonSerialize");
633638

634639
auto simdjson_exception_ce = register_class_SimdJsonException(spl_ce_RuntimeException);
635640
simdjson_decoder_exception_ce = register_class_SimdJsonDecoderException(simdjson_exception_ce);

php_simdjson.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ extern PHP_SIMDJSON_API zend_class_entry *simdjson_decoder_exception_ce;
117117
extern PHP_SIMDJSON_API zend_class_entry *simdjson_encoder_exception_ce;
118118
extern PHP_SIMDJSON_API zend_class_entry *simdjson_base64_encode_ce;
119119

120+
extern zend_string *simdjson_json_serialize;
121+
120122
/**
121123
* NOTE: Namespaces and references(&) and classes (instead of structs) are C++ only functionality.
122124
*

src/simdjson_encoder.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,13 +748,12 @@ static zend_result simdjson_encode_serializable_object(smart_str *buf, zval *val
748748
SIMDJSON_HASH_PROTECT_RECURSION(myht);
749749
#endif
750750

751-
ZVAL_STRING(&fname, "jsonSerialize");
751+
ZVAL_INTERNED_STR(&fname, simdjson_json_serialize); // jsonSerialize
752752

753753
if (FAILURE == call_user_function(NULL, val, &fname, &retval, 0, NULL) || Z_TYPE(retval) == IS_UNDEF) {
754754
if (!EG(exception)) {
755755
zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ZSTR_VAL(ce->name));
756756
}
757-
zval_ptr_dtor(&fname);
758757
#if PHP_VERSION_ID >= 80300
759758
ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON);
760759
#else
@@ -766,7 +765,6 @@ static zend_result simdjson_encode_serializable_object(smart_str *buf, zval *val
766765
if (EG(exception)) {
767766
/* Error already raised */
768767
zval_ptr_dtor(&retval);
769-
zval_ptr_dtor(&fname);
770768
#if PHP_VERSION_ID >= 80300
771769
ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON);
772770
#else
@@ -795,7 +793,6 @@ static zend_result simdjson_encode_serializable_object(smart_str *buf, zval *val
795793
}
796794

797795
zval_ptr_dtor(&retval);
798-
zval_ptr_dtor(&fname);
799796

800797
return return_code;
801798
}

0 commit comments

Comments
 (0)