From 357ceeae19541c9c527b204059d4b148ff299d24 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Fri, 6 Mar 2020 14:18:53 +0100 Subject: [PATCH] Avoid unnecessary freshTypeConstructor() calls --- .../calls/components/CompletionModeCalculator.kt | 3 ++- .../inference/components/ConstraintIncorporator.kt | 6 ++++-- .../TypeVariableDependencyInformationProvider.kt | 3 ++- .../inference/model/NewConstraintSystemImpl.kt | 13 ++++++++----- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CompletionModeCalculator.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CompletionModeCalculator.kt index c3a3cc42e4f..0dc000b6956 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CompletionModeCalculator.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CompletionModeCalculator.kt @@ -229,8 +229,9 @@ class CompletionModeCalculator { constraint: Constraint, variable: TypeVariableMarker ): Boolean { + val defaultType = variable.defaultType() return constraint.kind.isLower() && postponedAtoms.any { atom -> - atom.expectedType?.contains { type -> variable.defaultType() == type } ?: false + atom.expectedType?.contains { type -> defaultType == type } ?: false } } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintIncorporator.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintIncorporator.kt index 20c929292ec..c15ecf43fb1 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintIncorporator.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintIncorporator.kt @@ -41,7 +41,8 @@ class ConstraintIncorporator( fun incorporate(c: Context, typeVariable: TypeVariableMarker, constraint: Constraint) { // we shouldn't incorporate recursive constraint -- It is too dangerous with(c) { - if (constraint.type.contains { it.typeConstructor() == typeVariable.freshTypeConstructor() }) return + val freshTypeConstructor = typeVariable.freshTypeConstructor() + if (constraint.type.contains { it.typeConstructor() == freshTypeConstructor }) return } c.directWithVariable(typeVariable, constraint) @@ -101,9 +102,10 @@ class ConstraintIncorporator( typeVariable: TypeVariableMarker, constraint: Constraint ) { + val freshTypeConstructor = typeVariable.freshTypeConstructor() for (typeVariableWithConstraint in this@insideOtherConstraint.allTypeVariablesWithConstraints) { val constraintsWhichConstraintMyVariable = typeVariableWithConstraint.constraints.filter { - it.type.contains { it.typeConstructor() == typeVariable.freshTypeConstructor() } + it.type.contains { it.typeConstructor() == freshTypeConstructor } } constraintsWhichConstraintMyVariable.forEach { generateNewConstraint(typeVariableWithConstraint.typeVariable, it, typeVariable, constraint) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeVariableDependencyInformationProvider.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeVariableDependencyInformationProvider.kt index 899bfe93c25..75c6318c7f3 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeVariableDependencyInformationProvider.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeVariableDependencyInformationProvider.kt @@ -111,7 +111,8 @@ class TypeVariableDependencyInformationProvider( private fun KotlinTypeMarker.forAllMyTypeVariables(action: (TypeConstructorMarker) -> Unit) = with(typeSystemContext) { contains { - if (isMyTypeVariable(it.typeConstructor())) action(it.typeConstructor()) + val typeConstructor = it.typeConstructor() + if (isMyTypeVariable(typeConstructor)) action(typeConstructor) false } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index e40e8823075..b2032cc429a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -298,16 +298,18 @@ class NewConstraintSystemImpl( this, variable.defaultType(), resultType, FixVariableConstraintPosition(variable, atom) ) - val variableWithConstraints = notFixedTypeVariables.remove(variable.freshTypeConstructor()) + val freshTypeConstructor = variable.freshTypeConstructor() + + val variableWithConstraints = notFixedTypeVariables.remove(freshTypeConstructor) checkOnlyInputTypesAnnotation(variableWithConstraints, resultType) for (variableWithConstraint in notFixedTypeVariables.values) { variableWithConstraint.removeConstrains { - it.type.contains { it.typeConstructor() == variable.freshTypeConstructor() } + it.type.contains { it.typeConstructor() == freshTypeConstructor } } } - storage.fixedTypeVariables[variable.freshTypeConstructor()] = resultType + storage.fixedTypeVariables[freshTypeConstructor] = resultType } private fun checkOnlyInputTypesAnnotation( @@ -336,8 +338,9 @@ class NewConstraintSystemImpl( override fun containsOnlyFixedOrPostponedVariables(type: KotlinTypeMarker): Boolean { checkState(State.BUILDING, State.COMPLETION) return !type.contains { - val variable = storage.notFixedTypeVariables[it.typeConstructor()]?.typeVariable - variable !in storage.postponedTypeVariables && storage.notFixedTypeVariables.containsKey(it.typeConstructor()) + val typeConstructor = it.typeConstructor() + val variable = storage.notFixedTypeVariables[typeConstructor]?.typeVariable + variable !in storage.postponedTypeVariables && storage.notFixedTypeVariables.containsKey(typeConstructor) } }