FIR IDE: use custom thread local value for storing KtFirScopeProvider
java.lang.ThreadLocal stores value maps in corresponding threads which causes memory leaks
This commit is contained in:
+10
-4
@@ -5,13 +5,19 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.frontend.api.fir.utils
|
package org.jetbrains.kotlin.idea.frontend.api.fir.utils
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
|
||||||
internal class ThreadLocalValue<V>(private val threadLocal: ThreadLocal<V>) {
|
internal class ThreadLocalValue<V : Any>(private val init: () -> V) {
|
||||||
|
private val map = ConcurrentHashMap<Long, V>()
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
inline operator fun getValue(thisRef: Any?, property: KProperty<*>): V = threadLocal.get()
|
inline operator fun getValue(thisRef: Any?, property: KProperty<*>): V =
|
||||||
|
map.computeIfAbsent(Thread.currentThread().id) {
|
||||||
|
init()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal inline fun <T> threadLocal(crossinline init: () -> T): ThreadLocalValue<T> =
|
internal fun <V : Any> threadLocal(init: () -> V): ThreadLocalValue<V> =
|
||||||
ThreadLocalValue(ThreadLocal.withInitial { init() })
|
ThreadLocalValue(init)
|
||||||
|
|||||||
Reference in New Issue
Block a user