From 8b88315a699ffe4d1e7602482fd9d7f1bbe22ec6 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Tue, 13 Dec 2022 14:48:23 +0100 Subject: [PATCH] [LL FIR] do not allocate lambda for the ValueWithPostCompute.guard to reduce a memory consumption for a little ValueWithPostCompute are created very often --- .../low/level/api/fir/caches/ValueWithPostCompute.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/ValueWithPostCompute.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/ValueWithPostCompute.kt index 3d2ac9ea5c1..b8f713a980d 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/ValueWithPostCompute.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/ValueWithPostCompute.kt @@ -27,7 +27,7 @@ internal class ValueWithPostCompute( private var _calculate: ((KEY) -> Pair)? = calculate private var _postCompute: ((KEY, VALUE, DATA) -> Unit)? = postCompute private var lock: ReentrantLock? = ReentrantLock() - private var guard: ThreadLocal? = ThreadLocal.withInitial { false } + private var guard: ThreadLocal? = ThreadLocal() /** * can be in one of the following three states: @@ -43,7 +43,8 @@ internal class ValueWithPostCompute( private var value: Any? = ValueIsNotComputed private inline fun recursiveGuarded(body: () -> T): T { - check(!guard!!.get()) { + val currentGuardValue = guard!!.get() + check(currentGuardValue == null || !currentGuardValue) { "Should not be called recursively" } guard!!.set(true)