Skip to content

Commit 1447c34

Browse files
committed
Merge two single-key arrays once when unioning them
The single-key merge in reduceArrays() added for phpstan/phpstan#14462 unioned the two values to decide whether to merge, then mergeWith() unioned them again, and the keys-superset merge of the same pair below did it a third time. With arrays nested in each other (`$x = [$x]` in nested loops) every union repeats the work for each level, so the analysis became exponential in the nesting: three nested while loops take 44 seconds instead of 0.2, four do not finish. mergeWith() is now called once, its merged value decides the question, and the keys-superset branch reuses it. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c574-f264-7659-8779-5e71ca99b89b
1 parent 3b785fd commit 1447c34

3 files changed

Lines changed: 68 additions & 22 deletions

File tree

‎src/Type/TypeCombinator.php‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,17 +2003,21 @@ private static function reduceArrays(array $constantArrays, bool $preserveTagged
20032003
// and prevents exponential union growth when narrowing nested
20042004
// ArrayDimFetch expressions on a ConstantArrayType parent (see
20052005
// phpstan/phpstan#14462).
2006+
// mergeWith() unions the two values, so its result answers the
2007+
// question, and a keys-superset merge of the same pair below
2008+
// reuses it: unioning the values once more there made nested
2009+
// single-key arrays (`$x = [$x]` in nested loops) exponential
2010+
// in their depth.
2011+
$singleKeyMerge = null;
20062012
if (
20072013
$preserveTaggedUnions
20082014
&& $overlappingKeysCount === 1
20092015
&& count($arraysToProcess[$i]->getKeyTypes()) === 1
20102016
&& count($arraysToProcess[$j]->getKeyTypes()) === 1
20112017
) {
2012-
$iValueType = $arraysToProcess[$i]->getValueTypes()[0];
2013-
$jValueType = $arraysToProcess[$j]->getValueTypes()[0];
2014-
$unionValueType = self::union($iValueType, $jValueType);
2015-
if (!$unionValueType instanceof UnionType) {
2016-
$arraysToProcess[$j] = $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
2018+
$singleKeyMerge = $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
2019+
if (!$singleKeyMerge->getValueTypes()[0] instanceof UnionType) {
2020+
$arraysToProcess[$j] = $singleKeyMerge;
20172021
$constantScalarValues[$j] = null;
20182022
$nullifiedConstantScalarValues[$j] = true;
20192023
unset($arraysToProcess[$i]);
@@ -2039,7 +2043,7 @@ private static function reduceArrays(array $constantArrays, bool $preserveTagged
20392043
&& $overlappingKeysCount === count($arraysToProcess[$i]->getKeyTypes())
20402044
&& $arraysToProcess[$j]->isKeysSupersetOf($arraysToProcess[$i])
20412045
) {
2042-
$arraysToProcess[$j] = $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
2046+
$arraysToProcess[$j] = $singleKeyMerge ?? $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
20432047
$constantScalarValues[$j] = null;
20442048
$nullifiedConstantScalarValues[$j] = true;
20452049
unset($arraysToProcess[$i]);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace BenchNestedLoopsWrappingArray;
4+
5+
/**
6+
* Every loop wraps the value in a single-key array. Merging the loop
7+
* scopes unions single-key arrays nested in each other, and unioning
8+
* their values more than once per merge made it exponential in the
9+
* nesting depth.
10+
*/
11+
function run(): void
12+
{
13+
$x = 0;
14+
while (rand(0, 1)) {
15+
$x = [$x];
16+
while (rand(0, 1)) {
17+
$x = [$x];
18+
while (rand(0, 1)) {
19+
$x = [$x];
20+
while (rand(0, 1)) {
21+
$x = [$x];
22+
}
23+
}
24+
}
25+
}
26+
27+
var_dump($x);
28+
}

‎turbo-ext/src/TypeCombinator.cpp‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,15 +2967,21 @@ class TypeCombinator
29672967
return (zend_long) countOf(keyTypes.raw());
29682968
}
29692969

2970+
/* $arraysToProcess[$into] = $merged; unset($arraysToProcess[$from]); */
2971+
static void storeMerged(zv::Arr &arraysToProcess, zend_ulong into, zend_ulong from, zv::Val merged)
2972+
{
2973+
zval v = merged.take();
2974+
zend_hash_index_update(arraysToProcess.table(), into, &v);
2975+
zend_hash_index_del(arraysToProcess.table(), from);
2976+
}
2977+
29702978
/* $arraysToProcess[$into] = $arraysToProcess[$into]->mergeWith($arraysToProcess[$from]);
29712979
* unset($arraysToProcess[$from]); false = pending exception */
29722980
[[nodiscard]] static bool mergeInto(zv::Arr &arraysToProcess, zend_ulong into, zend_ulong from)
29732981
{
29742982
zv::Val merged = call(arrayAt(arraysToProcess, into), PT_LC("mergewith"), 1, arrayAt(arraysToProcess, from));
29752983
if (UNEXPECTED(merged.isUndef())) return false;
2976-
zval v = merged.take();
2977-
zend_hash_index_update(arraysToProcess.table(), into, &v);
2978-
zend_hash_index_del(arraysToProcess.table(), from);
2984+
storeMerged(arraysToProcess, into, from, std::move(merged));
29792985
return true;
29802986
}
29812987

@@ -3379,25 +3385,29 @@ class TypeCombinator
33793385
/* Merge two single-key arrays sharing the same key when their
33803386
* value types union into a single type (not a UnionType) —
33813387
* lossless, and it prevents exponential union growth
3382-
* (phpstan/phpstan#14462) */
3388+
* (phpstan/phpstan#14462). mergeWith() unions the two values,
3389+
* so its result answers the question, and a keys-superset
3390+
* merge of the same pair below reuses it: unioning the values
3391+
* once more there made nested single-key arrays (`$x = [$x]`
3392+
* in nested loops) exponential in their depth. */
3393+
zv::Val singleKeyMerge;
3394+
bool hasSingleKeyMerge = false;
33833395
if (preserveTaggedUnions && overlappingKeysCount == 1 && iKeysCount == 1) {
33843396
zend_long jKeysCount = keyTypesCount(arrayAt(arraysToProcess, j));
33853397
PT_FAIL_IF_NEG(jKeysCount);
33863398
if (jKeysCount == 1) {
3387-
zv::Val iValueTypes = call(arrayAt(arraysToProcess, i), PT_LC("getvaluetypes"));
3388-
PT_FAIL_IF_UNDEF(iValueTypes);
3389-
zv::Val jValueTypes = call(arrayAt(arraysToProcess, j), PT_LC("getvaluetypes"));
3390-
PT_FAIL_IF_UNDEF(jValueTypes);
3391-
zval *iValueType = indexOf(iValueTypes.raw(), 0);
3392-
zval *jValueType = indexOf(jValueTypes.raw(), 0);
3393-
if (UNEXPECTED(iValueType == NULL || jValueType == NULL)) {
3399+
singleKeyMerge = call(arrayAt(arraysToProcess, j), PT_LC("mergewith"), 1, arrayAt(arraysToProcess, i));
3400+
PT_FAIL_IF_UNDEF(singleKeyMerge);
3401+
hasSingleKeyMerge = true;
3402+
zv::Val mergedValueTypes = call(singleKeyMerge.raw(), PT_LC("getvaluetypes"));
3403+
PT_FAIL_IF_UNDEF(mergedValueTypes);
3404+
zval *mergedValueType = indexOf(mergedValueTypes.raw(), 0);
3405+
if (UNEXPECTED(mergedValueType == NULL)) {
33943406
zend_throw_error(NULL, "Undefined array key 0");
33953407
return zv::Val();
33963408
}
3397-
zv::Val unionValueType = union2(iValueType, jValueType);
3398-
PT_FAIL_IF_UNDEF(unionValueType);
3399-
if (!isInstance(unionValueType.raw(), pt_ce_union_type)) {
3400-
if (UNEXPECTED(!mergeInto(arraysToProcess, j, i))) return zv::Val();
3409+
if (!isInstance(mergedValueType, pt_ce_union_type)) {
3410+
storeMerged(arraysToProcess, j, i, std::move(singleKeyMerge));
34013411
forgetConstantScalarValues(constantScalarValues, j);
34023412
nullify(nullified, nullifiedList, j);
34033413
break;
@@ -3420,7 +3430,11 @@ class TypeCombinator
34203430
int superset = isKeysSupersetOf(arrayAt(arraysToProcess, j), arrayAt(arraysToProcess, i));
34213431
PT_FAIL_IF_NEG(superset);
34223432
if (superset) {
3423-
if (UNEXPECTED(!mergeInto(arraysToProcess, j, i))) return zv::Val();
3433+
if (hasSingleKeyMerge) {
3434+
storeMerged(arraysToProcess, j, i, std::move(singleKeyMerge));
3435+
} else if (UNEXPECTED(!mergeInto(arraysToProcess, j, i))) {
3436+
return zv::Val();
3437+
}
34243438
forgetConstantScalarValues(constantScalarValues, j);
34253439
nullify(nullified, nullifiedList, j);
34263440
break;

0 commit comments

Comments
 (0)