Skip to content

Commit eaf020b

Browse files
committed
encoder: Optimise encoding serializable object
1 parent a81d595 commit eaf020b

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

php_simdjson.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ PHP_MINIT_FUNCTION (simdjson) {
640640
#if PHP_VERSION_ID >= 80200
641641
SIMDJSON_NEW_INTERNED_STRING(simdjson_json_empty_array, "[]");
642642
#endif
643-
SIMDJSON_NEW_INTERNED_STRING(simdjson_json_serialize, "jsonSerialize");
643+
SIMDJSON_NEW_INTERNED_STRING(simdjson_json_serialize, "jsonserialize");
644644

645645
auto simdjson_exception_ce = register_class_SimdJsonException(spl_ce_RuntimeException);
646646
simdjson_decoder_exception_ce = register_class_SimdJsonDecoderException(simdjson_exception_ce);

src/simdjson_encoder.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ static void simdjson_encode_base64_object(smart_str *buf, const zval *val) {
725725

726726
static zend_result simdjson_encode_serializable_object(smart_str *buf, zval *val, simdjson_encoder *encoder) {
727727
zend_class_entry *ce = Z_OBJCE_P(val);
728-
zval retval, fname;
728+
zval retval;
729729
zend_result return_code;
730730

731731
#if PHP_VERSION_ID >= 80300
@@ -750,23 +750,11 @@ static zend_result simdjson_encode_serializable_object(smart_str *buf, zval *val
750750
SIMDJSON_HASH_PROTECT_RECURSION(myht);
751751
#endif
752752

753-
ZVAL_INTERNED_STR(&fname, simdjson_json_serialize); // jsonSerialize
754-
755-
if (FAILURE == call_user_function(NULL, val, &fname, &retval, 0, NULL) || Z_TYPE(retval) == IS_UNDEF) {
756-
if (!EG(exception)) {
757-
zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ZSTR_VAL(ce->name));
758-
}
759-
#if PHP_VERSION_ID >= 80300
760-
ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON);
761-
#else
762-
SIMDJSON_HASH_UNPROTECT_RECURSION(myht);
763-
#endif
764-
return FAILURE;
765-
}
766-
767-
if (EG(exception)) {
768-
/* Error already raised */
769-
zval_ptr_dtor(&retval);
753+
zend_function *json_serialize_method = (zend_function*)zend_hash_find_ex_ptr(&ce->function_table, simdjson_json_serialize, 1);
754+
ZEND_ASSERT(json_serialize_method != NULL && "This should be guaranteed prior to calling this function");
755+
zend_call_known_instance_method_with_0_params(json_serialize_method, Z_OBJ_P(val), &retval);
756+
/* An exception has occurred */
757+
if (Z_TYPE(retval) == IS_UNDEF) {
770758
#if PHP_VERSION_ID >= 80300
771759
ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON);
772760
#else
@@ -775,8 +763,8 @@ static zend_result simdjson_encode_serializable_object(smart_str *buf, zval *val
775763
return FAILURE;
776764
}
777765

778-
if ((Z_TYPE(retval) == IS_OBJECT) &&
779-
(Z_OBJ(retval) == Z_OBJ_P(val))) {
766+
/* An exception has occurred */
767+
if (Z_TYPE(retval) == IS_OBJECT && Z_OBJ(retval) == Z_OBJ_P(val)) {
780768
/* Handle the case where jsonSerialize does: return $this; by going straight to encode array */
781769
#if PHP_VERSION_ID >= 80300
782770
ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON);

0 commit comments

Comments
 (0)