Optimization. Do not create objects for flexible type capabilities.

This commit is contained in:
Stanislav Erokhin
2016-04-21 19:53:44 +03:00
parent 75d7390a94
commit c25e2e34a2
3 changed files with 36 additions and 31 deletions
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 val id: String get() = "kotlin.jvm.PlatformType"
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>, jetType: KotlinType, flexibility: Flexibility): T? { override fun createFlexibleType(lowerBound: KotlinType, upperBound: KotlinType): KotlinType {
@Suppress("UNCHECKED_CAST") if (lowerBound == upperBound) return lowerBound
return when (capabilityClass) {
CustomTypeVariable::class.java, Specificity::class.java -> Impl(flexibility) as T return Impl(lowerBound, upperBound)
else -> null
}
} }
private class Impl(lowerBound: KotlinType, upperBound: KotlinType) :
DelegatingFlexibleType(lowerBound, upperBound, FlexibleJavaClassifierTypeCapabilities), CustomTypeVariable, Specificity {
private class Impl(val flexibility: Flexibility) : CustomTypeVariable, Specificity { override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
@Suppress("UNCHECKED_CAST")
private val lowerBound: KotlinType get() = flexibility.lowerBound return when (capabilityClass) {
private val upperBound: KotlinType get() = flexibility.upperBound CustomTypeVariable::class.java, Specificity::class.java -> this as T
else -> super.getCapability(capabilityClass)
}
}
override val isTypeVariable: Boolean = lowerBound.getConstructor() == upperBound.getConstructor() override val isTypeVariable: Boolean = lowerBound.getConstructor() == upperBound.getConstructor()
&& lowerBound.getConstructor().getDeclarationDescriptor() is TypeParameterDescriptor && lowerBound.getConstructor().getDeclarationDescriptor() is TypeParameterDescriptor
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 KotlinType.isDynamic(): Boolean = this.getCapability(Dynamicity::class.java) != null
fun createDynamicType(builtIns: KotlinBuiltIns) = object : DelegatingFlexibleType( fun createDynamicType(builtIns: KotlinBuiltIns) = DynamicTypeCapabilities.createFlexibleType(builtIns.nothingType, builtIns.nullableAnyType)
builtIns.nothingType,
builtIns.nullableAnyType,
DynamicTypeCapabilities
) {}
object DynamicTypeCapabilities : FlexibleTypeCapabilities { object DynamicTypeCapabilities : FlexibleTypeCapabilities {
override val id: String get() = "kotlin.DynamicType" override val id: String get() = "kotlin.DynamicType"
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>, jetType: KotlinType, flexibility: Flexibility): T? { override fun createFlexibleType(lowerBound: KotlinType, upperBound: KotlinType): KotlinType {
@Suppress("UNCHECKED_CAST") if (lowerBound == upperBound) return lowerBound
return if (capabilityClass in Impl.capabilityClasses) Impl(flexibility) as T else null 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 { companion object {
internal val capabilityClasses = hashSetOf( internal val capabilityClasses = hashSetOf(
Dynamicity::class.java, Dynamicity::class.java,
@@ -57,7 +53,14 @@ object DynamicTypeCapabilities : FlexibleTypeCapabilities {
) )
} }
override val delegateType: KotlinType = flexibility.upperBound override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): 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 { override fun getSpecificityRelationTo(otherType: KotlinType): Specificity.Relation {
return if (!otherType.isDynamic()) Specificity.Relation.LESS_SPECIFIC else Specificity.Relation.DONT_KNOW return if (!otherType.isDynamic()) Specificity.Relation.LESS_SPECIFIC else Specificity.Relation.DONT_KNOW
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.ClassId
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
interface FlexibleTypeCapabilities { interface FlexibleTypeCapabilities {
fun <T: TypeCapability> getCapability(capabilityClass: Class<T>, jetType: KotlinType, flexibility: Flexibility): T? fun createFlexibleType(lowerBound: KotlinType, upperBound: KotlinType): KotlinType
val id: String val id: String
object NONE : FlexibleTypeCapabilities { object NONE : FlexibleTypeCapabilities {
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>, 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" override val id: String get() = "NONE"
} }
} }
@@ -124,8 +127,7 @@ open class DelegatingFlexibleType protected constructor(
@JvmStatic @JvmStatic
fun create(lowerBound: KotlinType, upperBound: KotlinType, extraCapabilities: FlexibleTypeCapabilities): KotlinType { fun create(lowerBound: KotlinType, upperBound: KotlinType, extraCapabilities: FlexibleTypeCapabilities): KotlinType {
if (lowerBound == upperBound) return lowerBound return extraCapabilities.createFlexibleType(lowerBound, upperBound)
return DelegatingFlexibleType(lowerBound, upperBound, extraCapabilities)
} }
@JvmField @JvmField
@@ -153,9 +155,6 @@ open class DelegatingFlexibleType protected constructor(
} }
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? { override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
val extra = extraCapabilities.getCapability(capabilityClass, this, this)
if (extra != null) return extra
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
if (capabilityClass in capabilityClasses) return this as T if (capabilityClass in capabilityClasses) return this as T