Skip to content

Commit

Permalink
Optimize calculating scalar values from huge unions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 25, 2021
1 parent 707cfb2 commit a6ca5f7
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Analyser/MutatingScope.php
Expand Up @@ -101,6 +101,8 @@
class MutatingScope implements Scope
{

public const CALCULATE_SCALARS_LIMIT = 128;

private const OPERATOR_SIGIL_MAP = [
Node\Expr\AssignOp\Plus::class => '+',
Node\Expr\AssignOp\Minus::class => '-',
Expand Down Expand Up @@ -1099,11 +1101,18 @@ private function resolveType(Expr $node): Type
$leftTypes = TypeUtils::getConstantScalars($this->getType($left));
$rightTypes = TypeUtils::getConstantScalars($this->getType($right));

if (count($leftTypes) > 0 && count($rightTypes) > 0) {
$leftTypesCount = count($leftTypes);
$rightTypesCount = count($rightTypes);
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
$resultTypes = [];
$generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT;
foreach ($leftTypes as $leftType) {
foreach ($rightTypes as $rightType) {
$resultTypes[] = $this->calculateFromScalars($node, $leftType, $rightType);
$resultType = $this->calculateFromScalars($node, $leftType, $rightType);
if ($generalize) {
$resultType = TypeUtils::generalizeType($resultType);
}
$resultTypes[] = $resultType;
}
}
return TypeCombinator::union(...$resultTypes);
Expand Down

0 comments on commit a6ca5f7

Please sign in to comment.