mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 22:51:17 +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.Files;
|
||||||
import java.nio.file.NoSuchFileException;
|
import java.nio.file.NoSuchFileException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public class JpsComponentLoader {
|
public class JpsComponentLoader {
|
||||||
@@ -24,10 +26,17 @@ public class JpsComponentLoader {
|
|||||||
private static final int MAX_ATTEMPTS = 5;
|
private static final int MAX_ATTEMPTS = 5;
|
||||||
protected final @Nullable Path myExternalConfigurationDirectory;
|
protected final @Nullable Path myExternalConfigurationDirectory;
|
||||||
private final JpsMacroExpander myMacroExpander;
|
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) {
|
public JpsComponentLoader(@NotNull JpsMacroExpander macroExpander, @Nullable Path externalConfigurationDirectory) {
|
||||||
|
this(macroExpander, externalConfigurationDirectory, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JpsComponentLoader(@NotNull JpsMacroExpander macroExpander, @Nullable Path externalConfigurationDirectory, boolean useCache) {
|
||||||
myMacroExpander = macroExpander;
|
myMacroExpander = macroExpander;
|
||||||
myExternalConfigurationDirectory = externalConfigurationDirectory;
|
myExternalConfigurationDirectory = externalConfigurationDirectory;
|
||||||
|
myRootElementsCache = useCache ? new ConcurrentHashMap<>() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull JpsMacroExpander getMacroExpander() {
|
public @NotNull JpsMacroExpander getMacroExpander() {
|
||||||
@@ -38,7 +47,16 @@ public class JpsComponentLoader {
|
|||||||
* Returns null if file doesn't exist
|
* Returns null if file doesn't exist
|
||||||
*/
|
*/
|
||||||
public @Nullable Element loadRootElement(@NotNull Path file) {
|
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) {
|
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.model.serialization.JpsProjectConfigurationLoading.createProjectMacroExpander
|
||||||
import org.jetbrains.jps.util.JpsPathUtil
|
import org.jetbrains.jps.util.JpsPathUtil
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import kotlin.io.path.Path
|
import kotlin.io.path.Path
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,7 +23,8 @@ internal class ProjectDirectJpsFileContentReader(
|
|||||||
) : JpsFileContentReader {
|
) : JpsFileContentReader {
|
||||||
|
|
||||||
private val projectMacroExpander = createProjectMacroExpander(pathVariables, projectBaseDir)
|
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? {
|
override fun loadComponent(fileUrl: String, componentName: String, customModuleFilePath: String?): Element? {
|
||||||
if (fileUrl.endsWith(".iml")) {
|
if (fileUrl.endsWith(".iml")) {
|
||||||
@@ -58,9 +60,13 @@ internal class ProjectDirectJpsFileContentReader(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getModuleLoader(imlFileUrl: String): JpsComponentLoader {
|
private fun getModuleLoader(imlFileUrl: String): JpsComponentLoader {
|
||||||
|
moduleLoadersCache[imlFileUrl]?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
val moduleFile = Path(JpsPathUtil.urlToPath(imlFileUrl))
|
val moduleFile = Path(JpsPathUtil.urlToPath(imlFileUrl))
|
||||||
val macroExpander = JpsProjectConfigurationLoading.createModuleMacroExpander(pathVariables, moduleFile)
|
val macroExpander = JpsProjectConfigurationLoading.createModuleMacroExpander(pathVariables, moduleFile)
|
||||||
//todo cache
|
val loader = JpsComponentLoader(macroExpander, null, true)
|
||||||
return JpsComponentLoader(macroExpander, null)
|
moduleLoadersCache[imlFileUrl] = loader
|
||||||
|
return loader
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user