Skip to content

Commit 6caf474

Browse files
committed
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 phpstan/phpstan#15862 Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4b-0cd2-7583-a39a-cf304ee8e07b
1 parent b50f182 commit 6caf474

3 files changed

Lines changed: 96 additions & 2 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4731,7 +4731,7 @@ private function mergeSameGuardConditionalExpressions(
47314731
$ourGuards,
47324732
new ExpressionTypeHolder(
47334733
$ourTypeHolder->getExpr(),
4734-
TypeCombinator::union($ourTypeHolder->getType(), $theirTypeHolder->getType()),
4734+
TypeCombinator::widen(TypeCombinator::union($ourTypeHolder->getType(), $theirTypeHolder->getType())),
47354735
$ourTypeHolder->getCertainty(),
47364736
),
47374737
);

‎tests/bench/data/bug-15862.php‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15862;
4+
5+
/** @param array<mixed> $rows */
6+
function f(array $rows): void {
7+
$r = [
8+
'T_7' => 0,
9+
'T_30' => 0,
10+
'T_90' => 0,
11+
'C0_7' => 0,
12+
'C0_30' => 0,
13+
'C0_90' => 0,
14+
'C1_7' => 0,
15+
'C1_30' => 0,
16+
'C1_90' => 0,
17+
'C2_7' => 0,
18+
'C2_30' => 0,
19+
'C2_90' => 0,
20+
'C3_7' => 0,
21+
'C3_30' => 0,
22+
'C3_90' => 0,
23+
];
24+
foreach ($rows as $row) {
25+
$a = $row['a'] ?? false;
26+
$b = $row['b'] ?? false;
27+
$c = $row['c'] ?? false;
28+
$s = $row['s'] ?? false;
29+
if ($a) {
30+
$r['T_7']++;
31+
$r['T_30']++;
32+
$r['T_90']++;
33+
} elseif ($b) {
34+
$r['T_30']++;
35+
$r['T_90']++;
36+
} elseif ($c) {
37+
$r['T_90']++;
38+
}
39+
switch ($row['t']) {
40+
case 'c0':
41+
if ($a) {
42+
$r['C0_7']++;
43+
$r['C0_30']++;
44+
$r['C0_90']++;
45+
} elseif ($b) {
46+
$r['C0_30']++;
47+
$r['C0_90']++;
48+
} elseif ($c) {
49+
$r['C0_90']++;
50+
}
51+
break;
52+
case 'c1':
53+
if ($a) {
54+
$r['C1_7']++;
55+
$r['C1_30']++;
56+
$r['C1_90']++;
57+
} elseif ($b) {
58+
$r['C1_30']++;
59+
$r['C1_90']++;
60+
} elseif ($c) {
61+
$r['C1_90']++;
62+
}
63+
break;
64+
case 'c2':
65+
if ($a) {
66+
$r['C2_7']++;
67+
$r['C2_30']++;
68+
$r['C2_90']++;
69+
} elseif ($b) {
70+
$r['C2_30']++;
71+
$r['C2_90']++;
72+
} elseif ($c) {
73+
$r['C2_90']++;
74+
}
75+
break;
76+
case 'c3':
77+
if ($a) {
78+
$r['C3_7']++;
79+
$r['C3_30']++;
80+
$r['C3_90']++;
81+
} elseif ($b) {
82+
$r['C3_30']++;
83+
$r['C3_90']++;
84+
} elseif ($c) {
85+
$r['C3_90']++;
86+
}
87+
break;
88+
}
89+
}
90+
\PHPStan\dumpType($r);
91+
}

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8961,7 +8961,10 @@ class MutatingScope
89618961
zv::Val theirType = holderType(theirTypeHolder.ref());
89628962
zv::Val ourExpr = holderExpr(ourTypeHolder.ref());
89638963
if (UNEXPECTED(ourType.isUndef() || theirType.isUndef() || ourExpr.isUndef())) return zv::Val();
8964-
zv::Val united = unionTypes(ourType.raw(), theirType.raw());
8964+
/* TypeCombinator::widen(TypeCombinator::union(...)): a join of branches, widened like the joins of scopes */
8965+
zv::Val joined = unionTypes(ourType.raw(), theirType.raw());
8966+
if (UNEXPECTED(joined.isUndef())) return zv::Val();
8967+
zv::Val united = pt_type_combinator_call(PT_LC("widen"), 1, joined.raw());
89658968
if (UNEXPECTED(united.isUndef())) return zv::Val();
89668969
zval typeHolder;
89678970
pt_holder_create(&typeHolder, ourExpr.raw(), united.raw(), ourCertainty);

0 commit comments

Comments
 (0)