make AllClassesSearchExecutor multi-threaded
authorPeter Gromov <peter@jetbrains.com>
Mon, 10 Aug 2020 13:38:30 +0000 (15:38 +0200)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Mon, 10 Aug 2020 14:06:20 +0000 (14:06 +0000)
GitOrigin-RevId: 7cd41f6d051ef45dd2ff878828c34e8ed0626aef

java/java-indexing-impl/src/com/intellij/psi/impl/search/AllClassesSearchExecutor.java

index e90e64c74e8338140e5677bf66b965cf2de02e16..da2e9082ba1210b3827ba1aae8ada8d70d262fc2 100644 (file)
@@ -5,6 +5,7 @@
  */
 package com.intellij.psi.impl.search;
 
+import com.intellij.concurrency.JobLauncher;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.progress.ProgressIndicatorProvider;
 import com.intellij.openapi.progress.ProgressManager;
@@ -58,7 +59,8 @@ public class AllClassesSearchExecutor implements QueryExecutor<PsiClass, AllClas
                                                         @NotNull final AllClassesSearch.SearchParameters parameters,
                                                         @NotNull Processor<? super PsiClass> processor) {
     final Set<String> names = new THashSet<>(10000);
-    processClassNames(parameters.getProject(), scope, s -> {
+    Project project = parameters.getProject();
+    processClassNames(project, scope, s -> {
       if (parameters.nameMatches(s)) {
         names.add(s);
       }
@@ -68,7 +70,9 @@ public class AllClassesSearchExecutor implements QueryExecutor<PsiClass, AllClas
     List<String> sorted = new ArrayList<>(names);
     sorted.sort(String.CASE_INSENSITIVE_ORDER);
 
-    return processClassesByNames(parameters.getProject(), scope, sorted, processor);
+    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
+    return JobLauncher.getInstance().invokeConcurrentlyUnderProgress(sorted, ProgressIndicatorProvider.getGlobalProgressIndicator(), name ->
+      processByName(project, scope, processor, cache, name));
   }
 
   public static boolean processClassesByNames(@NotNull Project project,
@@ -78,11 +82,20 @@ public class AllClassesSearchExecutor implements QueryExecutor<PsiClass, AllClas
     final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
     for (final String name : names) {
       ProgressIndicatorProvider.checkCanceled();
-      for (PsiClass psiClass : DumbService.getInstance(project).runReadActionInSmartMode(() -> cache.getClassesByName(name, scope))) {
-        ProgressIndicatorProvider.checkCanceled();
-        if (!processor.process(psiClass)) {
-          return false;
-        }
+      if (!processByName(project, scope, processor, cache, name)) return false;
+    }
+    return true;
+  }
+
+  private static boolean processByName(Project project,
+                                       GlobalSearchScope scope,
+                                       Processor<? super PsiClass> processor,
+                                       PsiShortNamesCache cache,
+                                       String name) {
+    for (PsiClass psiClass : DumbService.getInstance(project).runReadActionInSmartMode(() -> cache.getClassesByName(name, scope))) {
+      ProgressIndicatorProvider.checkCanceled();
+      if (!processor.process(psiClass)) {
+        return false;
       }
     }
     return true;