Skip to content

Use new ZPP and inline caches in ReflectionProperty::getValue() and variants #17698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 76 additions & 21 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ PHPAPI zend_class_entry *reflection_property_hook_type_ptr;
typedef struct _property_reference {
zend_property_info *prop;
zend_string *unmangled_name;
void *cache_slot[3];
} property_reference;

/* Struct for parameters */
Expand Down Expand Up @@ -1506,6 +1507,7 @@ static void reflection_property_factory(zend_class_entry *ce, zend_string *name,
reference = (property_reference*) emalloc(sizeof(property_reference));
reference->prop = prop;
reference->unmangled_name = zend_string_copy(name);
memset(reference->cache_slot, 0, sizeof(reference->cache_slot));
intern->ptr = reference;
intern->ref_type = REF_TYPE_PROPERTY;
intern->ce = ce;
Expand Down Expand Up @@ -5643,6 +5645,7 @@ ZEND_METHOD(ReflectionProperty, __construct)
reference = (property_reference*) emalloc(sizeof(property_reference));
reference->prop = dynam_prop ? NULL : property_info;
reference->unmangled_name = zend_string_copy(name);
memset(reference->cache_slot, 0, sizeof(reference->cache_slot));
intern->ptr = reference;
intern->ref_type = REF_TYPE_PROPERTY;
intern->ce = ce;
Expand Down Expand Up @@ -5794,9 +5797,10 @@ ZEND_METHOD(ReflectionProperty, getValue)
zval *object = NULL;
zval *member_p = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &object) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_OBJECT_EX(object, 1, 0)
ZEND_PARSE_PARAMETERS_END();

GET_REFLECTION_OBJECT_PTR(ref);

Expand All @@ -5819,7 +5823,23 @@ ZEND_METHOD(ReflectionProperty, getValue)
RETURN_THROWS();
}

member_p = zend_read_property_ex(intern->ce, Z_OBJ_P(object), ref->unmangled_name, 0, &rv);
if (ref->cache_slot[0] == Z_OBJCE_P(object)) {
uintptr_t prop_offset = (uintptr_t) ref->cache_slot[1];

if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
zval *retval = OBJ_PROP(Z_OBJ_P(object), prop_offset);
if (EXPECTED(Z_TYPE_INFO_P(retval) != IS_UNDEF)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it actually matters, but I'm curious why you didn't just do Z_ISUNDEF_P and instead go through the type info

Copy link
Member Author

@arnaud-lb arnaud-lb Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy of the happy path of ZEND_FETCH_OBJ_R here:

php-src/Zend/zend_vm_def.h

Lines 2073 to 2076 in 3967b7a

if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
ZEND_VM_C_LABEL(fetch_obj_r_simple):
retval = OBJ_PROP(zobj, prop_offset);
if (EXPECTED(Z_TYPE_INFO_P(retval) != IS_UNDEF)) {

I think the original code uses Z_TYPE_INFO_P() rather than Z_TYPE_P()/Z_ISUNDEF_P() because type_info is accessed again just after. Here it's not the case, and Z_TYPE_INFO_P() != IS_UNDEF generates similar code as !Z_ISUNDEF_P(), so I could switch to Z_ISUNDEF_P().

RETURN_COPY_DEREF(retval);
}
}
}

zend_class_entry *old_scope = EG(fake_scope);
EG(fake_scope) = intern->ce;
member_p = Z_OBJ_P(object)->handlers->read_property(Z_OBJ_P(object),
ref->unmangled_name, BP_VAR_R, ref->cache_slot, &rv);
EG(fake_scope) = old_scope;

if (member_p != &rv) {
RETURN_COPY_DEREF(member_p);
} else {
Expand Down Expand Up @@ -5873,7 +5893,10 @@ ZEND_METHOD(ReflectionProperty, setValue)
Z_PARAM_ZVAL(value)
ZEND_PARSE_PARAMETERS_END();

zend_update_property_ex(intern->ce, object, ref->unmangled_name, value);
zend_class_entry *old_scope = EG(fake_scope);
EG(fake_scope) = intern->ce;
object->handlers->write_property(object, ref->unmangled_name, value, ref->cache_slot);
EG(fake_scope) = old_scope;
}
}
/* }}} */
Expand All @@ -5897,9 +5920,9 @@ ZEND_METHOD(ReflectionProperty, getRawValue)
property_reference *ref;
zval *object;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &object) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT(object)
ZEND_PARSE_PARAMETERS_END();

GET_REFLECTION_OBJECT_PTR(ref);

Expand All @@ -5908,6 +5931,17 @@ ZEND_METHOD(ReflectionProperty, getRawValue)
RETURN_THROWS();
}

if (ref->cache_slot[0] == Z_OBJCE_P(object)) {
uintptr_t prop_offset = (uintptr_t) ref->cache_slot[1];

if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
zval *retval = OBJ_PROP(Z_OBJ_P(object), prop_offset);
if (EXPECTED(Z_TYPE_INFO_P(retval) != IS_UNDEF)) {
RETURN_COPY_DEREF(retval);
}
}
}

zend_property_info *prop = reflection_property_get_effective_prop(ref,
intern->ce, Z_OBJ_P(object));

Expand All @@ -5918,7 +5952,12 @@ ZEND_METHOD(ReflectionProperty, getRawValue)

if (!prop || !prop->hooks || !prop->hooks[ZEND_PROPERTY_HOOK_GET]) {
zval rv;
zval *member_p = zend_read_property_ex(intern->ce, Z_OBJ_P(object), ref->unmangled_name, 0, &rv);
zend_class_entry *old_scope = EG(fake_scope);
EG(fake_scope) = intern->ce;
zval *member_p = Z_OBJ_P(object)->handlers->read_property(
Z_OBJ_P(object), ref->unmangled_name, BP_VAR_R,
ref->cache_slot, &rv);
EG(fake_scope) = old_scope;

if (member_p != &rv) {
RETURN_COPY_DEREF(member_p);
Expand All @@ -5935,11 +5974,14 @@ ZEND_METHOD(ReflectionProperty, getRawValue)
}

static void reflection_property_set_raw_value(zend_property_info *prop,
zend_string *unmangled_name, reflection_object *intern,
zend_string *unmangled_name, void *cache_slot[3], reflection_object *intern,
zend_object *object, zval *value)
{
if (!prop || !prop->hooks || !prop->hooks[ZEND_PROPERTY_HOOK_SET]) {
zend_update_property_ex(intern->ce, object, unmangled_name, value);
zend_class_entry *old_scope = EG(fake_scope);
EG(fake_scope) = intern->ce;
object->handlers->write_property(object, unmangled_name, value, cache_slot);
EG(fake_scope) = old_scope;
} else {
zend_function *func = zend_get_property_hook_trampoline(prop, ZEND_PROPERTY_HOOK_SET, unmangled_name);
zend_call_known_instance_method_with_1_params(func, object, NULL, value);
Expand All @@ -5955,9 +5997,10 @@ ZEND_METHOD(ReflectionProperty, setRawValue)

GET_REFLECTION_OBJECT_PTR(ref);

if (zend_parse_parameters(ZEND_NUM_ARGS(), "oz", &object, &value) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(2, 2) {
Z_PARAM_OBJECT(object)
Z_PARAM_ZVAL(value)
} ZEND_PARSE_PARAMETERS_END();

zend_property_info *prop = reflection_property_get_effective_prop(ref,
intern->ce, Z_OBJ_P(object));
Expand All @@ -5967,7 +6010,8 @@ ZEND_METHOD(ReflectionProperty, setRawValue)
RETURN_THROWS();
}

reflection_property_set_raw_value(prop, ref->unmangled_name, intern, Z_OBJ_P(object), value);
reflection_property_set_raw_value(prop, ref->unmangled_name,
ref->cache_slot, intern, Z_OBJ_P(object), value);
}

static zend_result reflection_property_check_lazy_compatible(
Expand Down Expand Up @@ -6046,8 +6090,8 @@ ZEND_METHOD(ReflectionProperty, setRawValueWithoutLazyInitialization)
/* Do not trigger initialization */
Z_PROP_FLAG_P(var_ptr) &= ~IS_PROP_LAZY;

reflection_property_set_raw_value(prop, ref->unmangled_name, intern, object,
value);
reflection_property_set_raw_value(prop, ref->unmangled_name,
ref->cache_slot, intern, object, value);

/* Mark property as lazy again if an exception prevented update */
if (EG(exception) && prop_was_lazy && Z_TYPE_P(var_ptr) == IS_UNDEF
Expand Down Expand Up @@ -6143,9 +6187,10 @@ ZEND_METHOD(ReflectionProperty, isInitialized)
zval *object = NULL;
zval *member_p = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &object) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_OBJECT_EX(object, 1, 0)
ZEND_PARSE_PARAMETERS_END();

GET_REFLECTION_OBJECT_PTR(ref);

Expand All @@ -6170,9 +6215,19 @@ ZEND_METHOD(ReflectionProperty, isInitialized)
RETURN_THROWS();
}

if (ref->cache_slot[0] == Z_OBJCE_P(object)) {
uintptr_t prop_offset = (uintptr_t) ref->cache_slot[1];

if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
zval *value = OBJ_PROP(Z_OBJ_P(object), prop_offset);
RETURN_BOOL(Z_TYPE_INFO_P(value) != IS_UNDEF);
}
}

old_scope = EG(fake_scope);
EG(fake_scope) = intern->ce;
retval = Z_OBJ_HT_P(object)->has_property(Z_OBJ_P(object), ref->unmangled_name, ZEND_PROPERTY_EXISTS, NULL);
retval = Z_OBJ_HT_P(object)->has_property(Z_OBJ_P(object),
ref->unmangled_name, ZEND_PROPERTY_EXISTS, ref->cache_slot);
EG(fake_scope) = old_scope;

RETVAL_BOOL(retval);
Expand Down
Loading