From c25e2e34a2eeccb5b732fec32776985df6f5a61b Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Thu, 21 Apr 2016 19:53:44 +0300 Subject: [PATCH] Optimization. Do not create objects for flexible type capabilities. --- .../java/lazy/types/LazyJavaTypeResolver.kt | 25 +++++++++++-------- .../jetbrains/kotlin/types/dynamicTypes.kt | 25 +++++++++++-------- .../jetbrains/kotlin/types/flexibleTypes.kt | 17 ++++++------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt index 6eb904373d0..d359210220a 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -290,19 +290,22 @@ class LazyJavaTypeResolver( override val id: String get() = "kotlin.jvm.PlatformType" - override fun getCapability(capabilityClass: Class, jetType: KotlinType, flexibility: Flexibility): T? { - @Suppress("UNCHECKED_CAST") - return when (capabilityClass) { - CustomTypeVariable::class.java, Specificity::class.java -> Impl(flexibility) as T - else -> null - } + override fun createFlexibleType(lowerBound: KotlinType, upperBound: KotlinType): KotlinType { + if (lowerBound == upperBound) return lowerBound + + return Impl(lowerBound, upperBound) } + private class Impl(lowerBound: KotlinType, upperBound: KotlinType) : + DelegatingFlexibleType(lowerBound, upperBound, FlexibleJavaClassifierTypeCapabilities), CustomTypeVariable, Specificity { - private class Impl(val flexibility: Flexibility) : CustomTypeVariable, Specificity { - - private val lowerBound: KotlinType get() = flexibility.lowerBound - private val upperBound: KotlinType get() = flexibility.upperBound + override fun getCapability(capabilityClass: Class): T? { + @Suppress("UNCHECKED_CAST") + return when (capabilityClass) { + CustomTypeVariable::class.java, Specificity::class.java -> this as T + else -> super.getCapability(capabilityClass) + } + } override val isTypeVariable: Boolean = lowerBound.getConstructor() == upperBound.getConstructor() && lowerBound.getConstructor().getDeclarationDescriptor() is TypeParameterDescriptor diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt index 0f40b313877..1b284efb223 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,21 +33,17 @@ interface Dynamicity : TypeCapability fun KotlinType.isDynamic(): Boolean = this.getCapability(Dynamicity::class.java) != null -fun createDynamicType(builtIns: KotlinBuiltIns) = object : DelegatingFlexibleType( - builtIns.nothingType, - builtIns.nullableAnyType, - DynamicTypeCapabilities -) {} +fun createDynamicType(builtIns: KotlinBuiltIns) = DynamicTypeCapabilities.createFlexibleType(builtIns.nothingType, builtIns.nullableAnyType) object DynamicTypeCapabilities : FlexibleTypeCapabilities { override val id: String get() = "kotlin.DynamicType" - override fun getCapability(capabilityClass: Class, jetType: KotlinType, flexibility: Flexibility): T? { - @Suppress("UNCHECKED_CAST") - return if (capabilityClass in Impl.capabilityClasses) Impl(flexibility) as T else null + override fun createFlexibleType(lowerBound: KotlinType, upperBound: KotlinType): KotlinType { + if (lowerBound == upperBound) return lowerBound + return Impl(lowerBound, upperBound) } - private class Impl(flexibility: Flexibility) : Dynamicity, Specificity, NullAwareness, FlexibleTypeDelegation { + private class Impl(lowerBound: KotlinType, upperBound: KotlinType) : DelegatingFlexibleType(lowerBound, upperBound, DynamicTypeCapabilities), Dynamicity, Specificity, NullAwareness, FlexibleTypeDelegation { companion object { internal val capabilityClasses = hashSetOf( Dynamicity::class.java, @@ -57,7 +53,14 @@ object DynamicTypeCapabilities : FlexibleTypeCapabilities { ) } - override val delegateType: KotlinType = flexibility.upperBound + override fun getCapability(capabilityClass: Class): T? { + @Suppress("UNCHECKED_CAST") + if (capabilityClass in capabilityClasses) return this as T + + return super.getCapability(capabilityClass) + } + + override val delegateType: KotlinType get() = upperBound override fun getSpecificityRelationTo(otherType: KotlinType): Specificity.Relation { return if (!otherType.isDynamic()) Specificity.Relation.LESS_SPECIFIC else Specificity.Relation.DONT_KNOW diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt index 68f03987fea..a6224245c5b 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,15 +18,18 @@ package org.jetbrains.kotlin.types import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.types.checker.KotlinTypeChecker interface FlexibleTypeCapabilities { - fun getCapability(capabilityClass: Class, jetType: KotlinType, flexibility: Flexibility): T? + fun createFlexibleType(lowerBound: KotlinType, upperBound: KotlinType): KotlinType val id: String object NONE : FlexibleTypeCapabilities { - override fun getCapability(capabilityClass: Class, jetType: KotlinType, flexibility: Flexibility): T? = null + override fun createFlexibleType(lowerBound: KotlinType, upperBound: KotlinType): KotlinType { + if (lowerBound == upperBound) return lowerBound + return object : DelegatingFlexibleType(lowerBound, upperBound, this@NONE) {} + } + override val id: String get() = "NONE" } } @@ -124,8 +127,7 @@ open class DelegatingFlexibleType protected constructor( @JvmStatic fun create(lowerBound: KotlinType, upperBound: KotlinType, extraCapabilities: FlexibleTypeCapabilities): KotlinType { - if (lowerBound == upperBound) return lowerBound - return DelegatingFlexibleType(lowerBound, upperBound, extraCapabilities) + return extraCapabilities.createFlexibleType(lowerBound, upperBound) } @JvmField @@ -153,9 +155,6 @@ open class DelegatingFlexibleType protected constructor( } override fun getCapability(capabilityClass: Class): T? { - val extra = extraCapabilities.getCapability(capabilityClass, this, this) - if (extra != null) return extra - @Suppress("UNCHECKED_CAST") if (capabilityClass in capabilityClasses) return this as T