Repository navigation
Limit array shapes in unions and constant-array foreach unrolling in loop passes - #7306
Merged
Merged
Conversation
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
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.
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_LIMITvalues, and every loop pass joined the growing union.TypeCombinator::widen()now also widens unions with more thanArrayWidening::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:
New benchmarks:
conditional-list-appends.php,instanceof-list-appends-in-loop.php,constant-array-foreach-in-loop.php. The new nsrt testloop-in-unrolled-foreach-in-loop.phpguards the precision of a loop nested in an unrolled foreach (fromResultCacheManager).Full suite passes with and without the Turbo extension; walk-trace, smoke and parity checks are identical.