Skip to content

Fix exponential analysis slowdowns on deeply nested code - #7078

Merged
ondrejmirtes merged 8 commits into
2.3.xfrom
analyser-pathological-slowdowns
Nov 15, 2026
Merged

ondrejmirtes merged 8 commits into
2.3.xfrom
analyser-pathological-slowdowns

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Fixes for analysed code that made the analyser slow down exponentially, plus one cubic case. Each commit fixes one of them, mirrors the change in its turbo-ext/src C++ file, and adds a benchmark to tests/bench/data with the Tally header.

Wall clock is measured without the extension and includes about 1s of startup. Tally counts are instructions.

Benchmark What was slow Wall clock Tally
closure-nested-shape-description.php ConstantArrayType::toPhpDocNode() built each nested value twice (2^depth) 3.54s → 0.93s 24.8B → 430.8M
nested-loops-liveness.php VariableLivenessResolver re-solved inner loops from scratch on every outer pass 2.34s → 1.45s 8.45B → 1.343B
nullsafe-chain-narrowing.php ?-> chains: VariableFlow::choice() and the nullsafe narrowing callbacks repeated work per link 7.89s → 0.88s 46.7B → 333.8M
generic-inheritance-chain.php CrossCheckInterfacesHelper revisited classes; ClassReflection::getParentClass() did not cache generic parents 3.74s → 1.19s 14.3B → 216.0M
nested-nullable-shape-accepts.php UnionType::accepts() of another union walked nested shapes twice per level 10.28s → 1.21s → 216.9M
loop-derived-assignments.php Derived conditional expressions after assignments in a loop (cubic → quadratic) 7.32s → 2.87s → 13.03B

All of these produce the same analysis output as before, with and without the turbo extension. Verified with:

  • the full test suite, with and without the extension
  • make phpstan
  • phpcs
  • the turbo smoke test and signature parity check

The key-value and value-only item lists now share the node, so describing
a value nested N shapes deep no longer costs 2^N.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823
VariableLivenessResolver remembers the last next set, context and head of
each loop; an outer pass whose next set and context cover them starts the
inner loop's iteration from that head instead of from scratch.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823
…ull narrowing

VariableFlow::choice() of a sequence and its own first flow becomes a
sequence of that flow and the optional rest, and the nullsafe property
fetch and method call handlers ask once per link whether it did not
short-circuit.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823
…lone

The member-by-member check cannot come out yes unless the compound answer
does, which replaced its result anyway; asking both walked nested members
twice at every level.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823
…thmetic

When the assigned expression is arithmetic, concatenation or bitwise
operators over variables, literals and constants, conditions on variables
it does not read are not assigned, and the derived type of a consequent
without remaining conditions is computed once.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823
@ondrejmirtes
ondrejmirtes merged commit 1f57ff5 into 2.3.x Nov 15, 2026
381 of 386 checks passed
@ondrejmirtes
ondrejmirtes deleted the analyser-pathological-slowdowns branch November 15, 2026 14:45
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