[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(
|
||||
classifierIndices: TargetDependent<CirClassifierIndex>,
|
||||
targetDependencies: TargetDependent<CirProvidedClassifiers>,
|
||||
commonDependencies: CirProvidedClassifiers = CirProvidedClassifiers.EMPTY
|
||||
commonDependencies: CirProvidedClassifiers = CirProvidedClassifiers.EMPTY,
|
||||
cache: CirCommonClassifierIdResolverCache = CirCommonClassifierIdResolverCache.create()
|
||||
): CirCommonClassifierIdResolver {
|
||||
return CirCommonClassifierIdResolverImpl(classifierIndices, targetDependencies, commonDependencies)
|
||||
return CirCommonClassifierIdResolverImpl(classifierIndices, targetDependencies, commonDependencies, cache)
|
||||
}
|
||||
|
||||
interface CirCommonClassifierIdResolver {
|
||||
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 val classifierIndices: TargetDependent<CirClassifierIndex>,
|
||||
private val targetDependencies: TargetDependent<CirProvidedClassifiers>,
|
||||
private val commonDependencies: CirProvidedClassifiers
|
||||
private val commonDependencies: CirProvidedClassifiers,
|
||||
private val cache: CirCommonClassifierIdResolverCache
|
||||
) : CirCommonClassifierIdResolver {
|
||||
|
||||
private val cachedResults = THashMap<CirEntityId, CirCommonClassifierId>()
|
||||
private val cachedNullResults = THashSet<CirEntityId>()
|
||||
|
||||
override fun findCommonId(id: CirEntityId): CirCommonClassifierId? {
|
||||
cachedResults[id]?.let { return it }
|
||||
if (id in cachedNullResults) return null
|
||||
cache[id]?.let { return it }
|
||||
return doFindCommonId(id)
|
||||
}
|
||||
|
||||
@@ -105,8 +133,7 @@ private class CirCommonClassifierIdResolverImpl(
|
||||
}
|
||||
|
||||
val result = if (results.isNotEmpty()) CirCommonClassifierId(results) else null
|
||||
if (result != null) visited.forEach { cachedResults[it] = result }
|
||||
else visited.forEach { cachedNullResults.add(it) }
|
||||
visited.forEach { visitedId -> cache[visitedId] = result }
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
-1
@@ -22,7 +22,6 @@ interface CirProvidedClassifiers {
|
||||
fun hasClassifier(classifierId: CirEntityId): Boolean
|
||||
fun classifier(classifierId: CirEntityId): CirProvided.Classifier?
|
||||
|
||||
// TODO NOW: Test
|
||||
fun findTypeAliasesWithUnderlyingType(underlyingClassifier: CirEntityId): List<CirEntityId>
|
||||
|
||||
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.mergedtree.CirClassifierIndex
|
||||
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.tree.CirTreeRoot
|
||||
import org.jetbrains.kotlin.commonizer.utils.createCirProvidedClassifiers
|
||||
@@ -353,7 +354,8 @@ private fun createCommonClassifierIdResolver(
|
||||
.mapValue(::CirClassifierIndex),
|
||||
targetDependencies = root.withIndex()
|
||||
.associate { (index, _) -> LeafCommonizerTarget(index.toString()) to CirProvidedClassifiers.EMPTY }.toTargetDependent(),
|
||||
commonDependencies = dependencies
|
||||
commonDependencies = dependencies,
|
||||
cache = CirCommonClassifierIdResolverCache.None
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user