Skip to content

Commit

Permalink
Optimize repetitive calls to hasConstraints in presence of forks
Browse files Browse the repository at this point in the history
^KT-68768 Fixed
  • Loading branch information
dzharkov authored and Space Team committed Nov 8, 2024
1 parent 26003c2 commit 3b02853
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class ConstraintInjector(

fun resolveForkPointsConstraints()

fun onNewConstraintOrForkPoint()

fun recordTypeVariableReferenceInConstraint(
constraintOwner: TypeConstructorMarker,
referencedVariable: TypeConstructorMarker,
Expand Down Expand Up @@ -180,6 +182,8 @@ class ConstraintInjector(
typeCheckerState.position to forkPointData
}

c.onNewConstraintOrForkPoint()

// During completion, we start processing fork constrains immediately
if (c.atCompletionState) {
c.resolveForkPointsConstraints()
Expand Down Expand Up @@ -240,6 +244,7 @@ class ConstraintInjector(
}

if (wasAdded) {
c.onNewConstraintOrForkPoint()
recordReferencesOfOtherTypeVariableInConstraint(c, constraint, typeVariableConstructor)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class NewConstraintSystemImpl(
private val properTypesCache: MutableSet<KotlinTypeMarker> = SmartSet.create()
private val notProperTypesCache: MutableSet<KotlinTypeMarker> = SmartSet.create()
private val intersectionTypesCache: MutableMap<Collection<KotlinTypeMarker>, EmptyIntersectionTypeInfo?> = mutableMapOf()

// Cached value that should be reset on each new constraint or fork point
private var hasContradictionInForkPointsCache: Boolean? = null

override var typeVariablesThatAreCountedAsProperTypes: Set<TypeConstructorMarker>? = null

private var couldBeResolvedWithUnrestrictedBuilderInference: Boolean = false
Expand Down Expand Up @@ -392,6 +396,7 @@ class NewConstraintSystemImpl(
storage.postponedTypeVariables.addAll(otherSystem.postponedTypeVariables)
storage.constraintsFromAllForkPoints.addAll(otherSystem.constraintsFromAllForkPoints)

hasContradictionInForkPointsCache = null
}

@AssertionsOnly
Expand Down Expand Up @@ -524,6 +529,10 @@ class NewConstraintSystemImpl(
}
}

override fun onNewConstraintOrForkPoint() {
hasContradictionInForkPointsCache = null
}

/**
* Checks if the current state of forked constraints is not contradictory.
*
Expand All @@ -537,6 +546,8 @@ class NewConstraintSystemImpl(

if (constraintsFromAllForkPoints.isEmpty()) return false

hasContradictionInForkPointsCache?.let { return it }

val allForkPointsData = constraintsFromAllForkPoints.toList()
constraintsFromAllForkPoints.clear()

Expand All @@ -551,7 +562,7 @@ class NewConstraintSystemImpl(

constraintsFromAllForkPoints.addAll(allForkPointsData)

return isThereAnyUnsuccessful
return isThereAnyUnsuccessful.also { hasContradictionInForkPointsCache = it }
}

/**
Expand All @@ -568,7 +579,7 @@ class NewConstraintSystemImpl(
runTransaction {
applyForkPointBranch(constraintSetForForkBranch, position)

!hasContradiction
!storage.hasContradiction
}
}

Expand Down

0 comments on commit 3b02853

Please sign in to comment.