mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-19 08:31:29 +00:00
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:
@@ -5,13 +5,19 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.frontend.api.fir.utils
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
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")
|
||||
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> =
|
||||
ThreadLocalValue(ThreadLocal.withInitial { init() })
|
||||
internal fun <V : Any> threadLocal(init: () -> V): ThreadLocalValue<V> =
|
||||
ThreadLocalValue(init)
|
||||
|
||||
Reference in New Issue
Block a user