Optimize ConstraintInjector::isMyTypeVariable

Do not look into the map for constructors that are not
type variables
This commit is contained in:
Denis Zharkov
2019-06-05 14:51:13 +03:00
parent b43f717f6d
commit 3c8ed21e59
5 changed files with 24 additions and 8 deletions
@@ -246,6 +246,11 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext,
return this.typeConstructor return this.typeConstructor
} }
override fun KotlinTypeMarker.mayBeTypeVariable(): Boolean {
require(this is ConeKotlinType)
return this is ConeTypeVariableType
}
override fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker { override fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker {
require(this is ConeCapturedType) require(this is ConeCapturedType)
return this.constructor.projection return this.constructor.projection
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstituto
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutorByConstructorMap import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutorByConstructorMap
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableTypeConstructor
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext
import org.jetbrains.kotlin.types.checker.NewCapturedType import org.jetbrains.kotlin.types.checker.NewCapturedType
@@ -32,6 +33,11 @@ class ClassicTypeSystemContextForCS(override val builtIns: KotlinBuiltIns) : Typ
return this.freshTypeConstructor return this.freshTypeConstructor
} }
override fun KotlinTypeMarker.mayBeTypeVariable(): Boolean {
require(this is KotlinType, this::errorMessage)
return this.constructor is TypeVariableTypeConstructor
}
override fun createCapturedType( override fun createCapturedType(
constructorProjection: TypeArgumentMarker, constructorProjection: TypeArgumentMarker,
constructorSupertypes: List<KotlinTypeMarker>, constructorSupertypes: List<KotlinTypeMarker>,
@@ -143,7 +143,8 @@ class ConstraintInjector(val constraintIncorporator: ConstraintIncorporator, val
} }
// from AbstractTypeCheckerContextForConstraintSystem // from AbstractTypeCheckerContextForConstraintSystem
override fun isMyTypeVariable(type: SimpleTypeMarker): Boolean = c.allTypeVariables.containsKey(type.typeConstructor()) override fun isMyTypeVariable(type: SimpleTypeMarker): Boolean =
type.mayBeTypeVariable() && c.allTypeVariables.containsKey(type.typeConstructor())
override fun addUpperConstraint(typeVariable: TypeConstructorMarker, superType: KotlinTypeMarker) = override fun addUpperConstraint(typeVariable: TypeConstructorMarker, superType: KotlinTypeMarker) =
addConstraint(typeVariable, superType, UPPER) addConstraint(typeVariable, superType, UPPER)
@@ -11,18 +11,17 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.calls.inference.CapturedType import org.jetbrains.kotlin.resolve.calls.inference.CapturedType
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
import org.jetbrains.kotlin.resolve.descriptorUtil.hasExactAnnotation import org.jetbrains.kotlin.resolve.descriptorUtil.hasExactAnnotation
import org.jetbrains.kotlin.resolve.descriptorUtil.hasNoInferAnnotation import org.jetbrains.kotlin.resolve.descriptorUtil.hasNoInferAnnotation
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
import org.jetbrains.kotlin.resolve.descriptorUtil.isExactAnnotation import org.jetbrains.kotlin.resolve.descriptorUtil.isExactAnnotation
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.types.model.CaptureStatus
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
import org.jetbrains.kotlin.types.typeUtil.contains import org.jetbrains.kotlin.types.typeUtil.contains
import kotlin.math.max
import kotlin.contracts.ExperimentalContracts import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract import kotlin.contracts.contract
import kotlin.math.max
interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext { interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext {
override fun TypeConstructorMarker.isDenotable(): Boolean { override fun TypeConstructorMarker.isDenotable(): Boolean {
@@ -376,6 +375,10 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext {
errorSupportedOnlyInTypeInference() errorSupportedOnlyInTypeInference()
} }
override fun KotlinTypeMarker.mayBeTypeVariable(): Boolean {
errorSupportedOnlyInTypeInference()
}
override fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker { override fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker {
require(this is NewCapturedType, this::errorMessage) require(this is NewCapturedType, this::errorMessage)
return this.constructor.projection return this.constructor.projection
@@ -122,6 +122,7 @@ interface TypeSystemInferenceExtensionContext : TypeSystemContext, TypeSystemBui
fun TypeVariableMarker.freshTypeConstructor(): TypeConstructorMarker fun TypeVariableMarker.freshTypeConstructor(): TypeConstructorMarker
fun KotlinTypeMarker.mayBeTypeVariable(): Boolean
fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker
fun CapturedTypeMarker.captureStatus(): CaptureStatus fun CapturedTypeMarker.captureStatus(): CaptureStatus