From 97e3202e47937eea2eb42fbf26b25e62cdda6b5f Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Thu, 19 Dec 2024 20:28:40 +0100 Subject: [PATCH 1/4] Reuse content root when starting run config to improve performance, fix #3558 --- CHANGELOG.md | 1 + src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt | 4 ++-- src/nl/hannahsten/texifyidea/util/General.kt | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89c8d6687..37fca0499 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] ### Added +* Improve performance when starting a run configuration * Change order in structure view to match source file and sectioning level * Add command redefinitions to command definition filter in structure view * Add support for automatic language injection on the minted environment diff --git a/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt b/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt index 02de9d144..942a12a21 100644 --- a/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt +++ b/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt @@ -287,9 +287,9 @@ enum class LatexCompiler(private val displayName: String, val executableName: St */ fun getCommand(runConfig: LatexRunConfiguration, project: Project): List? { val rootManager = ProjectRootManager.getInstance(project) - val fileIndex = rootManager.fileIndex val mainFile = runConfig.mainFile ?: return null - val moduleRoot = fileIndex.getContentRootForFile(mainFile) + // Getting the content root is an expensive operation (See WorkspaceFileIndexDataImpl#ensureIsUpToDate), and since it probably won't change often we reuse a cached value + val moduleRoot = runConfig.outputPath.contentRoot // For now we disable module roots with Docker // Could be improved by mounting them to the right directory val moduleRoots = if (runConfig.getLatexDistributionType().isDocker()) { diff --git a/src/nl/hannahsten/texifyidea/util/General.kt b/src/nl/hannahsten/texifyidea/util/General.kt index d5db28d6b..15f3ed4c5 100644 --- a/src/nl/hannahsten/texifyidea/util/General.kt +++ b/src/nl/hannahsten/texifyidea/util/General.kt @@ -88,3 +88,9 @@ fun runInBackground(project: Project?, description: String, function: (indicator } }) } + +fun runInBackgroundWithoutProgress(function: () -> Unit) { + ApplicationManager.getApplication().invokeLater { + function() + } +} From eb02676026bc4d1068f74e9fedbb9a0344eea3c2 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Thu, 19 Dec 2024 20:52:50 +0100 Subject: [PATCH 2/4] Fill LatexExternalPackageInclusionCache in background, fix #3607 --- CHANGELOG.md | 2 +- .../LatexExternalPackageInclusionCache.kt | 45 ++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37fca0499..d2daf86e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## [Unreleased] ### Added -* Improve performance when starting a run configuration +* Improve performance when starting a run configuration and when using autocompletion directly after starting the IDE * Change order in structure view to match source file and sectioning level * Add command redefinitions to command definition filter in structure view * Add support for automatic language injection on the minted environment diff --git a/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt b/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt index e691989b3..eb1db8f5e 100644 --- a/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt +++ b/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt @@ -1,5 +1,6 @@ package nl.hannahsten.texifyidea.index.file +import com.intellij.openapi.application.runReadAction import com.intellij.openapi.project.DumbService import com.intellij.openapi.project.Project import com.intellij.psi.search.GlobalSearchScope @@ -8,6 +9,8 @@ import com.jetbrains.rd.util.concurrentMapOf import nl.hannahsten.texifyidea.algorithm.DFS import nl.hannahsten.texifyidea.lang.LatexPackage import nl.hannahsten.texifyidea.util.files.removeFileExtension +import nl.hannahsten.texifyidea.util.runInBackground +import java.util.concurrent.atomic.AtomicBoolean /** * Compute and cache for each package style file all the other style files it includes (directly or indirectly). @@ -18,11 +21,13 @@ object LatexExternalPackageInclusionCache { private val cache = concurrentMapOf>() + private var isFillingCache = AtomicBoolean(false) + /** * Map every LaTeX package style file to all the style files it includes, directly or indirectly. */ @Synchronized - fun getAllPackageInclusions(project: Project): Map> { + fun updateOrGetCache(project: Project): Map> { if (cache.isNotEmpty() || DumbService.isDumb(project)) return cache // Make sure the index is ready (#3754) @@ -31,20 +36,30 @@ object LatexExternalPackageInclusionCache { val directChildren = mutableMapOf>() // Get direct children from the index - FileBasedIndex.getInstance().getAllKeys(LatexExternalPackageInclusionIndex.Cache.id, project).forEach { indexKey -> - FileBasedIndex.getInstance().processValues( - LatexExternalPackageInclusionIndex.Cache.id, indexKey, null, { file, _ -> - val key = LatexPackage(file.name.removeFileExtension()) - directChildren[key] = directChildren.getOrDefault(key, mutableSetOf()).also { it.add(LatexPackage((indexKey))) } - true - }, - GlobalSearchScope.everythingScope(project) - ) - } + if (isFillingCache.getAndSet(true)) return cache + runInBackground(project, "Retrieving LaTeX package inclusions...") { indicator -> + try { + runReadAction { FileBasedIndex.getInstance().getAllKeys(LatexExternalPackageInclusionIndex.Cache.id, project) }.forEach { indexKey -> + runReadAction { + FileBasedIndex.getInstance().processValues( + LatexExternalPackageInclusionIndex.Cache.id, indexKey, null, { file, _ -> + indicator.checkCanceled() + val key = LatexPackage(file.name.removeFileExtension()) + directChildren[key] = directChildren.getOrDefault(key, mutableSetOf()).also { it.add(LatexPackage((indexKey))) } + true + }, + GlobalSearchScope.everythingScope(project) + ) + } + } - // Do some DFS for indirect inclusions - for (latexPackage in directChildren.keys) { - cache[latexPackage] = DFS(latexPackage) { parent -> directChildren[parent] ?: emptySet() }.execute() + // Do some DFS for indirect inclusions + for (latexPackage in directChildren.keys) { + cache[latexPackage] = DFS(latexPackage) { parent -> directChildren[parent] ?: emptySet() }.execute() + } + } finally { + isFillingCache.set(false) + } } return cache @@ -55,7 +70,7 @@ object LatexExternalPackageInclusionCache { */ fun getAllIndirectlyIncludedPackages(packages: Collection, project: Project): Set { val result = packages.toMutableSet() - val allInclusions = getAllPackageInclusions(project) + val allInclusions = updateOrGetCache(project) for (latexPackage in packages) { result.addAll(allInclusions[latexPackage] ?: emptySet()) } From 13184b61f3962f5f0cee8580082ac64a728a3890 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Fri, 20 Dec 2024 10:49:53 +0100 Subject: [PATCH 3/4] ?? --- .../LatexExternalPackageInclusionCache.kt | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt b/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt index eb1db8f5e..2184d59db 100644 --- a/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt +++ b/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt @@ -1,5 +1,7 @@ package nl.hannahsten.texifyidea.index.file +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.runInEdt import com.intellij.openapi.application.runReadAction import com.intellij.openapi.project.DumbService import com.intellij.openapi.project.Project @@ -37,28 +39,32 @@ object LatexExternalPackageInclusionCache { // Get direct children from the index if (isFillingCache.getAndSet(true)) return cache - runInBackground(project, "Retrieving LaTeX package inclusions...") { indicator -> - try { - runReadAction { FileBasedIndex.getInstance().getAllKeys(LatexExternalPackageInclusionIndex.Cache.id, project) }.forEach { indexKey -> - runReadAction { - FileBasedIndex.getInstance().processValues( - LatexExternalPackageInclusionIndex.Cache.id, indexKey, null, { file, _ -> - indicator.checkCanceled() - val key = LatexPackage(file.name.removeFileExtension()) - directChildren[key] = directChildren.getOrDefault(key, mutableSetOf()).also { it.add(LatexPackage((indexKey))) } - true - }, - GlobalSearchScope.everythingScope(project) - ) + // ??? + runInEdt { + runInBackground(project, "Retrieving LaTeX package inclusions...") { indicator -> + try { + runReadAction { FileBasedIndex.getInstance().getAllKeys(LatexExternalPackageInclusionIndex.Cache.id, project) }.forEach { indexKey -> + runReadAction { + FileBasedIndex.getInstance().processValues( + LatexExternalPackageInclusionIndex.Cache.id, indexKey, null, { file, _ -> + indicator.checkCanceled() + val key = LatexPackage(file.name.removeFileExtension()) + directChildren[key] = directChildren.getOrDefault(key, mutableSetOf()).also { it.add(LatexPackage((indexKey))) } + true + }, + GlobalSearchScope.everythingScope(project) + ) + } } - } - // Do some DFS for indirect inclusions - for (latexPackage in directChildren.keys) { - cache[latexPackage] = DFS(latexPackage) { parent -> directChildren[parent] ?: emptySet() }.execute() + // Do some DFS for indirect inclusions + for (latexPackage in directChildren.keys) { + cache[latexPackage] = DFS(latexPackage) { parent -> directChildren[parent] ?: emptySet() }.execute() + } + } + finally { + isFillingCache.set(false) } - } finally { - isFillingCache.set(false) } } From 80dc2aacfc96129737f8ce056a3955cf8c6bae81 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Fri, 20 Dec 2024 14:35:52 +0100 Subject: [PATCH 4/4] Unused import --- .../texifyidea/index/file/LatexExternalPackageInclusionCache.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt b/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt index 2184d59db..693ba49cd 100644 --- a/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt +++ b/src/nl/hannahsten/texifyidea/index/file/LatexExternalPackageInclusionCache.kt @@ -1,6 +1,5 @@ package nl.hannahsten.texifyidea.index.file -import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.runInEdt import com.intellij.openapi.application.runReadAction import com.intellij.openapi.project.DumbService