[FIR] Rename KEY and VALUE generics of FirCache to K and V

This commit is contained in:
Dmitriy Novozhilov
2021-02-06 12:56:33 +03:00
parent e7fdc18ced
commit 8f0e1035fa
5 changed files with 61 additions and 61 deletions
@@ -5,11 +5,11 @@
package org.jetbrains.kotlin.fir.caches package org.jetbrains.kotlin.fir.caches
abstract class FirCache<in KEY : Any, out VALUE, in CONTEXT> { abstract class FirCache<in K : Any, out V, in CONTEXT> {
abstract fun getValue(key: KEY, context: CONTEXT): VALUE abstract fun getValue(key: K, context: CONTEXT): V
abstract fun getValueIfComputed(key: KEY): VALUE? abstract fun getValueIfComputed(key: K): V?
} }
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun <KEY : Any, VALUE> FirCache<KEY, VALUE, Nothing?>.getValue(key: KEY): VALUE = inline fun <K : Any, V> FirCache<K, V, Nothing?>.getValue(key: K): V =
getValue(key, null) getValue(key, null)
@@ -18,12 +18,12 @@ abstract class FirCachesFactory : FirSessionComponent {
* Where: * Where:
* [CONTEXT] -- type of value which be used to create value by [createValue] * [CONTEXT] -- type of value which be used to create value by [createValue]
*/ */
abstract fun <KEY : Any, VALUE, CONTEXT> createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache<KEY, VALUE, CONTEXT> abstract fun <K : Any, V, CONTEXT> createCache(createValue: (K, CONTEXT) -> V): FirCache<K, V, CONTEXT>
/** /**
* Creates a cache with returns a caches value on demand if it is computed * Creates a cache with returns a caches value on demand if it is computed
* Otherwise computes the value in two phases: * 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 * - [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 * [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] * [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] * [DATA] -- type of additional data which will be passed from [createValue] to [postCompute]
*/ */
abstract fun <KEY : Any, VALUE, CONTEXT, DATA> createCacheWithPostCompute( abstract fun <K : Any, V, CONTEXT, DATA> createCacheWithPostCompute(
createValue: (KEY, CONTEXT) -> Pair<VALUE, DATA>, createValue: (K, CONTEXT) -> Pair<V, DATA>,
postCompute: (KEY, VALUE, DATA) -> Unit postCompute: (K, V, DATA) -> Unit
): FirCache<KEY, VALUE, CONTEXT> ): FirCache<K, V, CONTEXT>
} }
val FirSession.firCachesFactory: FirCachesFactory by FirSession.sessionComponentAccessor() val FirSession.firCachesFactory: FirCachesFactory by FirSession.sessionComponentAccessor()
inline fun <KEY : Any, VALUE> FirCachesFactory.createCache( inline fun <K : Any, V> FirCachesFactory.createCache(
crossinline createValue: (KEY) -> VALUE, crossinline createValue: (K) -> V,
): FirCache<KEY, VALUE, Nothing?> = createCache( ): FirCache<K, V, Nothing?> = createCache(
createValue = { key, _ -> createValue(key) }, createValue = { key, _ -> createValue(key) },
) )
inline fun <KEY : Any, VALUE, CONTEXT> FirCachesFactory.createCacheWithPostCompute( inline fun <K : Any, V, CONTEXT> FirCachesFactory.createCacheWithPostCompute(
crossinline createValue: (KEY, CONTEXT) -> VALUE, crossinline createValue: (K, CONTEXT) -> V,
crossinline postCompute: (KEY, VALUE) -> Unit crossinline postCompute: (K, V) -> Unit
): FirCache<KEY, VALUE, CONTEXT> = createCacheWithPostCompute( ): FirCache<K, V, CONTEXT> = createCacheWithPostCompute(
createValue = { key, context -> createValue(key, context) to null }, createValue = { key, context -> createValue(key, context) to null },
postCompute = { key, value, _ -> postCompute(key, value) } postCompute = { key, value, _ -> postCompute(key, value) }
) )
inline fun <KEY : Any, VALUE> FirCachesFactory.createCacheWithPostCompute( inline fun <K : Any, V> FirCachesFactory.createCacheWithPostCompute(
crossinline createValue: (KEY) -> VALUE, crossinline createValue: (K) -> V,
crossinline postCompute: (KEY, VALUE) -> Unit crossinline postCompute: (K, V) -> Unit
): FirCache<KEY, VALUE, Nothing?> = createCacheWithPostCompute( ): FirCache<K, V, Nothing?> = createCacheWithPostCompute(
createValue = { key, _ -> createValue(key) to null }, createValue = { key, _ -> createValue(key) to null },
postCompute = { key, value, _ -> postCompute(key, value) } postCompute = { key, value, _ -> postCompute(key, value) }
) )
inline fun <KEY : Any, VALUE, DATA> FirCachesFactory.createCacheWithPostCompute( inline fun <K : Any, V, DATA> FirCachesFactory.createCacheWithPostCompute(
crossinline createValue: (KEY) -> Pair<VALUE, DATA>, crossinline createValue: (K) -> Pair<V, DATA>,
crossinline postCompute: (KEY, VALUE, DATA) -> Unit crossinline postCompute: (K, V, DATA) -> Unit
): FirCache<KEY, VALUE, Nothing?> = createCacheWithPostCompute( ): FirCache<K, V, Nothing?> = createCacheWithPostCompute(
createValue = { key, _ -> createValue(key) }, createValue = { key, _ -> createValue(key) },
postCompute = { key, value, data -> postCompute(key, value, data) } postCompute = { key, value, data -> postCompute(key, value, data) }
) )
@@ -6,41 +6,41 @@
package org.jetbrains.kotlin.fir.caches package org.jetbrains.kotlin.fir.caches
object FirThreadUnsafeCachesFactory : FirCachesFactory() { object FirThreadUnsafeCachesFactory : FirCachesFactory() {
override fun <KEY : Any, VALUE, CONTEXT> createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache<KEY, VALUE, CONTEXT> = override fun <K : Any, V, CONTEXT> createCache(createValue: (K, CONTEXT) -> V): FirCache<K, V, CONTEXT> =
FirThreadUnsafeCache(createValue) FirThreadUnsafeCache(createValue)
override fun <KEY : Any, VALUE, CONTEXT, DATA> createCacheWithPostCompute( override fun <K : Any, V, CONTEXT, DATA> createCacheWithPostCompute(
createValue: (KEY, CONTEXT) -> Pair<VALUE, DATA>, createValue: (K, CONTEXT) -> Pair<V, DATA>,
postCompute: (KEY, VALUE, DATA) -> Unit postCompute: (K, V, DATA) -> Unit
): FirCache<KEY, VALUE, CONTEXT> = ): FirCache<K, V, CONTEXT> =
FirThreadUnsafeCacheWithPostCompute(createValue, postCompute) FirThreadUnsafeCacheWithPostCompute(createValue, postCompute)
} }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private class FirThreadUnsafeCache<KEY : Any, VALUE, CONTEXT>( private class FirThreadUnsafeCache<K : Any, V, CONTEXT>(
private val createValue: (KEY, CONTEXT) -> VALUE private val createValue: (K, CONTEXT) -> V
) : FirCache<KEY, VALUE, CONTEXT>() { ) : FirCache<K, V, CONTEXT>() {
private val map = NullableMap<KEY, VALUE>() private val map = NullableMap<K, V>()
override fun getValue(key: KEY, context: CONTEXT): VALUE = override fun getValue(key: K, context: CONTEXT): V =
map.getOrElse(key) { map.getOrElse(key) {
createValue(key, context).also { createdValue -> createValue(key, context).also { createdValue ->
map[key] = createdValue map[key] = createdValue
} }
} }
override fun getValueIfComputed(key: KEY): VALUE? = override fun getValueIfComputed(key: K): V? =
map.getOrElse(key) { null as VALUE } map.getOrElse(key) { null as V }
} }
private class FirThreadUnsafeCacheWithPostCompute<KEY : Any, VALUE, CONTEXT, DATA>( private class FirThreadUnsafeCacheWithPostCompute<K : Any, V, CONTEXT, DATA>(
private val createValue: (KEY, CONTEXT) -> Pair<VALUE, DATA>, private val createValue: (K, CONTEXT) -> Pair<V, DATA>,
private val postCompute: (KEY, VALUE, DATA) -> Unit private val postCompute: (K, V, DATA) -> Unit
) : FirCache<KEY, VALUE, CONTEXT>() { ) : FirCache<K, V, CONTEXT>() {
private val map = NullableMap<KEY, VALUE>() private val map = NullableMap<K, V>()
override fun getValue(key: KEY, context: CONTEXT): VALUE = override fun getValue(key: K, context: CONTEXT): V =
map.getOrElse(key) { map.getOrElse(key) {
val (createdValue, data) = createValue(key, context) val (createdValue, data) = createValue(key, context)
map[key] = createdValue map[key] = createdValue
@@ -50,6 +50,6 @@ private class FirThreadUnsafeCacheWithPostCompute<KEY : Any, VALUE, CONTEXT, DAT
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun getValueIfComputed(key: KEY): VALUE? = override fun getValueIfComputed(key: K): V? =
map.getOrElse(key) { null as VALUE } map.getOrElse(key) { null as V }
} }
@@ -8,14 +8,14 @@ package org.jetbrains.kotlin.idea.fir.low.level.api.fir.caches
import org.jetbrains.kotlin.fir.caches.FirCache import org.jetbrains.kotlin.fir.caches.FirCache
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
internal class FirThreadSafeCache<KEY : Any, VALUE, CONTEXT>( internal class FirThreadSafeCache<K : Any, V, CONTEXT>(
private val createValue: (KEY, CONTEXT) -> VALUE private val createValue: (K, CONTEXT) -> V
) : FirCache<KEY, VALUE, CONTEXT>() { ) : FirCache<K, V, CONTEXT>() {
private val map = ConcurrentHashMap<KEY, Any>() private val map = ConcurrentHashMap<K, Any>()
override fun getValue(key: KEY, context: CONTEXT): VALUE = override fun getValue(key: K, context: CONTEXT): V =
map.computeIfAbsentWithNullableValue(key) { createValue(it, context) } map.computeIfAbsentWithNullableValue(key) { createValue(it, context) }
override fun getValueIfComputed(key: KEY): VALUE? = override fun getValueIfComputed(key: K): V? =
map[key]?.nullValueToNull() map[key]?.nullValueToNull()
} }
@@ -8,14 +8,14 @@ package org.jetbrains.kotlin.idea.fir.low.level.api.fir.caches
import org.jetbrains.kotlin.fir.caches.FirCache import org.jetbrains.kotlin.fir.caches.FirCache
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
internal class FirThreadSafeCacheWithPostCompute<KEY : Any, VALUE, CONTEXT, DATA>( internal class FirThreadSafeCacheWithPostCompute<K : Any, V, CONTEXT, DATA>(
private val createValue: (KEY, CONTEXT) -> Pair<VALUE, DATA>, private val createValue: (K, CONTEXT) -> Pair<V, DATA>,
private val postCompute: (KEY, VALUE, DATA) -> Unit private val postCompute: (K, V, DATA) -> Unit
) : FirCache<KEY, VALUE, CONTEXT>() { ) : FirCache<K, V, CONTEXT>() {
private val map = ConcurrentHashMap<KEY, ValueWithPostCompute<KEY, VALUE, DATA>>() private val map = ConcurrentHashMap<K, ValueWithPostCompute<K, V, DATA>>()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun getValue(key: KEY, context: CONTEXT): VALUE = override fun getValue(key: K, context: CONTEXT): V =
map.computeIfAbsent(key) { map.computeIfAbsent(key) {
ValueWithPostCompute( ValueWithPostCompute(
key, key,
@@ -24,6 +24,6 @@ internal class FirThreadSafeCacheWithPostCompute<KEY : Any, VALUE, CONTEXT, DATA
) )
}.getValue() }.getValue()
override fun getValueIfComputed(key: KEY): VALUE? = override fun getValueIfComputed(key: K): V? =
map[key]?.getValueIfComputed() map[key]?.getValueIfComputed()
} }