Skip to content

Commit 094ff78

Browse files
vokuondrejmirtes
authored andcommitted
faster TypeCombinator::intersect()
-> ~18% less "count()" calls in my first small tests
1 parent b61a0fd commit 094ff78

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Type/TypeCombinator.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,9 @@ public static function intersect(Type ...$types): Type
795795
// transform callable & int to never
796796
// transform A & ~A to never
797797
// transform int & string to never
798-
for ($i = 0; $i < count($types); $i++) {
799-
for ($j = $i + 1; $j < count($types); $j++) {
798+
$typesCount = count($types);
799+
for ($i = 0; $i < $typesCount; $i++) {
800+
for ($j = $i + 1; $j < $typesCount; $j++) {
800801
if ($types[$j] instanceof SubtractableType) {
801802
$typeWithoutSubtractedTypeA = $types[$j]->getTypeWithoutSubtractedType();
802803

@@ -808,6 +809,7 @@ public static function intersect(Type ...$types): Type
808809
if ($isSuperTypeSubtractableA->yes()) {
809810
$types[$i] = self::unionWithSubtractedType($types[$i], $types[$j]->getSubtractedType());
810811
array_splice($types, $j--, 1);
812+
$typesCount--;
811813
continue 1;
812814
}
813815
}
@@ -823,6 +825,7 @@ public static function intersect(Type ...$types): Type
823825
if ($isSuperTypeSubtractableB->yes()) {
824826
$types[$j] = self::unionWithSubtractedType($types[$j], $types[$i]->getSubtractedType());
825827
array_splice($types, $i--, 1);
828+
$typesCount--;
826829
continue 2;
827830
}
828831
}
@@ -832,6 +835,7 @@ public static function intersect(Type ...$types): Type
832835
if ($intersectionType !== null) {
833836
$types[$j] = $intersectionType;
834837
array_splice($types, $i--, 1);
838+
$typesCount--;
835839
continue 2;
836840
}
837841
}
@@ -844,6 +848,7 @@ public static function intersect(Type ...$types): Type
844848

845849
if ($isSuperTypeA->yes()) {
846850
array_splice($types, $j--, 1);
851+
$typesCount--;
847852
continue;
848853
}
849854

@@ -857,12 +862,14 @@ public static function intersect(Type ...$types): Type
857862
if ($types[$i] instanceof ConstantArrayType && $types[$j] instanceof HasOffsetType) {
858863
$types[$i] = $types[$i]->makeOffsetRequired($types[$j]->getOffsetType());
859864
array_splice($types, $j--, 1);
865+
$typesCount--;
860866
continue;
861867
}
862868

863869
if ($types[$j] instanceof ConstantArrayType && $types[$i] instanceof HasOffsetType) {
864870
$types[$j] = $types[$j]->makeOffsetRequired($types[$i]->getOffsetType());
865871
array_splice($types, $i--, 1);
872+
$typesCount--;
866873
continue 2;
867874
}
868875

@@ -878,6 +885,7 @@ public static function intersect(Type ...$types): Type
878885
$types[$j] = new ArrayType($keyType, $itemType);
879886
}
880887
array_splice($types, $i--, 1);
888+
$typesCount--;
881889
continue 2;
882890
}
883891

@@ -886,6 +894,7 @@ public static function intersect(Type ...$types): Type
886894

887895
if ($isSuperTypeB->yes()) {
888896
array_splice($types, $i--, 1);
897+
$typesCount--;
889898
continue 2;
890899
}
891900

@@ -895,7 +904,7 @@ public static function intersect(Type ...$types): Type
895904
}
896905
}
897906

898-
if (count($types) === 1) {
907+
if ($typesCount === 1) {
899908
return $types[0];
900909
}
901910

0 commit comments

Comments
 (0)