Skip to content

Added ZEND_ASSIGN_OBJ fast path for the first write to an uninitializ… - #25057

Draft
adapik wants to merge 1 commit into
php:masterfrom
adapik:opt-assign-obj-uninit-typed-prop-master
Draft

adapik wants to merge 1 commit into
php:masterfrom
adapik:opt-assign-obj-uninit-typed-prop-master

Conversation

@adapik

@adapik adapik commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Modern PHP code mostly initializes properties through constructor property promotion, often with readonly (DTOs, value objects, services). Every such property is a typed property without a default, so its first write always goes through zend_std_write_property().

This PR adds a fast path in ZEND_ASSIGN_OBJ for the case where the cache slot is hit, the property is IS_UNDEF with IS_PROP_UNINIT set (never unset()), and neither the property nor the object is lazy. A new zend_assign_to_uninit_typed_prop() verifies the type and stores the value.

Results (release build, callgrind over 200k constructor calls with promoted readonly properties):

  • Without opcache: −24% instructions, −25% time.
  • Opcache without JIT: −24% instructions, −26% time.

Two new tests cover coercion, union and class types, readonly, protected(set), hooks and lazy objects.

This PR is related to #24566 and relies on it: that fix makes sure a failed write doesn't leave the cache slot primed. Without it, this fast path would also let a first write bypass those checks. It would be best if this lands after #24566.

Follow-up to #22709.

@adapik

adapik commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Tests will be green after #24566 lands, i put this PR to draft, but it's ready for the review. I'll rebase later on.

@ArtUkrainskiy ArtUkrainskiy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two things to fix before merge, see the line comments. With both changes the speedup stays: 8 promoted readonly properties take 397 cycles per new against 628 without the PR (GCC 15, opcache, no JIT). The two repros could go into the tests, the current ones pass without the PR too. Under tracing JIT this path isn't used, so maybe mention that in the description.

Comment thread Zend/zend_execute.c
Comment on lines +1144 to +1147
if (UNEXPECTED(!type_matched)) {
zval_ptr_dtor(&tmp);
return &EG(uninitialized_zval);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

An error handler or __toString() can run inside the type check and write this property. ZVAL_COPY_VALUE then overwrites it, and a reference taken there leaks. ASan reports it for this:

class P { public int $i; }
set_error_handler(function () { global $o, $r; $o->i = 5; $r = &$o->i; return true; });
function w(P $p, $v) { $p->i = $v; }
w(new P, 1);
$o = new P;
w($o, 1.5);
Suggested change
if (UNEXPECTED(!type_matched)) {
zval_ptr_dtor(&tmp);
return &EG(uninitialized_zval);
}
if (UNEXPECTED(!type_matched)) {
zval_ptr_dtor(&tmp);
return &EG(uninitialized_zval);
}
if (UNEXPECTED(Z_TYPE_P(property_val) != IS_UNDEF)) {
return zend_assign_to_variable(property_val, &tmp, IS_TMP_VAR, EX_USES_STRICT_TYPES());
}

Comment thread Zend/zend_vm_def.h Outdated
Comment on lines +2539 to +2541
} else if (prop_info != NULL
&& EXPECTED((Z_PROP_FLAG_P(property_val) & (IS_PROP_UNINIT|IS_PROP_LAZY)) == IS_PROP_UNINIT)
&& EXPECTED(!zend_object_is_lazy(zobj))) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The cache slot is shared by all objects of the class, but ArrayObject and DateInterval decide per object where a write goes. Here the second write sets the property instead of the array entry:

class A extends ArrayObject { public int $x; }
function w(A $o) { $o->x = 2; }
w(new A([]));
$b = new A([], ArrayObject::ARRAY_AS_PROPS);
w($b);
var_dump($b->getArrayCopy()); // ['x' => 2] without this PR, [] with it

A DateInterval subclass with a declared public int $y skips the interval's handler the same way. Checking the handler keeps those on the slow path:

Suggested change
} else if (prop_info != NULL
&& EXPECTED((Z_PROP_FLAG_P(property_val) & (IS_PROP_UNINIT|IS_PROP_LAZY)) == IS_PROP_UNINIT)
&& EXPECTED(!zend_object_is_lazy(zobj))) {
} else if (prop_info != NULL
&& EXPECTED((Z_PROP_FLAG_P(property_val) & (IS_PROP_UNINIT|IS_PROP_LAZY)) == IS_PROP_UNINIT)
&& EXPECTED(!zend_object_is_lazy(zobj))
&& EXPECTED(zobj->handlers->write_property == zend_std_write_property)) {

@adapik
adapik force-pushed the opt-assign-obj-uninit-typed-prop-master branch from 9fd27bf to 16841c8 Compare November 24, 2026 13:00
@adapik

adapik commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @ArtUkrainskiy, fixed both.

  1. After the type check I now re-check whether the property is still IS_UNDEF and assign normally if it isn't. One small change to your suggestion: I used zend_assign_to_variable_ex() with garbage_ptr. With the plain version, a destructor of the old value can free the object, and the VM then reads freed memory when the result is used. ASan caught it.
  2. Added the write_property == zend_std_write_property check, as you suggested.

I added all three repros to typed_properties_uninit_assign_fast_path.phpt. They pass under ASan.

This PR still depends on #24566: without it readonly_props/variation.phpt and the new _scope test fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants