mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-15 02:59:33 +07:00
[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:
committed by
intellij-monorepo-bot
parent
55fb09873d
commit
dbe582ee4c
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user