[LL FIR] optimize FirCacheValue, do not create ConcurrentHashMap to store a single value

^KTIJ-24640
^KTIJ-24641
This commit is contained in:
Ilya Kirillov
2023-02-17 09:01:33 +01:00
committed by Space Team
parent 57da858810
commit 4c3672de0a
9 changed files with 40 additions and 24 deletions
@@ -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
}