From 6a80bf9077044d677bbc264bc22d351524f655b9 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Fri, 27 Nov 2026 11:42:29 +0000 Subject: [PATCH 1/2] Widen the join of conditional expression holders with the same guards mergeSameGuardConditionalExpressions() joined the types of conditional holders across branches with the exact union() only. Since union() no longer generalizes big constant arrays itself, these joins grew without bound: a loop bumping counters of a 15-key shape in if/elseif chains inside a switch held about 70 shapes per holder and took 57 s. The join is now widened like the joins of scopes in ExpressionTypeHolder::and(). Closes https://github.com/phpstan/phpstan/issues/15862 Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4b-0cd2-7583-a39a-cf304ee8e07b --- src/Analyser/MutatingScope.php | 2 +- tests/bench/data/bug-15862.php | 91 +++++++++++++++++++++++++++++++++ turbo-ext/src/MutatingScope.cpp | 5 +- 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 tests/bench/data/bug-15862.php diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index febc36f6690..52398d7320e 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -4731,7 +4731,7 @@ private function mergeSameGuardConditionalExpressions( $ourGuards, new ExpressionTypeHolder( $ourTypeHolder->getExpr(), - TypeCombinator::union($ourTypeHolder->getType(), $theirTypeHolder->getType()), + TypeCombinator::widen(TypeCombinator::union($ourTypeHolder->getType(), $theirTypeHolder->getType())), $ourTypeHolder->getCertainty(), ), ); diff --git a/tests/bench/data/bug-15862.php b/tests/bench/data/bug-15862.php new file mode 100644 index 00000000000..f5de03ef7d4 --- /dev/null +++ b/tests/bench/data/bug-15862.php @@ -0,0 +1,91 @@ + $rows */ +function f(array $rows): void { + $r = [ + 'T_7' => 0, + 'T_30' => 0, + 'T_90' => 0, + 'C0_7' => 0, + 'C0_30' => 0, + 'C0_90' => 0, + 'C1_7' => 0, + 'C1_30' => 0, + 'C1_90' => 0, + 'C2_7' => 0, + 'C2_30' => 0, + 'C2_90' => 0, + 'C3_7' => 0, + 'C3_30' => 0, + 'C3_90' => 0, + ]; + foreach ($rows as $row) { + $a = $row['a'] ?? false; + $b = $row['b'] ?? false; + $c = $row['c'] ?? false; + $s = $row['s'] ?? false; + if ($a) { + $r['T_7']++; + $r['T_30']++; + $r['T_90']++; + } elseif ($b) { + $r['T_30']++; + $r['T_90']++; + } elseif ($c) { + $r['T_90']++; + } + switch ($row['t']) { + case 'c0': + if ($a) { + $r['C0_7']++; + $r['C0_30']++; + $r['C0_90']++; + } elseif ($b) { + $r['C0_30']++; + $r['C0_90']++; + } elseif ($c) { + $r['C0_90']++; + } + break; + case 'c1': + if ($a) { + $r['C1_7']++; + $r['C1_30']++; + $r['C1_90']++; + } elseif ($b) { + $r['C1_30']++; + $r['C1_90']++; + } elseif ($c) { + $r['C1_90']++; + } + break; + case 'c2': + if ($a) { + $r['C2_7']++; + $r['C2_30']++; + $r['C2_90']++; + } elseif ($b) { + $r['C2_30']++; + $r['C2_90']++; + } elseif ($c) { + $r['C2_90']++; + } + break; + case 'c3': + if ($a) { + $r['C3_7']++; + $r['C3_30']++; + $r['C3_90']++; + } elseif ($b) { + $r['C3_30']++; + $r['C3_90']++; + } elseif ($c) { + $r['C3_90']++; + } + break; + } + } + \PHPStan\dumpType($r); +} diff --git a/turbo-ext/src/MutatingScope.cpp b/turbo-ext/src/MutatingScope.cpp index 8fa592728ec..c580841b247 100644 --- a/turbo-ext/src/MutatingScope.cpp +++ b/turbo-ext/src/MutatingScope.cpp @@ -8961,7 +8961,10 @@ class MutatingScope zv::Val theirType = holderType(theirTypeHolder.ref()); zv::Val ourExpr = holderExpr(ourTypeHolder.ref()); if (UNEXPECTED(ourType.isUndef() || theirType.isUndef() || ourExpr.isUndef())) return zv::Val(); - zv::Val united = unionTypes(ourType.raw(), theirType.raw()); + /* TypeCombinator::widen(TypeCombinator::union(...)): a join of branches, widened like the joins of scopes */ + zv::Val joined = unionTypes(ourType.raw(), theirType.raw()); + if (UNEXPECTED(joined.isUndef())) return zv::Val(); + zv::Val united = pt_type_combinator_call(PT_LC("widen"), 1, joined.raw()); if (UNEXPECTED(united.isUndef())) return zv::Val(); zval typeHolder; pt_holder_create(&typeHolder, ourExpr.raw(), united.raw(), ourCertainty); From de38a6f55f4434db2117ee85590df6f9e9994d44 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Fri, 27 Nov 2026 11:59:24 +0000 Subject: [PATCH 2/2] Accept the benchmark numbers of bug-15862 From https://github.com/phpstan/phpstan-src/actions/runs/45992975831, the push run of 6a80bf907. The new benchmark had no numbers yet. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4b-0cd2-7583-a39a-cf304ee8e07b --- tests/bench/data/bug-15862.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/bench/data/bug-15862.php b/tests/bench/data/bug-15862.php index f5de03ef7d4..7d6a99922a3 100644 --- a/tests/bench/data/bug-15862.php +++ b/tests/bench/data/bug-15862.php @@ -1,5 +1,11 @@ $rows */