Optimize getFqName calls in KotlinBuiltIns

Check the simple name first, and only then construct the full FqName, in all
'is*' methods
This commit is contained in:
Alexander Udalov
2016-01-25 18:06:12 +03:00
parent 04026dbe84
commit 7d880f10ec
3 changed files with 19 additions and 27 deletions
@@ -678,6 +678,11 @@ public abstract class KotlinBuiltIns {
return TypeUtils.makeNullable(getAnyType()); return TypeUtils.makeNullable(getAnyType());
} }
@NotNull
public KotlinType getDefaultBound() {
return getNullableAnyType();
}
// Primitive // Primitive
@NotNull @NotNull
@@ -992,12 +997,14 @@ public abstract class KotlinBuiltIns {
return parameterTypes; return parameterTypes;
} }
// Recognized & special
private static boolean isConstructedFromGivenClass(@NotNull KotlinType type, @NotNull FqNameUnsafe fqName) { private static boolean isConstructedFromGivenClass(@NotNull KotlinType type, @NotNull FqNameUnsafe fqName) {
ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor(); ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
return descriptor != null && return descriptor instanceof ClassDescriptor && classFqNameEquals(descriptor, fqName);
/* quick check to avoid creation of full FqName instance */ descriptor.getName().equals(fqName.shortName()) && }
private static boolean classFqNameEquals(@NotNull ClassifierDescriptor descriptor, @NotNull FqNameUnsafe fqName) {
// Quick check to avoid creation of full FqName instance
return descriptor.getName().equals(fqName.shortName()) &&
fqName.equals(getFqName(descriptor)); fqName.equals(getFqName(descriptor));
} }
@@ -1006,12 +1013,11 @@ public abstract class KotlinBuiltIns {
} }
public static boolean isSpecialClassWithNoSupertypes(@NotNull ClassDescriptor descriptor) { public static boolean isSpecialClassWithNoSupertypes(@NotNull ClassDescriptor descriptor) {
FqNameUnsafe fqName = getFqName(descriptor); return classFqNameEquals(descriptor, FQ_NAMES.any) || classFqNameEquals(descriptor, FQ_NAMES.nothing);
return FQ_NAMES.any.equals(fqName) || FQ_NAMES.nothing.equals(fqName);
} }
public static boolean isAny(@NotNull ClassDescriptor descriptor) { public static boolean isAny(@NotNull ClassDescriptor descriptor) {
return isAny(getFqName(descriptor)); return classFqNameEquals(descriptor, FQ_NAMES.any);
} }
public static boolean isAny(@NotNull KotlinType type) { public static boolean isAny(@NotNull KotlinType type) {
@@ -1019,7 +1025,6 @@ public abstract class KotlinBuiltIns {
} }
public static boolean isBoolean(@NotNull KotlinType type) { public static boolean isBoolean(@NotNull KotlinType type) {
return isConstructedFromGivenClassAndNotNullable(type, FQ_NAMES._boolean); return isConstructedFromGivenClassAndNotNullable(type, FQ_NAMES._boolean);
} }
@@ -1028,7 +1033,7 @@ public abstract class KotlinBuiltIns {
} }
public static boolean isBoolean(@NotNull ClassDescriptor classDescriptor) { public static boolean isBoolean(@NotNull ClassDescriptor classDescriptor) {
return FQ_NAMES._boolean.equals(getFqName(classDescriptor)); return classFqNameEquals(classDescriptor, FQ_NAMES._boolean);
} }
public static boolean isChar(@NotNull KotlinType type) { public static boolean isChar(@NotNull KotlinType type) {
@@ -1063,10 +1068,6 @@ public abstract class KotlinBuiltIns {
return isConstructedFromGivenClass(type, fqName) && !type.isMarkedNullable(); return isConstructedFromGivenClass(type, fqName) && !type.isMarkedNullable();
} }
public static boolean isAny(@NotNull FqNameUnsafe fqName) {
return FQ_NAMES.any.equals(fqName);
}
public static boolean isNothing(@NotNull KotlinType type) { public static boolean isNothing(@NotNull KotlinType type) {
return isNothingOrNullableNothing(type) return isNothingOrNullableNothing(type)
&& !type.isMarkedNullable(); && !type.isMarkedNullable();
@@ -1130,15 +1131,15 @@ public abstract class KotlinBuiltIns {
} }
public static boolean isKClass(@NotNull ClassDescriptor descriptor) { public static boolean isKClass(@NotNull ClassDescriptor descriptor) {
return FQ_NAMES.kClass.equals(getFqName(descriptor)); return classFqNameEquals(descriptor, FQ_NAMES.kClass);
} }
public static boolean isNonPrimitiveArray(@NotNull ClassDescriptor descriptor) { public static boolean isNonPrimitiveArray(@NotNull ClassDescriptor descriptor) {
return FQ_NAMES.array.equals(getFqName(descriptor)); return classFqNameEquals(descriptor, FQ_NAMES.array);
} }
public static boolean isCloneable(@NotNull ClassDescriptor descriptor) { public static boolean isCloneable(@NotNull ClassDescriptor descriptor) {
return FQ_NAMES.cloneable.equals(getFqName(descriptor)); return classFqNameEquals(descriptor, FQ_NAMES.cloneable);
} }
public static boolean isDeprecated(@NotNull DeclarationDescriptor declarationDescriptor) { public static boolean isDeprecated(@NotNull DeclarationDescriptor declarationDescriptor) {
@@ -1173,13 +1174,4 @@ public abstract class KotlinBuiltIns {
return false; return false;
} }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@NotNull
public KotlinType getDefaultBound() {
return getNullableAnyType();
}
} }
@@ -61,7 +61,7 @@ private class ClassClsStubBuilder(
private val supertypeIds = run { private val supertypeIds = run {
val supertypeIds = classProto.supertypes(c.typeTable).map { c.nameResolver.getClassId(it.className) } val supertypeIds = classProto.supertypes(c.typeTable).map { c.nameResolver.getClassId(it.className) }
//empty supertype list if single supertype is Any //empty supertype list if single supertype is Any
if (supertypeIds.singleOrNull()?.let { KotlinBuiltIns.isAny(it.asSingleFqName().toUnsafe()) } ?: false) { if (supertypeIds.singleOrNull()?.let { KotlinBuiltIns.FQ_NAMES.any == it.asSingleFqName().toUnsafe() } ?: false) {
listOf() listOf()
} }
else { else {
@@ -253,7 +253,7 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
private fun Type.isDefaultUpperBound(): Boolean { private fun Type.isDefaultUpperBound(): Boolean {
return this.hasClassName() && return this.hasClassName() &&
c.nameResolver.getClassId(className).let { KotlinBuiltIns.isAny(it.asSingleFqName().toUnsafe()) } && c.nameResolver.getClassId(className).let { KotlinBuiltIns.FQ_NAMES.any == it.asSingleFqName().toUnsafe() } &&
this.nullable this.nullable
} }
} }