[jps model] cache contents of loaded files when using the new implementation (IJPL-409)

Unlike the old implementation, the new one may call JpsComponentLoader.loadComponent multiple times for the same file, so it's better to cache content to speed up loading.

GitOrigin-RevId: 4c3630f24bdcb8740ef5cd3ba6d414febb0f9d77
This commit is contained in:
Nikolay Chashnikov
2024-06-27 18:29:26 +02:00
committed by intellij-monorepo-bot
parent 55fb09873d
commit dbe582ee4c
2 changed files with 28 additions and 4 deletions

View File

@@ -17,6 +17,8 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
@ApiStatus.Internal
public class JpsComponentLoader {
@@ -24,10 +26,17 @@ public class JpsComponentLoader {
private static final int MAX_ATTEMPTS = 5;
protected final @Nullable Path myExternalConfigurationDirectory;
private final JpsMacroExpander myMacroExpander;
private static final Element NULL_VALUE = new Element("null");
private final ConcurrentHashMap<Path, Element> myRootElementsCache;
public JpsComponentLoader(@NotNull JpsMacroExpander macroExpander, @Nullable Path externalConfigurationDirectory) {
this(macroExpander, externalConfigurationDirectory, false);
}
public JpsComponentLoader(@NotNull JpsMacroExpander macroExpander, @Nullable Path externalConfigurationDirectory, boolean useCache) {
myMacroExpander = macroExpander;
myExternalConfigurationDirectory = externalConfigurationDirectory;
myRootElementsCache = useCache ? new ConcurrentHashMap<>() : null;
}
public @NotNull JpsMacroExpander getMacroExpander() {
@@ -38,7 +47,16 @@ public class JpsComponentLoader {
* Returns null if file doesn't exist
*/
public @Nullable Element loadRootElement(@NotNull Path file) {
return loadRootElement(file, myMacroExpander);
if (myRootElementsCache == null) {
return loadRootElement(file, myMacroExpander);
}
Element cached = myRootElementsCache.get(file);
if (cached != null) {
return cached != NULL_VALUE ? cached : null;
}
Element result = loadRootElement(file, myMacroExpander);
myRootElementsCache.put(file, Objects.requireNonNullElse(result, NULL_VALUE));
return result;
}
public @Nullable Element loadComponent(@NotNull Path file, @NotNull String componentName) {

View File

@@ -10,6 +10,7 @@ import org.jetbrains.jps.model.serialization.JpsProjectConfigurationLoading
import org.jetbrains.jps.model.serialization.JpsProjectConfigurationLoading.createProjectMacroExpander
import org.jetbrains.jps.util.JpsPathUtil
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap
import kotlin.io.path.Path
/**
@@ -22,7 +23,8 @@ internal class ProjectDirectJpsFileContentReader(
) : JpsFileContentReader {
private val projectMacroExpander = createProjectMacroExpander(pathVariables, projectBaseDir)
val projectComponentLoader = JpsComponentLoader(projectMacroExpander, externalConfigurationDirectory)
private val moduleLoadersCache = ConcurrentHashMap<String, JpsComponentLoader>()
val projectComponentLoader = JpsComponentLoader(projectMacroExpander, externalConfigurationDirectory, true)
override fun loadComponent(fileUrl: String, componentName: String, customModuleFilePath: String?): Element? {
if (fileUrl.endsWith(".iml")) {
@@ -58,9 +60,13 @@ internal class ProjectDirectJpsFileContentReader(
}
private fun getModuleLoader(imlFileUrl: String): JpsComponentLoader {
moduleLoadersCache[imlFileUrl]?.let {
return it
}
val moduleFile = Path(JpsPathUtil.urlToPath(imlFileUrl))
val macroExpander = JpsProjectConfigurationLoading.createModuleMacroExpander(pathVariables, moduleFile)
//todo cache
return JpsComponentLoader(macroExpander, null)
val loader = JpsComponentLoader(macroExpander, null, true)
moduleLoadersCache[imlFileUrl] = loader
return loader
}
}