[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
+4
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.analysis.low.level.api.fir.fir.caches
|
|||||||
import org.jetbrains.kotlin.fir.caches.FirCache
|
import org.jetbrains.kotlin.fir.caches.FirCache
|
||||||
import org.jetbrains.kotlin.fir.caches.FirCachesFactory
|
import org.jetbrains.kotlin.fir.caches.FirCachesFactory
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
import org.jetbrains.kotlin.fir.caches.FirLazyValue
|
||||||
|
|
||||||
object FirThreadSafeCachesFactory : FirCachesFactory() {
|
object FirThreadSafeCachesFactory : FirCachesFactory() {
|
||||||
override fun <KEY : Any, VALUE, CONTEXT> createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache<KEY, VALUE, CONTEXT> =
|
override fun <KEY : Any, VALUE, CONTEXT> createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache<KEY, VALUE, CONTEXT> =
|
||||||
@@ -29,4 +30,7 @@ object FirThreadSafeCachesFactory : FirCachesFactory() {
|
|||||||
postCompute: (KEY, VALUE, DATA) -> Unit
|
postCompute: (KEY, VALUE, DATA) -> Unit
|
||||||
): FirCache<KEY, VALUE, CONTEXT> =
|
): FirCache<KEY, VALUE, CONTEXT> =
|
||||||
FirThreadSafeCacheWithPostCompute(createValue, postCompute)
|
FirThreadSafeCacheWithPostCompute(createValue, postCompute)
|
||||||
|
|
||||||
|
override fun <V> createLazyValue(createValue: () -> V): FirLazyValue<V> =
|
||||||
|
FirThreadSafeValue(createValue)
|
||||||
}
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.analysis.low.level.api.fir.fir.caches
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.fir.caches.FirLazyValue
|
||||||
|
|
||||||
|
internal class FirThreadSafeValue<V>(createValue: () -> V) : FirLazyValue<V>() {
|
||||||
|
private val lazyValue by lazy(LazyThreadSafetyMode.SYNCHRONIZED, createValue)
|
||||||
|
override fun getValue(): V = lazyValue
|
||||||
|
}
|
||||||
+1
-1
@@ -25,7 +25,7 @@ internal class LLFirIdeRegisteredPluginAnnotations(
|
|||||||
override val annotations: Set<AnnotationFqn>
|
override val annotations: Set<AnnotationFqn>
|
||||||
get() = allAnnotationsCache.getValue()
|
get() = allAnnotationsCache.getValue()
|
||||||
|
|
||||||
private val allAnnotationsCache: FirLazyValue<Set<AnnotationFqn>, Nothing?> = session.firCachesFactory.createLazyValue {
|
private val allAnnotationsCache: FirLazyValue<Set<AnnotationFqn>> = session.firCachesFactory.createLazyValue {
|
||||||
// at this point, both metaAnnotations and annotationsFromPlugins should be collected
|
// at this point, both metaAnnotations and annotationsFromPlugins should be collected
|
||||||
val result = metaAnnotations.flatMapTo(mutableSetOf()) { getAnnotationsWithMetaAnnotation(it) }
|
val result = metaAnnotations.flatMapTo(mutableSetOf()) { getAnnotationsWithMetaAnnotation(it) }
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -85,11 +85,11 @@ abstract class FirDeclarationGenerationExtension(session: FirSession) : FirExten
|
|||||||
}
|
}
|
||||||
|
|
||||||
@FirExtensionApiInternals
|
@FirExtensionApiInternals
|
||||||
val topLevelClassIdsCache: FirLazyValue<Set<ClassId>, Nothing?> =
|
val topLevelClassIdsCache: FirLazyValue<Set<ClassId>> =
|
||||||
session.firCachesFactory.createLazyValue { getTopLevelClassIds() }
|
session.firCachesFactory.createLazyValue { getTopLevelClassIds() }
|
||||||
|
|
||||||
@FirExtensionApiInternals
|
@FirExtensionApiInternals
|
||||||
val topLevelCallableIdsCache: FirLazyValue<Set<CallableId>, Nothing?> =
|
val topLevelCallableIdsCache: FirLazyValue<Set<CallableId>> =
|
||||||
session.firCachesFactory.createLazyValue { getTopLevelCallableIds() }
|
session.firCachesFactory.createLazyValue { getTopLevelCallableIds() }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -53,7 +53,7 @@ class FirExtensionDeclarationsSymbolProvider private constructor(
|
|||||||
hasPackage(packageFqName)
|
hasPackage(packageFqName)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val callableNamesInPackageCache: FirLazyValue<Map<FqName, Set<Name>>, Nothing?> =
|
private val callableNamesInPackageCache: FirLazyValue<Map<FqName, Set<Name>>> =
|
||||||
cachesFactory.createLazyValue {
|
cachesFactory.createLazyValue {
|
||||||
computeNamesGroupedByPackage(
|
computeNamesGroupedByPackage(
|
||||||
FirDeclarationGenerationExtension::getTopLevelCallableIds,
|
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 {
|
cachesFactory.createLazyValue {
|
||||||
computeNamesGroupedByPackage(
|
computeNamesGroupedByPackage(
|
||||||
FirDeclarationGenerationExtension::getTopLevelClassIds,
|
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 {
|
session.firCachesFactory.createLazyValue {
|
||||||
extensions.flatGroupBy { it.topLevelClassIdsCache.getValue() }
|
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 {
|
session.firCachesFactory.createLazyValue {
|
||||||
extensions.flatGroupBy { it.topLevelCallableIdsCache.getValue() }
|
extensions.flatGroupBy { it.topLevelCallableIdsCache.getValue() }
|
||||||
}
|
}
|
||||||
@@ -171,7 +171,7 @@ class FirExtensionDeclarationsSymbolProvider private constructor(
|
|||||||
|
|
||||||
override fun computePackageSetWithTopLevelCallables(): Set<String> =
|
override fun computePackageSetWithTopLevelCallables(): Set<String> =
|
||||||
extensions.flatMapTo(mutableSetOf()) { extension ->
|
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> =
|
override fun knownTopLevelClassifiersInPackage(packageFqName: FqName): Set<String> =
|
||||||
|
|||||||
+1
-1
@@ -74,7 +74,7 @@ class FirGeneratedClassDeclaredMemberScope private constructor(
|
|||||||
generateMemberProperties(callableId)
|
generateMemberProperties(callableId)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val constructorCache: FirLazyValue<List<FirConstructorSymbol>, Nothing?> = firCachesFactory.createLazyValue {
|
private val constructorCache: FirLazyValue<List<FirConstructorSymbol>> = firCachesFactory.createLazyValue {
|
||||||
generateConstructors()
|
generateConstructors()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,17 +20,10 @@ operator fun <K : Any, V> FirCache<K, V, Nothing>.contains(key: K): Boolean {
|
|||||||
return getValueIfComputed(key) != null
|
return getValueIfComputed(key) != null
|
||||||
}
|
}
|
||||||
|
|
||||||
class FirLazyValue<out V, in CONTEXT>(private val cache: FirCache<Unit, V, CONTEXT>) {
|
abstract class FirLazyValue<out V> {
|
||||||
fun getValue(context: CONTEXT): V {
|
abstract fun getValue(): V
|
||||||
return cache.getValue(Unit, context)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
operator fun <V> FirLazyValue<V, Nothing?>.getValue(thisRef: Any?, property: KProperty<*>): V {
|
operator fun <V> FirLazyValue<V>.getValue(thisRef: Any?, property: KProperty<*>): V {
|
||||||
return getValue(context = null)
|
return getValue()
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
|
||||||
inline fun <V> FirLazyValue<V, Nothing?>.getValue(): V {
|
|
||||||
return getValue(null)
|
|
||||||
}
|
}
|
||||||
@@ -59,9 +59,7 @@ abstract class FirCachesFactory : FirSessionComponent {
|
|||||||
postCompute: (K, V, DATA) -> Unit
|
postCompute: (K, V, DATA) -> Unit
|
||||||
): FirCache<K, V, CONTEXT>
|
): FirCache<K, V, CONTEXT>
|
||||||
|
|
||||||
fun <V, CONTEXT> createLazyValue(createValue: (CONTEXT) -> V): FirLazyValue<V, CONTEXT> {
|
abstract fun <V> createLazyValue(createValue: () -> V): FirLazyValue<V>
|
||||||
return FirLazyValue(createCache { _, context -> createValue(context) })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val FirSession.firCachesFactory: FirCachesFactory by FirSession.sessionComponentAccessor()
|
val FirSession.firCachesFactory: FirCachesFactory by FirSession.sessionComponentAccessor()
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ object FirThreadUnsafeCachesFactory : FirCachesFactory() {
|
|||||||
postCompute: (K, V, DATA) -> Unit
|
postCompute: (K, V, DATA) -> Unit
|
||||||
): FirCache<K, V, CONTEXT> =
|
): FirCache<K, V, CONTEXT> =
|
||||||
FirThreadUnsafeCacheWithPostCompute(createValue, postCompute)
|
FirThreadUnsafeCacheWithPostCompute(createValue, postCompute)
|
||||||
|
|
||||||
|
override fun <V> createLazyValue(createValue: () -> V): FirLazyValue<V> =
|
||||||
|
FirThreadUnsafeValue(createValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
@@ -63,3 +66,8 @@ private class FirThreadUnsafeCacheWithPostCompute<K : Any, V, CONTEXT, DATA>(
|
|||||||
override fun getValueIfComputed(key: K): V? =
|
override fun getValueIfComputed(key: K): V? =
|
||||||
map.getOrElse(key) { null as 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