Skip to content

Revisit only newly guarded expressions in conditional-expression matching rounds - #7232

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
index-holders-by-guard
Nov 27, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
index-holders-by-guard

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

ScopeOps::matchConditionalExpressions() repeated full passes over every conditional expression holder in the scope until no new expression got specified. It runs on every applySpecifiedTypes(), so in code with many stored conditions it does quadratic work.

An expression that stays unmatched in one round can only match in a later one once one of its guards gets matched. Its other guards read the same specified values as before. So after the first round, the unmatched expressions are indexed by their guards, and later rounds revisit only the expressions guarded by one matched since. That includes expressions matched earlier in the same round, so the matches and their order are exactly the same.

Both passes also check a holder's guards before its certainty. The guards usually rule a holder out with one array_key_exists, before the three method calls of the certainty check.

On 800 statements of $xN = $in[N]; $okN = $xN !== null; if ($okN) …, analysis goes from 21.8 s to 14.0 s with the same output. The new benchmark tests/bench/data/many-stored-conditions.php (150 such statements) is about 12% faster locally.

The first round still scans every holder, and invalidation still scans every key. Both would need indexes kept in the scope itself, which this PR doesn't do.

The C++ mirror in ScopeOps.cpp is updated. Walk-trace is identical, and the full suite passes with and without the extension.

…hing rounds

matchConditionalExpressions() repeated full passes over every holder of the
scope until no new expression was specified. An expression unmatched in one
round can only match in a later one once one of its guards got matched, so
after the first round the unmatched expressions are indexed by their guards
and later rounds revisit only the ones guarded by an expression matched since.
The matches and their order are unchanged.

Both passes also check a holder's guards before its certainty, as the guards
usually rule it out first.

Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
@ondrejmirtes
ondrejmirtes force-pushed the index-holders-by-guard branch from b7ded4a to f3b91d7 Compare November 27, 2026 20:29
@ondrejmirtes
ondrejmirtes merged commit 7e3b7e2 into 2.3.x Nov 27, 2026
426 of 435 checks passed
@ondrejmirtes
ondrejmirtes deleted the index-holders-by-guard branch November 27, 2026 20:42
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