[LL FIR] optimize FirCacheValue, do not create ConcurrentHashMap to store a single value
^KTIJ-24640 ^KTIJ-24641
This commit is contained in:
committed by
Space Team
parent
57da858810
commit
4c3672de0a
+2
-2
@@ -85,11 +85,11 @@ abstract class FirDeclarationGenerationExtension(session: FirSession) : FirExten
|
||||
}
|
||||
|
||||
@FirExtensionApiInternals
|
||||
val topLevelClassIdsCache: FirLazyValue<Set<ClassId>, Nothing?> =
|
||||
val topLevelClassIdsCache: FirLazyValue<Set<ClassId>> =
|
||||
session.firCachesFactory.createLazyValue { getTopLevelClassIds() }
|
||||
|
||||
@FirExtensionApiInternals
|
||||
val topLevelCallableIdsCache: FirLazyValue<Set<CallableId>, Nothing?> =
|
||||
val topLevelCallableIdsCache: FirLazyValue<Set<CallableId>> =
|
||||
session.firCachesFactory.createLazyValue { getTopLevelCallableIds() }
|
||||
|
||||
}
|
||||
|
||||
+5
-5
@@ -53,7 +53,7 @@ class FirExtensionDeclarationsSymbolProvider private constructor(
|
||||
hasPackage(packageFqName)
|
||||
}
|
||||
|
||||
private val callableNamesInPackageCache: FirLazyValue<Map<FqName, Set<Name>>, Nothing?> =
|
||||
private val callableNamesInPackageCache: FirLazyValue<Map<FqName, Set<Name>>> =
|
||||
cachesFactory.createLazyValue {
|
||||
computeNamesGroupedByPackage(
|
||||
FirDeclarationGenerationExtension::getTopLevelCallableIds,
|
||||
@@ -61,7 +61,7 @@ class FirExtensionDeclarationsSymbolProvider private constructor(
|
||||
)
|
||||
}
|
||||
|
||||
private val classNamesInPackageCache: FirLazyValue<Map<FqName, Set<String>>, Nothing?> =
|
||||
private val classNamesInPackageCache: FirLazyValue<Map<FqName, Set<String>>> =
|
||||
cachesFactory.createLazyValue {
|
||||
computeNamesGroupedByPackage(
|
||||
FirDeclarationGenerationExtension::getTopLevelClassIds,
|
||||
@@ -82,12 +82,12 @@ class FirExtensionDeclarationsSymbolProvider private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private val extensionsByTopLevelClassId: FirLazyValue<Map<ClassId, List<FirDeclarationGenerationExtension>>, Nothing?> =
|
||||
private val extensionsByTopLevelClassId: FirLazyValue<Map<ClassId, List<FirDeclarationGenerationExtension>>> =
|
||||
session.firCachesFactory.createLazyValue {
|
||||
extensions.flatGroupBy { it.topLevelClassIdsCache.getValue() }
|
||||
}
|
||||
|
||||
private val extensionsByTopLevelCallableId: FirLazyValue<Map<CallableId, List<FirDeclarationGenerationExtension>>, Nothing?> =
|
||||
private val extensionsByTopLevelCallableId: FirLazyValue<Map<CallableId, List<FirDeclarationGenerationExtension>>> =
|
||||
session.firCachesFactory.createLazyValue {
|
||||
extensions.flatGroupBy { it.topLevelCallableIdsCache.getValue() }
|
||||
}
|
||||
@@ -171,7 +171,7 @@ class FirExtensionDeclarationsSymbolProvider private constructor(
|
||||
|
||||
override fun computePackageSetWithTopLevelCallables(): Set<String> =
|
||||
extensions.flatMapTo(mutableSetOf()) { extension ->
|
||||
extension.topLevelCallableIdsCache.getValue(null).map { it.packageName.asString() }
|
||||
extension.topLevelCallableIdsCache.getValue().map { it.packageName.asString() }
|
||||
}
|
||||
|
||||
override fun knownTopLevelClassifiersInPackage(packageFqName: FqName): Set<String> =
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ class FirGeneratedClassDeclaredMemberScope private constructor(
|
||||
generateMemberProperties(callableId)
|
||||
}
|
||||
|
||||
private val constructorCache: FirLazyValue<List<FirConstructorSymbol>, Nothing?> = firCachesFactory.createLazyValue {
|
||||
private val constructorCache: FirLazyValue<List<FirConstructorSymbol>> = firCachesFactory.createLazyValue {
|
||||
generateConstructors()
|
||||
}
|
||||
|
||||
|
||||
@@ -20,17 +20,10 @@ operator fun <K : Any, V> FirCache<K, V, Nothing>.contains(key: K): Boolean {
|
||||
return getValueIfComputed(key) != null
|
||||
}
|
||||
|
||||
class FirLazyValue<out V, in CONTEXT>(private val cache: FirCache<Unit, V, CONTEXT>) {
|
||||
fun getValue(context: CONTEXT): V {
|
||||
return cache.getValue(Unit, context)
|
||||
}
|
||||
abstract class FirLazyValue<out V> {
|
||||
abstract fun getValue(): V
|
||||
}
|
||||
|
||||
operator fun <V> FirLazyValue<V, Nothing?>.getValue(thisRef: Any?, property: KProperty<*>): V {
|
||||
return getValue(context = null)
|
||||
}
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <V> FirLazyValue<V, Nothing?>.getValue(): V {
|
||||
return getValue(null)
|
||||
}
|
||||
operator fun <V> FirLazyValue<V>.getValue(thisRef: Any?, property: KProperty<*>): V {
|
||||
return getValue()
|
||||
}
|
||||
@@ -59,9 +59,7 @@ abstract class FirCachesFactory : FirSessionComponent {
|
||||
postCompute: (K, V, DATA) -> Unit
|
||||
): FirCache<K, V, CONTEXT>
|
||||
|
||||
fun <V, CONTEXT> createLazyValue(createValue: (CONTEXT) -> V): FirLazyValue<V, CONTEXT> {
|
||||
return FirLazyValue(createCache { _, context -> createValue(context) })
|
||||
}
|
||||
abstract fun <V> createLazyValue(createValue: () -> V): FirLazyValue<V>
|
||||
}
|
||||
|
||||
val FirSession.firCachesFactory: FirCachesFactory by FirSession.sessionComponentAccessor()
|
||||
|
||||
@@ -24,6 +24,9 @@ object FirThreadUnsafeCachesFactory : FirCachesFactory() {
|
||||
postCompute: (K, V, DATA) -> Unit
|
||||
): FirCache<K, V, CONTEXT> =
|
||||
FirThreadUnsafeCacheWithPostCompute(createValue, postCompute)
|
||||
|
||||
override fun <V> createLazyValue(createValue: () -> V): FirLazyValue<V> =
|
||||
FirThreadUnsafeValue(createValue)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@@ -63,3 +66,8 @@ private class FirThreadUnsafeCacheWithPostCompute<K : Any, V, CONTEXT, DATA>(
|
||||
override fun getValueIfComputed(key: K): V? =
|
||||
map.getOrElse(key) { null as V }
|
||||
}
|
||||
|
||||
private class FirThreadUnsafeValue<V>(createValue: () -> V) : FirLazyValue<V>() {
|
||||
private val lazyValue by lazy(LazyThreadSafetyMode.NONE, createValue)
|
||||
override fun getValue(): V = lazyValue
|
||||
}
|
||||
Reference in New Issue
Block a user