[213] Avoid using ConcurrentCollectionFactory.createConcurrentIdentityMap

The `ConcurrentCollectionFactory` class was moved to the module
which is no longer available in the 213 platform distribution

`MapMaker().weakKeys().makeMap()` is used as drop-in replacement of
concurrent hash map with identity-based equals strategy

The new implementation stores keys on weak references, which should not
change the behavior of the `ModuleFileCacheImpl`.

1. If someone still has a strong reference to the `ktFile`, then the
cache will also keep it.

2. If there are no more strong references to the `ktFile`, then the
cache will not hold it from being garbage-collected. However, since
nobody  has a strong reference to the `ktFile` anymore, nobody can call
 `fileCached` or `getCachedFirFile` with it, and it will never be
 requested from the cache.

KTI-1114
This commit is contained in:
Roman Golyshev
2022-08-02 13:33:30 +02:00
committed by Space Team
parent d4cffb8a5a
commit fb9de6ba45
@@ -5,7 +5,7 @@
package org.jetbrains.kotlin.analysis.low.level.api.fir.file.builder
import com.intellij.concurrency.ConcurrentCollectionFactory
import com.google.common.collect.MapMaker
import org.jetbrains.kotlin.analysis.api.impl.barebone.annotations.ThreadSafe
import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirModuleResolveComponents
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.fir.declarations.FirFile
import org.jetbrains.kotlin.fir.psi
import org.jetbrains.kotlin.psi.KtFile
import java.util.*
import java.util.concurrent.ConcurrentMap
/**
* Caches mapping [KtFile] -> [FirFile] of module [KtModule]
@@ -33,7 +34,7 @@ internal abstract class ModuleFileCache {
}
internal class ModuleFileCacheImpl(override val moduleComponents: LLFirModuleResolveComponents) : ModuleFileCache() {
private val ktFileToFirFile = ConcurrentCollectionFactory.createConcurrentIdentityMap<KtFile, FirFile>()
private val ktFileToFirFile: ConcurrentMap<KtFile, FirFile> = MapMaker().weakKeys().makeMap()
override fun fileCached(file: KtFile, createValue: () -> FirFile): FirFile =
ktFileToFirFile.computeIfAbsent(file) { createValue() }