From da2d417662871ba6b51fa8fa4ec37783cd404811 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 16 Apr 2013 14:02:57 +0400 Subject: [PATCH] Optimization: no need to build a set of all supertypes, if we only need to check whether there's a nullable one --- .../org/jetbrains/jet/lang/types/TypeUtils.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java index f4c7c557206..02a6f1229a5 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java @@ -374,7 +374,9 @@ public class TypeUtils { @NotNull public static Set getAllSupertypes(@NotNull JetType type) { - Set result = Sets.newLinkedHashSet(); + // 15 is obtained by experimentation: JDK classes like ArrayList tend to have so many supertypes, + // the average number is lower + Set result = new LinkedHashSet(15); collectAllSupertypes(type, result); return result; } @@ -389,11 +391,16 @@ public class TypeUtils { } public static boolean hasNullableSuperType(@NotNull JetType type) { - for (JetType supertype : getAllSupertypes(type)) { - if (supertype.isNullable()) { - return true; - } + if (type.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor) { + // A class/trait cannot have a nullable supertype + return false; } + + for (JetType supertype : getImmediateSupertypes(type)) { + if (supertype.isNullable()) return true; + if (hasNullableSuperType(supertype)) return true; + } + return false; }