[Commonizer] CirCommonClassifierIdResolver: Implement caching as composition
^KT-48288
This commit is contained in:
committed by
Space
parent
97befd0520
commit
d2627f0a76
+37
-10
@@ -16,27 +16,55 @@ import org.jetbrains.kotlin.commonizer.cir.CirTypeAlias
|
|||||||
internal fun CirCommonClassifierIdResolver(
|
internal fun CirCommonClassifierIdResolver(
|
||||||
classifierIndices: TargetDependent<CirClassifierIndex>,
|
classifierIndices: TargetDependent<CirClassifierIndex>,
|
||||||
targetDependencies: TargetDependent<CirProvidedClassifiers>,
|
targetDependencies: TargetDependent<CirProvidedClassifiers>,
|
||||||
commonDependencies: CirProvidedClassifiers = CirProvidedClassifiers.EMPTY
|
commonDependencies: CirProvidedClassifiers = CirProvidedClassifiers.EMPTY,
|
||||||
|
cache: CirCommonClassifierIdResolverCache = CirCommonClassifierIdResolverCache.create()
|
||||||
): CirCommonClassifierIdResolver {
|
): CirCommonClassifierIdResolver {
|
||||||
return CirCommonClassifierIdResolverImpl(classifierIndices, targetDependencies, commonDependencies)
|
return CirCommonClassifierIdResolverImpl(classifierIndices, targetDependencies, commonDependencies, cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CirCommonClassifierIdResolver {
|
interface CirCommonClassifierIdResolver {
|
||||||
fun findCommonId(id: CirEntityId): CirCommonClassifierId?
|
fun findCommonId(id: CirEntityId): CirCommonClassifierId?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal interface CirCommonClassifierIdResolverCache {
|
||||||
|
|
||||||
|
operator fun set(id: CirEntityId, result: CirCommonClassifierId?)
|
||||||
|
operator fun get(id: CirEntityId): CirCommonClassifierId?
|
||||||
|
|
||||||
|
object None : CirCommonClassifierIdResolverCache {
|
||||||
|
override fun set(id: CirEntityId, result: CirCommonClassifierId?) = Unit
|
||||||
|
override fun get(id: CirEntityId): CirCommonClassifierId? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Default : CirCommonClassifierIdResolverCache {
|
||||||
|
private val cachedResults = THashMap<CirEntityId, CirCommonClassifierId>()
|
||||||
|
private val cachedNullResults = THashSet<CirEntityId>()
|
||||||
|
|
||||||
|
override fun set(id: CirEntityId, result: CirCommonClassifierId?) {
|
||||||
|
if (result == null) cachedNullResults.add(id)
|
||||||
|
else cachedResults[id] = result
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun get(id: CirEntityId): CirCommonClassifierId? {
|
||||||
|
if (id in cachedNullResults) return null
|
||||||
|
return cachedResults[id]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun create(): CirCommonClassifierIdResolverCache = Default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class CirCommonClassifierIdResolverImpl(
|
private class CirCommonClassifierIdResolverImpl(
|
||||||
private val classifierIndices: TargetDependent<CirClassifierIndex>,
|
private val classifierIndices: TargetDependent<CirClassifierIndex>,
|
||||||
private val targetDependencies: TargetDependent<CirProvidedClassifiers>,
|
private val targetDependencies: TargetDependent<CirProvidedClassifiers>,
|
||||||
private val commonDependencies: CirProvidedClassifiers
|
private val commonDependencies: CirProvidedClassifiers,
|
||||||
|
private val cache: CirCommonClassifierIdResolverCache
|
||||||
) : CirCommonClassifierIdResolver {
|
) : CirCommonClassifierIdResolver {
|
||||||
|
|
||||||
private val cachedResults = THashMap<CirEntityId, CirCommonClassifierId>()
|
|
||||||
private val cachedNullResults = THashSet<CirEntityId>()
|
|
||||||
|
|
||||||
override fun findCommonId(id: CirEntityId): CirCommonClassifierId? {
|
override fun findCommonId(id: CirEntityId): CirCommonClassifierId? {
|
||||||
cachedResults[id]?.let { return it }
|
cache[id]?.let { return it }
|
||||||
if (id in cachedNullResults) return null
|
|
||||||
return doFindCommonId(id)
|
return doFindCommonId(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,8 +133,7 @@ private class CirCommonClassifierIdResolverImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val result = if (results.isNotEmpty()) CirCommonClassifierId(results) else null
|
val result = if (results.isNotEmpty()) CirCommonClassifierId(results) else null
|
||||||
if (result != null) visited.forEach { cachedResults[it] = result }
|
visited.forEach { visitedId -> cache[visitedId] = result }
|
||||||
else visited.forEach { cachedNullResults.add(it) }
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-1
@@ -22,7 +22,6 @@ interface CirProvidedClassifiers {
|
|||||||
fun hasClassifier(classifierId: CirEntityId): Boolean
|
fun hasClassifier(classifierId: CirEntityId): Boolean
|
||||||
fun classifier(classifierId: CirEntityId): CirProvided.Classifier?
|
fun classifier(classifierId: CirEntityId): CirProvided.Classifier?
|
||||||
|
|
||||||
// TODO NOW: Test
|
|
||||||
fun findTypeAliasesWithUnderlyingType(underlyingClassifier: CirEntityId): List<CirEntityId>
|
fun findTypeAliasesWithUnderlyingType(underlyingClassifier: CirEntityId): List<CirEntityId>
|
||||||
|
|
||||||
object EMPTY : CirProvidedClassifiers {
|
object EMPTY : CirProvidedClassifiers {
|
||||||
|
|||||||
+3
-1
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.commonizer
|
|||||||
import org.jetbrains.kotlin.commonizer.cir.CirEntityId
|
import org.jetbrains.kotlin.commonizer.cir.CirEntityId
|
||||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirClassifierIndex
|
import org.jetbrains.kotlin.commonizer.mergedtree.CirClassifierIndex
|
||||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirCommonClassifierIdResolver
|
import org.jetbrains.kotlin.commonizer.mergedtree.CirCommonClassifierIdResolver
|
||||||
|
import org.jetbrains.kotlin.commonizer.mergedtree.CirCommonClassifierIdResolverCache
|
||||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirProvidedClassifiers
|
import org.jetbrains.kotlin.commonizer.mergedtree.CirProvidedClassifiers
|
||||||
import org.jetbrains.kotlin.commonizer.tree.CirTreeRoot
|
import org.jetbrains.kotlin.commonizer.tree.CirTreeRoot
|
||||||
import org.jetbrains.kotlin.commonizer.utils.createCirProvidedClassifiers
|
import org.jetbrains.kotlin.commonizer.utils.createCirProvidedClassifiers
|
||||||
@@ -353,7 +354,8 @@ private fun createCommonClassifierIdResolver(
|
|||||||
.mapValue(::CirClassifierIndex),
|
.mapValue(::CirClassifierIndex),
|
||||||
targetDependencies = root.withIndex()
|
targetDependencies = root.withIndex()
|
||||||
.associate { (index, _) -> LeafCommonizerTarget(index.toString()) to CirProvidedClassifiers.EMPTY }.toTargetDependent(),
|
.associate { (index, _) -> LeafCommonizerTarget(index.toString()) to CirProvidedClassifiers.EMPTY }.toTargetDependent(),
|
||||||
commonDependencies = dependencies
|
commonDependencies = dependencies,
|
||||||
|
cache = CirCommonClassifierIdResolverCache.None
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user