[Analysis API] optimize RAM consumption in ValidityAwareCachedValue

- make it inline class
- remove non-required `token` field

^KTIJ-22749
This commit is contained in:
Ilya Kirillov
2023-02-03 12:32:08 +01:00
committed by Space Team
parent 6d7096b5e9
commit 2e5d284472
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.analysis.api.fir.utils
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeOwner
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.assertIsValidAndAccessible
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
@@ -14,16 +13,17 @@ import kotlin.reflect.KProperty
/**
* Lazy value that guaranties safe publication and checks validity on every access
*/
class ValidityAwareCachedValue<T>(
private val token: KtLifetimeToken,
init: () -> T
@JvmInline
value class ValidityAwareCachedValue<T>(
private val lazyValue: Lazy<T>,
) : ReadOnlyProperty<Any, T> {
private val lazyValue = lazy(LazyThreadSafetyMode.PUBLICATION, init)
override fun getValue(thisRef: Any, property: KProperty<*>): T {
token.assertIsValidAndAccessible()
require(thisRef is KtLifetimeOwner)
thisRef.token.assertIsValidAndAccessible()
return lazyValue.value
}
}
internal fun <T> KtLifetimeOwner.cached(init: () -> T) = ValidityAwareCachedValue(token, init)
@Suppress("UnusedReceiverParameter") // we need to have the KtLifetimeOwner as receiver to make sure it's called only for KtLifetimeOwner
internal fun <T> KtLifetimeOwner.cached(init: () -> T) = ValidityAwareCachedValue(lazy(LazyThreadSafetyMode.PUBLICATION, init))