Optimization. Do not create objects for flexible type capabilities.
This commit is contained in:
+14
-11
@@ -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 <T : TypeCapability> getCapability(capabilityClass: Class<T>, 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 <T : TypeCapability> getCapability(capabilityClass: Class<T>): 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
|
||||
|
||||
@@ -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 <T : TypeCapability> getCapability(capabilityClass: Class<T>, 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 <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 {
|
||||
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");
|
||||
* 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 <T: TypeCapability> getCapability(capabilityClass: Class<T>, jetType: KotlinType, flexibility: Flexibility): T?
|
||||
fun createFlexibleType(lowerBound: KotlinType, upperBound: KotlinType): KotlinType
|
||||
val id: String
|
||||
|
||||
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"
|
||||
}
|
||||
}
|
||||
@@ -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 <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
|
||||
val extra = extraCapabilities.getCapability(capabilityClass, this, this)
|
||||
if (extra != null) return extra
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
if (capabilityClass in capabilityClasses) return this as T
|
||||
|
||||
|
||||
Reference in New Issue
Block a user