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 2921e4f95bf..328c748da70 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 @@ -5,11 +5,11 @@ package org.jetbrains.kotlin.fir.caches -abstract class FirCache { - abstract fun getValue(key: KEY, context: CONTEXT): VALUE - abstract fun getValueIfComputed(key: KEY): VALUE? +abstract class FirCache { + abstract fun getValue(key: K, context: CONTEXT): V + abstract fun getValueIfComputed(key: K): V? } @Suppress("NOTHING_TO_INLINE") -inline fun FirCache.getValue(key: KEY): VALUE = +inline fun FirCache.getValue(key: K): V = getValue(key, null) 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 4cc8d948c0f..14bce79dc4c 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 @@ -18,12 +18,12 @@ abstract class FirCachesFactory : FirSessionComponent { * Where: * [CONTEXT] -- type of value which be used to create value by [createValue] */ - abstract fun createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache + abstract fun createCache(createValue: (K, CONTEXT) -> V): FirCache /** * Creates a cache with returns a caches value on demand if it is computed * Otherwise computes the value in two phases: - * - [createValue] -- creates values and stores [VALUE] to cache and passes [VALUE] & [DATA] to [postCompute] + * - [createValue] -- creates values and stores value of type [V] to cache and passes [V] & [DATA] to [postCompute] * - [postCompute] -- performs some operations on computed value after it placed into map * * [FirCache.getValue] can be safely called in postCompute from the same thread and correct value computed by [createValue] will be returned @@ -33,40 +33,40 @@ abstract class FirCachesFactory : FirSessionComponent { * [CONTEXT] -- type of value which be used to create value by [createValue] * [DATA] -- type of additional data which will be passed from [createValue] to [postCompute] */ - abstract fun createCacheWithPostCompute( - createValue: (KEY, CONTEXT) -> Pair, - postCompute: (KEY, VALUE, DATA) -> Unit - ): FirCache + abstract fun createCacheWithPostCompute( + createValue: (K, CONTEXT) -> Pair, + postCompute: (K, V, DATA) -> Unit + ): FirCache } val FirSession.firCachesFactory: FirCachesFactory by FirSession.sessionComponentAccessor() -inline fun FirCachesFactory.createCache( - crossinline createValue: (KEY) -> VALUE, -): FirCache = createCache( +inline fun FirCachesFactory.createCache( + crossinline createValue: (K) -> V, +): FirCache = createCache( createValue = { key, _ -> createValue(key) }, ) -inline fun FirCachesFactory.createCacheWithPostCompute( - crossinline createValue: (KEY, CONTEXT) -> VALUE, - crossinline postCompute: (KEY, VALUE) -> Unit -): FirCache = createCacheWithPostCompute( +inline fun FirCachesFactory.createCacheWithPostCompute( + crossinline createValue: (K, CONTEXT) -> V, + crossinline postCompute: (K, V) -> Unit +): FirCache = createCacheWithPostCompute( createValue = { key, context -> createValue(key, context) to null }, postCompute = { key, value, _ -> postCompute(key, value) } ) -inline fun FirCachesFactory.createCacheWithPostCompute( - crossinline createValue: (KEY) -> VALUE, - crossinline postCompute: (KEY, VALUE) -> Unit -): FirCache = createCacheWithPostCompute( +inline fun FirCachesFactory.createCacheWithPostCompute( + crossinline createValue: (K) -> V, + crossinline postCompute: (K, V) -> Unit +): FirCache = createCacheWithPostCompute( createValue = { key, _ -> createValue(key) to null }, postCompute = { key, value, _ -> postCompute(key, value) } ) -inline fun FirCachesFactory.createCacheWithPostCompute( - crossinline createValue: (KEY) -> Pair, - crossinline postCompute: (KEY, VALUE, DATA) -> Unit -): FirCache = createCacheWithPostCompute( +inline fun FirCachesFactory.createCacheWithPostCompute( + crossinline createValue: (K) -> Pair, + crossinline postCompute: (K, V, DATA) -> Unit +): FirCache = createCacheWithPostCompute( createValue = { key, _ -> createValue(key) }, postCompute = { key, value, data -> postCompute(key, value, data) } ) 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 5532f99dfbc..7c166625ac8 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 @@ -6,41 +6,41 @@ package org.jetbrains.kotlin.fir.caches object FirThreadUnsafeCachesFactory : FirCachesFactory() { - override fun createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache = + override fun createCache(createValue: (K, CONTEXT) -> V): FirCache = FirThreadUnsafeCache(createValue) - override fun createCacheWithPostCompute( - createValue: (KEY, CONTEXT) -> Pair, - postCompute: (KEY, VALUE, DATA) -> Unit - ): FirCache = + override fun createCacheWithPostCompute( + createValue: (K, CONTEXT) -> Pair, + postCompute: (K, V, DATA) -> Unit + ): FirCache = FirThreadUnsafeCacheWithPostCompute(createValue, postCompute) } @Suppress("UNCHECKED_CAST") -private class FirThreadUnsafeCache( - private val createValue: (KEY, CONTEXT) -> VALUE -) : FirCache() { - private val map = NullableMap() +private class FirThreadUnsafeCache( + private val createValue: (K, CONTEXT) -> V +) : FirCache() { + private val map = NullableMap() - override fun getValue(key: KEY, context: CONTEXT): VALUE = + override fun getValue(key: K, context: CONTEXT): V = map.getOrElse(key) { createValue(key, context).also { createdValue -> map[key] = createdValue } } - override fun getValueIfComputed(key: KEY): VALUE? = - map.getOrElse(key) { null as VALUE } + override fun getValueIfComputed(key: K): V? = + map.getOrElse(key) { null as V } } -private class FirThreadUnsafeCacheWithPostCompute( - private val createValue: (KEY, CONTEXT) -> Pair, - private val postCompute: (KEY, VALUE, DATA) -> Unit -) : FirCache() { - private val map = NullableMap() +private class FirThreadUnsafeCacheWithPostCompute( + private val createValue: (K, CONTEXT) -> Pair, + private val postCompute: (K, V, DATA) -> Unit +) : FirCache() { + private val map = NullableMap() - override fun getValue(key: KEY, context: CONTEXT): VALUE = + override fun getValue(key: K, context: CONTEXT): V = map.getOrElse(key) { val (createdValue, data) = createValue(key, context) map[key] = createdValue @@ -50,6 +50,6 @@ private class FirThreadUnsafeCacheWithPostCompute( - private val createValue: (KEY, CONTEXT) -> VALUE -) : FirCache() { - private val map = ConcurrentHashMap() +internal class FirThreadSafeCache( + private val createValue: (K, CONTEXT) -> V +) : FirCache() { + private val map = ConcurrentHashMap() - override fun getValue(key: KEY, context: CONTEXT): VALUE = + override fun getValue(key: K, context: CONTEXT): V = map.computeIfAbsentWithNullableValue(key) { createValue(it, context) } - override fun getValueIfComputed(key: KEY): VALUE? = + override fun getValueIfComputed(key: K): V? = map[key]?.nullValueToNull() -} \ No newline at end of file +} diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/fir/caches/FirThreadSafeCacheWithPostCompute.kt b/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/fir/caches/FirThreadSafeCacheWithPostCompute.kt index 57c6e458008..a378e2b807a 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/fir/caches/FirThreadSafeCacheWithPostCompute.kt +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/fir/caches/FirThreadSafeCacheWithPostCompute.kt @@ -8,14 +8,14 @@ package org.jetbrains.kotlin.idea.fir.low.level.api.fir.caches import org.jetbrains.kotlin.fir.caches.FirCache import java.util.concurrent.ConcurrentHashMap -internal class FirThreadSafeCacheWithPostCompute( - private val createValue: (KEY, CONTEXT) -> Pair, - private val postCompute: (KEY, VALUE, DATA) -> Unit -) : FirCache() { - private val map = ConcurrentHashMap>() +internal class FirThreadSafeCacheWithPostCompute( + private val createValue: (K, CONTEXT) -> Pair, + private val postCompute: (K, V, DATA) -> Unit +) : FirCache() { + private val map = ConcurrentHashMap>() @Suppress("UNCHECKED_CAST") - override fun getValue(key: KEY, context: CONTEXT): VALUE = + override fun getValue(key: K, context: CONTEXT): V = map.computeIfAbsent(key) { ValueWithPostCompute( key, @@ -24,6 +24,6 @@ internal class FirThreadSafeCacheWithPostCompute