From 314d059d73fa3bc257770bbb8830e41f9d972b84 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Thu, 14 Jul 2016 13:39:54 +0300 Subject: [PATCH] inspection view: do not execute huge read actions IDEA-157005 --- .../offline/OfflineProblemDescriptor.java | 18 +++--- .../ex/InspectionRVContentProvider.java | 61 ++++++++++--------- .../ex/InspectionRVContentProviderImpl.java | 9 ++- .../OfflineDescriptorResolveResult.java | 4 +- .../OfflineInspectionRVContentProvider.java | 3 +- .../ui/InspectionResultsView.java | 18 +++--- .../codeInspection/ui/InspectionTreeNode.java | 23 ++++--- 7 files changed, 77 insertions(+), 59 deletions(-) diff --git a/platform/lang-api/src/com/intellij/codeInspection/offline/OfflineProblemDescriptor.java b/platform/lang-api/src/com/intellij/codeInspection/offline/OfflineProblemDescriptor.java index e172c5bca124..4030d1ad08d8 100644 --- a/platform/lang-api/src/com/intellij/codeInspection/offline/OfflineProblemDescriptor.java +++ b/platform/lang-api/src/com/intellij/codeInspection/offline/OfflineProblemDescriptor.java @@ -24,12 +24,12 @@ import com.intellij.codeInspection.reference.RefElement; import com.intellij.codeInspection.reference.RefEntity; import com.intellij.codeInspection.reference.RefManager; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ReadAction; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; import java.util.List; public class OfflineProblemDescriptor { @@ -91,14 +91,16 @@ public class OfflineProblemDescriptor { @Nullable public RefEntity getRefElement(final RefManager refManager) { - final RefEntity refElement = refManager.getReference(myType, myFQName); - if (refElement instanceof RefElement) { - final PsiElement element = ((RefElement)refElement).getElement(); - if (element != null && element.isValid()) { - UIUtil.invokeLaterIfNeeded(() -> PsiDocumentManager.getInstance(element.getProject()).commitAllDocuments()); + return ReadAction.compute(() -> { + final RefEntity refElement = refManager.getReference(myType, myFQName); + if (refElement instanceof RefElement) { + final PsiElement element = ((RefElement)refElement).getElement(); + if (element != null && element.isValid()) { + UIUtil.invokeLaterIfNeeded(() -> PsiDocumentManager.getInstance(element.getProject()).commitAllDocuments()); + } } - } - return refElement; + return refElement; + }); } public boolean equals(final Object o) { diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProvider.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProvider.java index 93c7e9fca428..70fddcfdcead 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProvider.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProvider.java @@ -27,6 +27,7 @@ import com.intellij.codeInspection.reference.RefElement; import com.intellij.codeInspection.reference.RefEntity; import com.intellij.codeInspection.reference.RefModule; import com.intellij.codeInspection.ui.*; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; @@ -73,7 +74,7 @@ public abstract class InspectionRVContentProvider { @NotNull public RefElementNode createNode(@NotNull InspectionToolPresentation presentation) { - return new RefElementNode(myEntity, presentation); + return ReadAction.compute(() -> new RefElementNode(myEntity, presentation)); } @Nullable @@ -203,7 +204,7 @@ public abstract class InspectionRVContentProvider { if (moduleNode == null) { if (moduleName != null) { - final Module module = ModuleManager.getInstance(myProject).findModuleByName(moduleName); + final Module module = ReadAction.compute(() -> ModuleManager.getInstance(myProject).findModuleByName(moduleName)); if (module != null) { moduleNode = new InspectionModuleNode(module); moduleNodes.put(moduleName, moduleNode); @@ -356,39 +357,41 @@ public abstract class InspectionRVContentProvider { @SuppressWarnings({"ConstantConditions"}) //class cast suppression public static InspectionTreeNode merge(InspectionTreeNode child, InspectionTreeNode parent, boolean merge) { - if (merge) { - for (int i = 0; i < parent.getChildCount(); i++) { - InspectionTreeNode current = (InspectionTreeNode)parent.getChildAt(i); - if (child.getClass() != current.getClass()) { - continue; - } - if (current instanceof InspectionPackageNode) { - if (((InspectionPackageNode)current).getPackageName().compareTo(((InspectionPackageNode)child).getPackageName()) == 0) { - processDepth(child, current); - return current; + return ReadAction.compute(() -> { + if (merge) { + for (int i = 0; i < parent.getChildCount(); i++) { + InspectionTreeNode current = (InspectionTreeNode)parent.getChildAt(i); + if (child.getClass() != current.getClass()) { + continue; } - } - else if (current instanceof RefElementNode) { - if (InspectionResultsViewComparator.getInstance().compare(current, child) == 0) { - processDepth(child, current); - return current; + if (current instanceof InspectionPackageNode) { + if (((InspectionPackageNode)current).getPackageName().compareTo(((InspectionPackageNode)child).getPackageName()) == 0) { + processDepth(child, current); + return current; + } } - } - else if (current instanceof InspectionNode) { - if (((InspectionNode)current).getToolWrapper().getShortName().compareTo(((InspectionNode)child).getToolWrapper().getShortName()) == 0) { - processDepth(child, current); - return current; + else if (current instanceof RefElementNode) { + if (InspectionResultsViewComparator.getInstance().compare(current, child) == 0) { + processDepth(child, current); + return current; + } } - } - else if (current instanceof InspectionModuleNode) { - if (((InspectionModuleNode)current).getName().compareTo(((InspectionModuleNode)child).getName()) == 0) { - processDepth(child, current); - return current; + else if (current instanceof InspectionNode) { + if (((InspectionNode)current).getToolWrapper().getShortName().compareTo(((InspectionNode)child).getToolWrapper().getShortName()) == 0) { + processDepth(child, current); + return current; + } + } + else if (current instanceof InspectionModuleNode) { + if (((InspectionModuleNode)current).getName().compareTo(((InspectionModuleNode)child).getName()) == 0) { + processDepth(child, current); + return current; + } } } } - } - return parent.insertByOrder(child, false); + return parent.insertByOrder(child, false); + }); } private static void processDepth(final InspectionTreeNode child, final InspectionTreeNode current) { diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProviderImpl.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProviderImpl.java index a823c3961fca..5dd2167432c5 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProviderImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProviderImpl.java @@ -21,8 +21,12 @@ package com.intellij.codeInspection.ex; import com.intellij.codeInspection.CommonProblemDescriptor; -import com.intellij.codeInspection.reference.*; +import com.intellij.codeInspection.reference.RefElement; +import com.intellij.codeInspection.reference.RefEntity; +import com.intellij.codeInspection.reference.RefModule; +import com.intellij.codeInspection.reference.RefUtil; import com.intellij.codeInspection.ui.*; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; @@ -125,7 +129,8 @@ public class InspectionRVContentProviderImpl extends InspectionRVContentProvider final RefElementNode elemNode = addNodeToParent(container, presentation, pNode); for (CommonProblemDescriptor problem : problems) { assert problem != null; - elemNode.insertByOrder(new ProblemDescriptionNode(refElement, problem, toolWrapper,presentation), true); + elemNode + .insertByOrder(ReadAction.compute(() -> new ProblemDescriptionNode(refElement, problem, toolWrapper, presentation)), true); if (problems.length == 1) { elemNode.setProblem(problems[0]); } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineDescriptorResolveResult.java b/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineDescriptorResolveResult.java index 8aa87b53903b..53bc0b00c1e5 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineDescriptorResolveResult.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineDescriptorResolveResult.java @@ -30,6 +30,7 @@ import com.intellij.codeInspection.reference.RefElement; import com.intellij.codeInspection.reference.RefEntity; import com.intellij.codeInspection.ui.InspectionToolPresentation; import com.intellij.lang.Language; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; @@ -71,7 +72,8 @@ class OfflineDescriptorResolveResult { @NotNull InspectionToolWrapper wrapper, @NotNull InspectionToolPresentation presentation) { final RefEntity element = descriptor.getRefElement(presentation.getContext().getRefManager()); - final CommonProblemDescriptor resolvedDescriptor = createDescriptor(element, descriptor, wrapper, presentation); + final CommonProblemDescriptor resolvedDescriptor = + ReadAction.compute(() -> createDescriptor(element, descriptor, wrapper, presentation)); return new OfflineDescriptorResolveResult(element, resolvedDescriptor); } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineInspectionRVContentProvider.java b/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineInspectionRVContentProvider.java index 6fecff4b71a6..18644c6c33e3 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineInspectionRVContentProvider.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineInspectionRVContentProvider.java @@ -26,6 +26,7 @@ import com.intellij.codeInspection.offline.OfflineProblemDescriptor; import com.intellij.codeInspection.reference.RefElement; import com.intellij.codeInspection.reference.RefEntity; import com.intellij.codeInspection.ui.*; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; import com.intellij.util.containers.FactoryMap; @@ -190,7 +191,7 @@ public class OfflineInspectionRVContentProvider extends InspectionRVContentProvi for (OfflineProblemDescriptor descriptor : ((RefEntityContainer)container).getDescriptors()) { final OfflineDescriptorResolveResult resolveResult = myResolvedDescriptor.get(toolWrapper.getShortName()) .computeIfAbsent(descriptor, d -> OfflineDescriptorResolveResult.resolve(d, toolWrapper, presentation)); - elemNode.insertByOrder(OfflineProblemDescriptorNode.create(descriptor, resolveResult, toolWrapper, presentation), true); + elemNode.insertByOrder(ReadAction.compute(() -> OfflineProblemDescriptorNode.create(descriptor, resolveResult, toolWrapper, presentation)), true); } } } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionResultsView.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionResultsView.java index 1fc502143aed..d72acb790b4e 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionResultsView.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionResultsView.java @@ -759,14 +759,16 @@ public class InspectionResultsView extends JPanel implements Disposable, Occuren final HighlightDisplayKey key = HighlightDisplayKey.find(defaultToolWrapper.getShortName()); for (ScopeToolState state : myProvider.getTools(currentTools)) { InspectionToolWrapper toolWrapper = state.getTool(); - ReadAction.run(() -> { - if (myProvider.checkReportedProblems(myGlobalInspectionContext, toolWrapper)) { - addTool(toolWrapper, - profile.getErrorLevel(key, state.getScope(myProject), myProject), - isGroupedBySeverity, - singleInspectionRun); - } - }); + if (ReadAction.compute(() -> myProvider.checkReportedProblems(myGlobalInspectionContext, toolWrapper))) { + //ReadAction.run( + // () -> + addTool(toolWrapper, + profile.getErrorLevel(key, state.getScope(myProject), myProject), + isGroupedBySeverity, + singleInspectionRun) + //) + ; + } } } } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTreeNode.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTreeNode.java index fee8c8bfa333..de0c543a2cb5 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTreeNode.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTreeNode.java @@ -18,6 +18,7 @@ package com.intellij.codeInspection.ui; import com.intellij.codeHighlighting.HighlightDisplayLevel; import com.intellij.codeInspection.reference.RefEntity; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.vcs.FileStatus; import com.intellij.util.containers.FactoryMap; import com.intellij.util.ui.tree.TreeUtil; @@ -101,18 +102,20 @@ public abstract class InspectionTreeNode extends DefaultMutableTreeNode { } public InspectionTreeNode insertByOrder(InspectionTreeNode child, boolean allowDuplication) { - if (!allowDuplication) { - int index = getIndex(child); - if (index != -1) { + return ReadAction.compute(() -> { + if (!allowDuplication) { + int index = getIndex(child); + if (index != -1) { + return (InspectionTreeNode)getChildAt(index); + } + } + int index = TreeUtil.indexedBinarySearch(this, child, InspectionResultsViewComparator.getInstance()); + if (!allowDuplication && index >= 0){ return (InspectionTreeNode)getChildAt(index); } - } - int index = TreeUtil.indexedBinarySearch(this, child, InspectionResultsViewComparator.getInstance()); - if (!allowDuplication && index >= 0){ - return (InspectionTreeNode)getChildAt(index); - } - insert(child, Math.abs(index + 1)); - return child; + insert(child, Math.abs(index + 1)); + return child; + }); } @Override