Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement OversizedArrayBuilder to improve huge constant array perfor…
…mance
- Loading branch information
1 parent
25dbb8f
commit ae37fb9
Showing
15 changed files
with
24,374 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Constant; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PHPStan\Node\Expr\TypeExpr; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Type\Accessory\AccessoryArrayListType; | ||
use PHPStan\Type\Accessory\NonEmptyArrayType; | ||
use PHPStan\Type\Accessory\OversizedArrayType; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\GeneralizePrecision; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function array_splice; | ||
use function array_values; | ||
use function count; | ||
|
||
class OversizedArrayBuilder | ||
{ | ||
|
||
/** | ||
* @param callable(Expr): Type $getTypeCallback | ||
*/ | ||
public function build(Array_ $expr, callable $getTypeCallback): Type | ||
{ | ||
$isList = true; | ||
$valueTypes = []; | ||
$keyTypes = []; | ||
$nextAutoIndex = 0; | ||
$items = $expr->items; | ||
for ($i = 0; $i < count($items); $i++) { | ||
$item = $items[$i]; | ||
if ($item === null) { | ||
continue; | ||
} | ||
if (!$item->unpack) { | ||
continue; | ||
} | ||
|
||
$valueType = $getTypeCallback($item->value); | ||
if ($valueType instanceof ConstantArrayType) { | ||
array_splice($items, $i, 1); | ||
foreach ($valueType->getKeyTypes() as $j => $innerKeyType) { | ||
$innerValueType = $valueType->getValueTypes()[$j]; | ||
if ($innerKeyType->isString()->no()) { | ||
$keyExpr = null; | ||
} else { | ||
$keyExpr = new TypeExpr($innerKeyType); | ||
} | ||
array_splice($items, $i++, 0, [new Expr\ArrayItem( | ||
new TypeExpr($innerValueType), | ||
$keyExpr, | ||
)]); | ||
} | ||
} else { | ||
array_splice($items, $i, 1, [new Expr\ArrayItem( | ||
new TypeExpr($valueType->getIterableValueType()), | ||
new TypeExpr($valueType->getIterableKeyType()), | ||
)]); | ||
} | ||
} | ||
foreach ($items as $item) { | ||
if ($item === null) { | ||
continue; | ||
} | ||
if ($item->unpack) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
if ($item->key !== null) { | ||
$itemKeyType = $getTypeCallback($item->key); | ||
if (!$itemKeyType instanceof ConstantIntegerType) { | ||
$isList = false; | ||
} elseif ($itemKeyType->getValue() !== $nextAutoIndex) { | ||
$isList = false; | ||
$nextAutoIndex = $itemKeyType->getValue() + 1; | ||
} else { | ||
$itemKeyType = new ConstantIntegerType($nextAutoIndex); | ||
$nextAutoIndex++; | ||
} | ||
} else { | ||
$itemKeyType = new ConstantIntegerType($nextAutoIndex); | ||
$nextAutoIndex++; | ||
} | ||
|
||
$generalizedKeyType = $itemKeyType->generalize(GeneralizePrecision::moreSpecific()); | ||
$keyTypes[$generalizedKeyType->describe(VerbosityLevel::precise())] = $generalizedKeyType; | ||
|
||
$itemValueType = $getTypeCallback($item->value); | ||
$generalizedValueType = $itemValueType->generalize(GeneralizePrecision::moreSpecific()); | ||
$valueTypes[$generalizedValueType->describe(VerbosityLevel::precise())] = $generalizedValueType; | ||
} | ||
|
||
$keyType = TypeCombinator::union(...array_values($keyTypes)); | ||
$valueType = TypeCombinator::union(...array_values($valueTypes)); | ||
|
||
$arrayType = new ArrayType($keyType, $valueType); | ||
if ($isList) { | ||
$arrayType = AccessoryArrayListType::intersectWith($arrayType); | ||
} | ||
|
||
return TypeCombinator::intersect($arrayType, new NonEmptyArrayType(), new OversizedArrayType()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.