Skip to content

Commit

Permalink
FIR IDE: use custom thread local value for storing KtFirScopeProvider
Browse files Browse the repository at this point in the history
java.lang.ThreadLocal stores value maps in corresponding threads
which causes memory leaks
  • Loading branch information
darthorimar committed Nov 23, 2020
1 parent b31def0 commit bac5ebc
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit bac5ebc

Please sign in to comment.