From 8d9c1b175d02d4a50708a0eeeceeafeb33e1e63a Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Fri, 18 May 2012 13:22:20 +0400 Subject: [PATCH] instantiate tools outside read action --- .../daemon/impl/DaemonCodeAnalyzerImpl.java | 23 +++++++++++++++++ .../ex/InspectionProfileWrapper.java | 25 +++++++++++++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java index b9e0f7d040af..d8e2621a5a0c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java @@ -26,6 +26,7 @@ import com.intellij.codeInsight.daemon.LineMarkerInfo; import com.intellij.codeInsight.daemon.ReferenceImporter; import com.intellij.codeInsight.hint.HintManager; import com.intellij.codeInsight.intention.impl.IntentionHintComponent; +import com.intellij.codeInspection.ex.InspectionProfileWrapper; import com.intellij.concurrency.Job; import com.intellij.ide.PowerSaveMode; import com.intellij.lang.annotation.HighlightSeverity; @@ -53,6 +54,7 @@ import com.intellij.openapi.util.*; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileManager; import com.intellij.packageDependencies.DependencyValidationManager; +import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.PsiCompiledElement; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; @@ -749,10 +751,31 @@ public class DaemonCodeAnalyzerImpl extends DaemonCodeAnalyzer implements JDOMEx myPassExecutorService.submitPasses(passes, progress, Job.DEFAULT_PRIORITY); } }; + + if (activeEditor == null) { runnable.run(); } else { + final InspectionProfileWrapper profile = InspectionProjectProfileManager.getInstance(myProject).getProfileWrapper(); + final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(activeEditor.getDocument()); + if (psiFile != null && profile != null && !profile.areToolsInstantiated()) { + // optimization: do expensive classloading outside readaction + ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { + @Override + public void run() { + try { + if (!psiFile.getManager().isDisposed() && !profile.areToolsInstantiated()) { + profile.preInstantiateTools(psiFile); + } + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + }); + } + ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(myProject)).cancelAndRunWhenAllCommitted( "start daemon when all committed", runnable); } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileWrapper.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileWrapper.java index c2052337d83b..35a5ee5873bb 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileWrapper.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileWrapper.java @@ -22,8 +22,10 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; import com.intellij.util.Function; import gnu.trove.THashSet; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; @@ -66,9 +68,9 @@ public class InspectionProfileWrapper { return enabled; } - // check whether some inspection got registered twice by accident. Bit only once. + // check whether some inspection got registered twice by accident. 've bit once. private static boolean alreadyChecked; - private static void checkInspectionsDuplicates(InspectionTool[] tools) { + private static void checkInspectionsDuplicates(@NotNull InspectionTool[] tools) { if (alreadyChecked) return; alreadyChecked = true; Set uniqTools = new THashSet(tools.length); @@ -79,6 +81,22 @@ public class InspectionProfileWrapper { } } + private volatile boolean toolsInstantiated; + public void preInstantiateTools(PsiFile psiFile) { + if (toolsInstantiated) return; + toolsInstantiated = true; + InspectionTool[] tools = getInspectionTools(psiFile); + for (InspectionTool tool : tools) { + if (tool instanceof InspectionToolWrapper) { + ((InspectionToolWrapper)tool).getTool(); + } + } + } + + public boolean areToolsInstantiated() { + return toolsInstantiated; + } + public String getName() { return myProfile.getName(); } @@ -105,13 +123,10 @@ public class InspectionProfileWrapper { } public void cleanup(final Project project){ - myProfile.cleanup(project); } public InspectionProfile getInspectionProfile() { return myProfile; } - - }