Optimize hot TypeUtils.contains function

optimize for small number of visited types
This commit is contained in:
Ilya Chernikov
2020-03-06 15:00:27 +01:00
parent 4538e212db
commit 53d289206e
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner;
import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor;
import org.jetbrains.kotlin.types.refinement.TypeRefinement;
import org.jetbrains.kotlin.utils.SmartSet;
import java.util.*;
@@ -418,21 +419,25 @@ public class TypeUtils {
@Nullable KotlinType type,
@NotNull Function1<UnwrappedType, Boolean> isSpecialType
) {
return contains(type, isSpecialType, new HashSet<KotlinType>());
return contains(type, isSpecialType, null);
}
private static boolean contains(
@Nullable KotlinType type,
@NotNull Function1<UnwrappedType, Boolean> isSpecialType,
HashSet<KotlinType> visited
SmartSet<KotlinType> visited
) {
if (type == null) return false;
if (visited.contains(type)) return false;
visited.add(type);
if (visited != null && visited.contains(type)) return false;
UnwrappedType unwrappedType = type.unwrap();
if (isSpecialType.invoke(unwrappedType)) return true;
if (visited == null) {
visited = SmartSet.create();
}
visited.add(type);
FlexibleType flexibleType = unwrappedType instanceof FlexibleType ? (FlexibleType) unwrappedType : null;
if (flexibleType != null
&& (contains(flexibleType.getLowerBound(), isSpecialType, visited) || contains(flexibleType.getUpperBound(), isSpecialType, visited))) {