Skip to content

Do not remember the container before a write into array shapes at a literal key - #7315

Open
ondrejmirtes wants to merge 1 commit into
2.4.xfrom
skip-write-base-of-replaced-shape-offset
Open

ondrejmirtes wants to merge 1 commit into
2.4.xfrom
skip-write-base-of-replaced-shape-offset

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

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 OffsetWriteBaseExpr holder. 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:

  • The literal key. A variable key, even when it is a single constant on the first pass of a loop, can be another key on the next pass, and then the remembered container matters. The new case in nsrt/pr-4390.php keeps non-empty-array for the inner arrays only because of that.
  • Constant arrays. A general array needs the remembered container to drop the value type written before (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.php is 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):

Benchmark 2.4.x This PR Change
conditional-offset-writes-in-loop (instructions) 14.68B 8.054B −45.1%
conditional-offset-writes-in-loop (cycles) 17.89B 10.02B −44.0%
conditional-offset-writes-in-loop (memory) 23.31 MiB 11.90 MiB −49%
loop-widening-optional-keys (instructions) 4.815B 1.209B −74.9% (its header expects 1.101B)
loop-widening-optional-keys (memory) 6.72 MiB 5.83 MiB −13%

The types $values gets 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 ArrayAccess objects.

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 pair itemtype with non-dropdown types. The return.type error is still reported, but its message changed, so the entry in e2e/integration/glpi-baseline.neon in phpstan/phpstan needs updating.

Verification on this branch

  • make tests: 25080 tests OK (119 skipped), both without Turbo and with the extension loaded.
  • turbo-ext: strict build, tests/smoke.php ALL OK, bin/side-by-side.php OK, 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's offset-write-base-in-switch-loop benchmark from 10.10B to 8.56B instructions.

Requested from https://ampcode.com/threads/T-01a23b0c-2623-7217-8dd2-922690c49d46

…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
ondrejmirtes force-pushed the skip-write-base-of-replaced-shape-offset branch from 776748d to 22b4eb9 Compare December 3, 2026 19:07
@ondrejmirtes ondrejmirtes changed the title Do not remember the container before a write that replaces a key of a constant array Do not remember the container before a write into array shapes at a literal key Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant