Skip to content

Performance: super-linear analysis time when a variable is assigned across many === narrowing branches #14868

Closed

Description

@SanderMuller

Summary

Assigning a variable inside many if ($x === <const>) $v = …; branches makes analysis time grow super-linearly in the number of branches. It's a common shape (lexers, state machines, value mappers, factories, generated dispatch tables), so a large such function makes PHPStan disproportionately slow on that one file.

Reproduction

<?php
// php -r '$n=400; $c="<?php\nfunction f(int \$x): string {\n \$v=0;\n"; for($i=0;$i<$n;$i++){$c.=" if (\$x === $i) \$v = \"$i\";\n";} $c.=" return (string) \$v;\n}\n"; file_put_contents("repro.php",$c);'
function f(int $x): string {
    $v = 0;
    if ($x === 0) $v = "0";
    if ($x === 1) $v = "1";
    // ... one such branch per constant ...
    return (string) $v;
}

vendor/bin/phpstan analyse -l 8 repro.php, single file, PHP 8.5, PHPStan 2.2.x-dev:

N (branches) analysis CPU
25 0.46 s
100 0.79 s
400 20.6 s

100 → 400 is 4× the input for ~26× the time.

What it is NOT (discriminated at N=400)

variant CPU
if ($x === $i) $v = … (narrow + assign) 20.6 s the blowup
if (rand()) $v = … (same $v union, no narrowing) 1.9 s union-building is fine
if ($x === $i) return $i; (narrowing, no $v union) 0.6 s narrowing alone is fine
if ($x instanceof C$i) $v = … (instanceof, not ===) 2.7 s instanceof narrowing is fine

So it's specifically the interaction of constant-value === narrowing with assigning a variable, not union-building, not narrowing in general, and not instanceof.

Root cause

The cost is a call-count explosion in the conditional-expression machinery, not per-call work. Counting TypeCombinator calls on this reproducer:

  • N=200: intersect called 2.69M times; N=400: 21.4M times (≈8× for 2× input). Average args per call ~3 (tiny).

So each === branch records a conditional relating $v to $x, and applying the accumulated conditionals re-intersects $v's growing type once per narrowing op, compounding to a super-linear number of small intersect calls. (Confirmed it's the conditional application, not holder storage: capping the per-target conditional holders had no effect on the time.)

Related shapes (same family, also super-linear, for completeness)

  • Literal-union subject !== chain. With /** @param 1|2|…|N $x */ and if ($x !== 1 && $x !== 2 && … && $x !== N), analysis is ~O(N^2.9) (≈7 s at N=200, ~50 s at N=400), here dominated by TypeCombinator::remove (≈11M calls at N=200 on ~2-member unions). Note: the plain-string &&/|| version of this is already handled (the BooleanAnd/Or flattening optimisation, guarded by and-chain-truthy-blowup.php / or-chain-falsey-blowup.php); a literal-union subject appears to defeat that path.
  • Large match (true) constant chain. match (true) { $x === 0 => …, $x === 1 => …, … } is ~O(N²) (≈5.7 s at N=400). match ($value) over literal arms is fine (~0.8 s at N=400); it's the per-arm narrowing of the subject in match (true) that accumulates.

Environment

PHPStan 2.2.x-dev, PHP 8.5, level 8, single file, result cache cold. CPU = user+sys.

Activity

  1. mergeable commented on Jun 25, 2026

    @mergeable

    Hi there! 👋

    Looks like you opened an issue without following one of the issue templates:

    Bug report 🐛 (open an issue)

    If something isn't working as expected 🤔.

    Feature request 🚀 (open an issue)

    I have a suggestion (and may want to implement it 🙂)!

    Support question ❓ (open a discussion)

    I need some help with my code because PHPStan doesn't like it.


    The current issue will be closed. This is a precaution to save maintainer's time, I hope you'll understand.

    Sincerely, the bot 🤖

  2. github-actions commented on Jul 27, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  3. locked as resolved and limited conversation to collaborators on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions