Repository navigation
Do not remember the container before a write into array shapes at a literal key - #7315
Open
ondrejmirtes wants to merge 1 commit into
Open
ondrejmirtes wants to merge 1 commit into
ondrejmirtes wants to merge 1 commit into
Conversation
…iteral key A shape takes a write at a literal key at exactly that key, so a later write to it has no previous value to drop. Remembering the container before the first write only added one remembered container per written key, which every branch merge had to join. A key written through a variable is still remembered: it can be one key on a pass of a loop and other keys on the next. Amp-Thread-ID: https://ampcode.com/threads/T-01a23cc2-ae72-744e-a853-c1c30b157a1b
ondrejmirtes
force-pushed
the
skip-write-base-of-replaced-shape-offset
branch
from
December 3, 2026 19:07
776748d to
22b4eb9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since 811ba58 ("Write array offsets weakly and remember the container before the offset's first write"), every offset write remembers the container from before that offset's first write as an
OffsetWriteBaseExprholder. For a write into array shapes at a literal key, as in$entry['k7'] = [...], that holder never changes a type: a shape takes the write at exactly that key, so a later write to it has no earlier value to drop. It still costs, though. A wide shape written at many literal keys under conditions in a loop carries one remembered copy of itself per key, and every branch merge joins all of them.AssignHandler::produceArrayDimFetchAssignValueToWrite()now skips the write base for the written (last) dimension when the dim is a literal string or integer and every member of the container is a constant array (writesShapeAtLiteralKey()). Both conditions are needed:nsrt/pr-4390.phpkeepsnon-empty-arrayfor the inner arrays only because of that.nsrt/has-offset-type-bug.php).The change is ported to
turbo-ext/src/AssignHandler.cpp, and the declaration headers are regenerated.The new benchmark
tests/bench/data/conditional-offset-writes-in-loop.phpis based on anonymized code from a real project. It has a 30-key shape with conditional writes in a foreach, and a file loop with$used[$k][$name][] = [...]appends. It has no numbers header yet.Tally
Run locally without Turbo, compared with origin/2.4.x (5e0c143):
The types
$valuesgets in loop-widening-optional-keys are identical before and after. These stayed within ±0.03% in instructions: conditional-list-appends, constant-array-foreach-in-loop, instanceof-list-appends-in-loop, literal-key-union-offset-write, loop-derived-assignments, nested-loops-liveness, nested-loops-wrapping-array and unrolled-foreach-list-guards.Inferred types
The full suite passes unchanged, apart from the added pr-4390 case. That case passes on 2.4.x too, and fails if the literal-key condition is dropped. About 60 extra scratch cases infer the same types as on 2.4.x: optional, missing and present literal keys, nested writes, loops, and unions with general arrays and
ArrayAccessobjects.One project's type changes. In the glpi integration test,
GenericobjectPluginMigration::getCustomFieldSpecs()gets a more precise return type: the same one 811ba58^ inferred, without the shapes that pairitemtypewith non-dropdown types. Thereturn.typeerror is still reported, but its message changed, so the entry ine2e/integration/glpi-baseline.neonin phpstan/phpstan needs updating.Verification on this branch
make tests: 25080 tests OK (119 skipped), both without Turbo and with the extension loaded.tests/smoke.phpALL OK,bin/side-by-side.phpOK, signature parity OK, and walk-trace (8 shards) identical over 789044 lines.The double merge of completed write bases in
MutatingScope::mergeWithVariableState()is handled separately in #7314. Together, the two take #7314'soffset-write-base-in-switch-loopbenchmark from 10.10B to 8.56B instructions.Requested from https://ampcode.com/threads/T-01a23b0c-2623-7217-8dd2-922690c49d46