From fb9de6ba452bb3570cb870827d9010f8c5a1eca4 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Tue, 2 Aug 2022 13:33:30 +0200 Subject: [PATCH] [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 --- .../api/fir/file/builder/LLFirDeclrationsCacheForModule.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/file/builder/LLFirDeclrationsCacheForModule.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/file/builder/LLFirDeclrationsCacheForModule.kt index 4496314937b..8d38c61c747 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/file/builder/LLFirDeclrationsCacheForModule.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/file/builder/LLFirDeclrationsCacheForModule.kt @@ -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() + private val ktFileToFirFile: ConcurrentMap = MapMaker().weakKeys().makeMap() override fun fileCached(file: KtFile, createValue: () -> FirFile): FirFile = ktFileToFirFile.computeIfAbsent(file) { createValue() }