Skip to content

Commit

Permalink
Optimize inner loop of intersect union search
Browse files Browse the repository at this point in the history
Array slice spends a good bit of time allocating memory for new arrays
even for small arrays. For larger computed unions this can be a
non-trivial amount of time. We can minimize the cost by not repeating
the slice for ever iteration of the inner loop since it doesn't depend
on the loop.
  • Loading branch information
neclimdul committed Jun 23, 2022
1 parent d194a47 commit 55caf64
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,13 @@ public static function intersect(Type ...$types): Type
$topLevelUnionSubTypes = [];
$innerTypes = $type->getTypes();
usort($innerTypes, $sortTypes);
$slice1 = array_slice($types, 0, $i);
$slice2 = array_slice($types, $i + 1);
foreach ($innerTypes as $innerUnionSubType) {
$topLevelUnionSubTypes[] = self::intersect(
$innerUnionSubType,
...array_slice($types, 0, $i),
...array_slice($types, $i + 1),
...$slice1,
...$slice2,
);
}

Expand Down

0 comments on commit 55caf64

Please sign in to comment.