diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/FirThreadSafeCachesFactory.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/FirThreadSafeCachesFactory.kt index b1fe5ecedb2..eec9f64f0b5 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/FirThreadSafeCachesFactory.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/FirThreadSafeCachesFactory.kt @@ -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.FirCachesFactory import java.util.concurrent.ConcurrentHashMap +import org.jetbrains.kotlin.fir.caches.FirLazyValue object FirThreadSafeCachesFactory : FirCachesFactory() { override fun createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache = @@ -29,4 +30,7 @@ object FirThreadSafeCachesFactory : FirCachesFactory() { postCompute: (KEY, VALUE, DATA) -> Unit ): FirCache = FirThreadSafeCacheWithPostCompute(createValue, postCompute) + + override fun createLazyValue(createValue: () -> V): FirLazyValue = + FirThreadSafeValue(createValue) } \ No newline at end of file diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/FirThreadSafeValue.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/FirThreadSafeValue.kt new file mode 100644 index 00000000000..7e41989cd4f --- /dev/null +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/FirThreadSafeValue.kt @@ -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(createValue: () -> V) : FirLazyValue() { + private val lazyValue by lazy(LazyThreadSafetyMode.SYNCHRONIZED, createValue) + override fun getValue(): V = lazyValue +} \ No newline at end of file diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/providers/LLFirIdeRegisteredPluginAnnotations.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/providers/LLFirIdeRegisteredPluginAnnotations.kt index 5b19cf4d4cd..8be2dce8748 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/providers/LLFirIdeRegisteredPluginAnnotations.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/providers/LLFirIdeRegisteredPluginAnnotations.kt @@ -25,7 +25,7 @@ internal class LLFirIdeRegisteredPluginAnnotations( override val annotations: Set get() = allAnnotationsCache.getValue() - private val allAnnotationsCache: FirLazyValue, Nothing?> = session.firCachesFactory.createLazyValue { + private val allAnnotationsCache: FirLazyValue> = session.firCachesFactory.createLazyValue { // at this point, both metaAnnotations and annotationsFromPlugins should be collected val result = metaAnnotations.flatMapTo(mutableSetOf()) { getAnnotationsWithMetaAnnotation(it) } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/extensions/FirDeclarationGenerationExtension.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/extensions/FirDeclarationGenerationExtension.kt index 4c50c598310..e8c5c44caa6 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/extensions/FirDeclarationGenerationExtension.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/extensions/FirDeclarationGenerationExtension.kt @@ -85,11 +85,11 @@ abstract class FirDeclarationGenerationExtension(session: FirSession) : FirExten } @FirExtensionApiInternals - val topLevelClassIdsCache: FirLazyValue, Nothing?> = + val topLevelClassIdsCache: FirLazyValue> = session.firCachesFactory.createLazyValue { getTopLevelClassIds() } @FirExtensionApiInternals - val topLevelCallableIdsCache: FirLazyValue, Nothing?> = + val topLevelCallableIdsCache: FirLazyValue> = session.firCachesFactory.createLazyValue { getTopLevelCallableIds() } } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/extensions/FirExtensionDeclarationsSymbolProvider.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/extensions/FirExtensionDeclarationsSymbolProvider.kt index 5e6ae3b25e2..c1ab2e831ce 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/extensions/FirExtensionDeclarationsSymbolProvider.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/extensions/FirExtensionDeclarationsSymbolProvider.kt @@ -53,7 +53,7 @@ class FirExtensionDeclarationsSymbolProvider private constructor( hasPackage(packageFqName) } - private val callableNamesInPackageCache: FirLazyValue>, Nothing?> = + private val callableNamesInPackageCache: FirLazyValue>> = cachesFactory.createLazyValue { computeNamesGroupedByPackage( FirDeclarationGenerationExtension::getTopLevelCallableIds, @@ -61,7 +61,7 @@ class FirExtensionDeclarationsSymbolProvider private constructor( ) } - private val classNamesInPackageCache: FirLazyValue>, Nothing?> = + private val classNamesInPackageCache: FirLazyValue>> = cachesFactory.createLazyValue { computeNamesGroupedByPackage( FirDeclarationGenerationExtension::getTopLevelClassIds, @@ -82,12 +82,12 @@ class FirExtensionDeclarationsSymbolProvider private constructor( } } - private val extensionsByTopLevelClassId: FirLazyValue>, Nothing?> = + private val extensionsByTopLevelClassId: FirLazyValue>> = session.firCachesFactory.createLazyValue { extensions.flatGroupBy { it.topLevelClassIdsCache.getValue() } } - private val extensionsByTopLevelCallableId: FirLazyValue>, Nothing?> = + private val extensionsByTopLevelCallableId: FirLazyValue>> = session.firCachesFactory.createLazyValue { extensions.flatGroupBy { it.topLevelCallableIdsCache.getValue() } } @@ -171,7 +171,7 @@ class FirExtensionDeclarationsSymbolProvider private constructor( override fun computePackageSetWithTopLevelCallables(): Set = 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 = diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirGeneratedClassDeclaredMemberScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirGeneratedClassDeclaredMemberScope.kt index 54f1ab3cd7d..7d4a55b029c 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirGeneratedClassDeclaredMemberScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirGeneratedClassDeclaredMemberScope.kt @@ -74,7 +74,7 @@ class FirGeneratedClassDeclaredMemberScope private constructor( generateMemberProperties(callableId) } - private val constructorCache: FirLazyValue, Nothing?> = firCachesFactory.createLazyValue { + private val constructorCache: FirLazyValue> = firCachesFactory.createLazyValue { generateConstructors() } diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirCacheWithPostCompute.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirCacheWithPostCompute.kt index 590fa4e7330..c541a1dcf90 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirCacheWithPostCompute.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirCacheWithPostCompute.kt @@ -20,17 +20,10 @@ operator fun FirCache.contains(key: K): Boolean { return getValueIfComputed(key) != null } -class FirLazyValue(private val cache: FirCache) { - fun getValue(context: CONTEXT): V { - return cache.getValue(Unit, context) - } +abstract class FirLazyValue { + abstract fun getValue(): V } -operator fun FirLazyValue.getValue(thisRef: Any?, property: KProperty<*>): V { - return getValue(context = null) -} - -@Suppress("NOTHING_TO_INLINE") -inline fun FirLazyValue.getValue(): V { - return getValue(null) -} +operator fun FirLazyValue.getValue(thisRef: Any?, property: KProperty<*>): V { + return getValue() +} \ No newline at end of file diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirCachesFactory.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirCachesFactory.kt index 304196ea7a8..276a50050d1 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirCachesFactory.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirCachesFactory.kt @@ -59,9 +59,7 @@ abstract class FirCachesFactory : FirSessionComponent { postCompute: (K, V, DATA) -> Unit ): FirCache - fun createLazyValue(createValue: (CONTEXT) -> V): FirLazyValue { - return FirLazyValue(createCache { _, context -> createValue(context) }) - } + abstract fun createLazyValue(createValue: () -> V): FirLazyValue } val FirSession.firCachesFactory: FirCachesFactory by FirSession.sessionComponentAccessor() diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirThreadUnsafeCachesFactory.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirThreadUnsafeCachesFactory.kt index 322aa857cac..6f5f450aad3 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirThreadUnsafeCachesFactory.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/caches/FirThreadUnsafeCachesFactory.kt @@ -24,6 +24,9 @@ object FirThreadUnsafeCachesFactory : FirCachesFactory() { postCompute: (K, V, DATA) -> Unit ): FirCache = FirThreadUnsafeCacheWithPostCompute(createValue, postCompute) + + override fun createLazyValue(createValue: () -> V): FirLazyValue = + FirThreadUnsafeValue(createValue) } @Suppress("UNCHECKED_CAST") @@ -63,3 +66,8 @@ private class FirThreadUnsafeCacheWithPostCompute( override fun getValueIfComputed(key: K): V? = map.getOrElse(key) { null as V } } + +private class FirThreadUnsafeValue(createValue: () -> V) : FirLazyValue() { + private val lazyValue by lazy(LazyThreadSafetyMode.NONE, createValue) + override fun getValue(): V = lazyValue +} \ No newline at end of file