Skip to content

Limit array shapes in unions and constant-array foreach unrolling in loop passes - #7306

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
limit-union-shapes-and-nested-unrolling
Dec 3, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
limit-union-shapes-and-nested-unrolling

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Two analysis-time fixes for slowdowns found comparing 2.3.7 with 2.3.x-dev on real projects.

Widen unions holding more than 16 array shapes. A list appended to in N mutually exclusive branches kept N shapes of up to N keys, which stayed far below ARRAY_COUNT_LIMIT values, and every loop pass joined the growing union. TypeCombinator::widen() now also widens unions with more than ArrayWidening::UNION_SHAPES_LIMIT (16) shapes; a list with optional keys counts once per possible length. Four nsrt expectations change to the widened (less exact, still correct) types.

Unroll a constant-array foreach in loop passes only when it writes with the loop variable. In an enclosing loop's convergence passes, a constant-array foreach was unrolled again every pass. It now unrolls there only when the body writes an offset keyed by the loop's key or value (what bug-15906 relies on). Ported to the native ForeachHandler.

Samples, Turbo, HEAD → this PR:

Sample Time Memory
160 conditional int appends 4.86 → 0.18 s 681 → 38 MB
80 conditional appends 1.35 → 0.16 s 134 → 16 MB
instanceof claims 0.35 → 0.06 s 2 → 2 MB
16-key foreach in a loop 0.38 → 0.01 s 2 → 0 MB

New benchmarks: conditional-list-appends.php, instanceof-list-appends-in-loop.php, constant-array-foreach-in-loop.php. The new nsrt test loop-in-unrolled-foreach-in-loop.php guards the precision of a loop nested in an unrolled foreach (from ResultCacheManager).

Full suite passes with and without the Turbo extension; walk-trace, smoke and parity checks are identical.

Exact unions keep shapes that do not merge into one, so a list appended to
in N mutually exclusive branches joined N shapes of up to N keys -
quadratic in the branches, while staying far below
ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT values. In a loop, every
convergence pass joined such a growing union: 160 conditional appends took
4.9 s and 681 MB.

TypeCombinator::widen() now also widens a union with more than
ArrayWidening::UNION_SHAPES_LIMIT shapes, counting a list with optional
keys once per length it can have. The 160 appends take 0.18 s and 38 MB.

Amp-Thread-ID: https://ampcode.com/threads/T-01a23b0c-2623-7217-8dd2-922690c49d46
…th the loop variable

Inside an enclosing loop's convergence passes, a foreach over a constant
array was unrolled key by key in every pass. Unrolling there is needed
only to keep the offsets the body writes with the loop's key or value
required, such as `$ids[$kind] = ...` (bug-15906); other bodies now
converge like any nested loop. A 16-key foreach in a loop drops from
0.38 s to 0.01 s.

The native ForeachHandler mirrors the check.

Amp-Thread-ID: https://ampcode.com/threads/T-01a23b0c-2623-7217-8dd2-922690c49d46
@ondrejmirtes
ondrejmirtes merged commit 6a3532b into 2.3.x Dec 3, 2026
897 of 916 checks passed
@ondrejmirtes
ondrejmirtes deleted the limit-union-shapes-and-nested-unrolling branch December 3, 2026 13:28
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