Repository navigation
Conversation
|
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
left a comment
There was a problem hiding this comment.
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.
| if (UNEXPECTED(!type_matched)) { | ||
| zval_ptr_dtor(&tmp); | ||
| return &EG(uninitialized_zval); | ||
| } |
There was a problem hiding this comment.
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);| 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()); | |
| } |
| } 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))) { |
There was a problem hiding this comment.
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 itA 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:
| } 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)) { |
…ed typed property
9fd27bf to
16841c8
Compare
|
Thanks, @ArtUkrainskiy, fixed both.
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. |
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_OBJfor the case where the cache slot is hit, the property isIS_UNDEFwithIS_PROP_UNINITset (never unset()), and neither the property nor the object is lazy. A newzend_assign_to_uninit_typed_prop()verifies the type and stores the value.Results (release build, callgrind over 200k constructor calls with promoted readonly properties):
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.