From 646653f47ebc98e2796d4306558cfcae5431f2ed Mon Sep 17 00:00:00 2001 From: Sergey Evdokimov Date: Tue, 29 May 2012 19:18:19 +0400 Subject: [PATCH] Optimization of LibraryRuntimeClasspathScope --- .../scopes/LibraryRuntimeClasspathScope.java | 111 ++++++++++++------ 1 file changed, 73 insertions(+), 38 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java b/platform/lang-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java index d02c7bf96610..89a0585a2cad 100644 --- a/platform/lang-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java +++ b/platform/lang-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java @@ -21,17 +21,15 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.*; import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.util.containers.ContainerUtil; import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Collections; -import java.util.LinkedHashSet; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -39,7 +37,7 @@ import java.util.Set; */ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { private final ProjectFileIndex myIndex; - private final LinkedHashSet myEntries = new LinkedHashSet(); + private final LinkedHashMap myEntries = new LinkedHashMap(); public LibraryRuntimeClasspathScope(final Project project, final List modules) { super(project); @@ -47,45 +45,68 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { final Set processedSdk = new THashSet(); final Set processedLibraries = new THashSet(); - ProjectRootManager.getInstance(project).orderEntries(modules).recursively().process(new RootPolicy>() { - public LinkedHashSet visitLibraryOrderEntry(final LibraryOrderEntry libraryOrderEntry, - final LinkedHashSet value) { - final Library library = libraryOrderEntry.getLibrary(); - if (library != null && processedLibraries.add(library)) { - ContainerUtil.addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.CLASSES)); - } - return value; + ProjectRootManager.getInstance(project).orderEntries(modules).recursively() + .process(new RootPolicy>() { + @Override + public LinkedHashMap visitLibraryOrderEntry(final LibraryOrderEntry libraryOrderEntry, + final LinkedHashMap value) { + final Library library = libraryOrderEntry.getLibrary(); + if (library != null && processedLibraries.add(library)) { + addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.CLASSES)); } + return value; + } - public LinkedHashSet visitModuleSourceOrderEntry(final ModuleSourceOrderEntry moduleSourceOrderEntry, - final LinkedHashSet value) { - ContainerUtil.addAll(value, moduleSourceOrderEntry.getFiles(OrderRootType.SOURCES)); - return value; - } + @Override + public LinkedHashMap visitModuleSourceOrderEntry(final ModuleSourceOrderEntry moduleSourceOrderEntry, + final LinkedHashMap value) { + addAll(value, moduleSourceOrderEntry.getFiles(OrderRootType.SOURCES)); + return value; + } - @Override - public LinkedHashSet visitModuleOrderEntry(ModuleOrderEntry moduleOrderEntry, LinkedHashSet value) { - final Module depModule = moduleOrderEntry.getModule(); - if (depModule != null) { - ContainerUtil.addAll(value, ModuleRootManager.getInstance(depModule).getSourceRoots()); - } - return value; + @Override + public LinkedHashMap visitModuleOrderEntry(ModuleOrderEntry moduleOrderEntry, + LinkedHashMap value) { + final Module depModule = moduleOrderEntry.getModule(); + if (depModule != null) { + addAll(value, ModuleRootManager.getInstance(depModule).getSourceRoots()); } + return value; + } - public LinkedHashSet visitJdkOrderEntry(final JdkOrderEntry jdkOrderEntry, final LinkedHashSet value) { - final Sdk jdk = jdkOrderEntry.getJdk(); - if (jdk != null && processedSdk.add(jdk)) { - ContainerUtil.addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.CLASSES)); - } - return value; + @Override + public LinkedHashMap visitJdkOrderEntry(JdkOrderEntry jdkOrderEntry, + LinkedHashMap value) { + final Sdk jdk = jdkOrderEntry.getJdk(); + if (jdk != null && processedSdk.add(jdk)) { + addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.CLASSES)); } - }, myEntries); + return value; + } + }, myEntries); + + fillIndexes(); } public LibraryRuntimeClasspathScope(Project project, LibraryOrderEntry entry) { super(project); myIndex = ProjectRootManager.getInstance(project).getFileIndex(); - Collections.addAll(myEntries, entry.getRootFiles(OrderRootType.CLASSES)); + addAll(myEntries, entry.getRootFiles(OrderRootType.CLASSES)); + + fillIndexes(); + } + + private void fillIndexes() { + int i = 0; + for (Map.Entry entry : myEntries.entrySet()) { + entry.setValue(i++); + } + } + + private static void addAll(Map map, VirtualFile[] files) { + for (VirtualFile file : files) { + map.put(file, null); + } } public int hashCode() { @@ -101,7 +122,7 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { } public boolean contains(VirtualFile file) { - return myEntries.contains(getFileRoot(file)); + return myEntries.containsKey(getFileRoot(file)); } @Nullable @@ -121,11 +142,25 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { public int compare(VirtualFile file1, VirtualFile file2) { final VirtualFile r1 = getFileRoot(file1); final VirtualFile r2 = getFileRoot(file2); - for (VirtualFile root : myEntries) { - if (r1 == root) return 1; - if (r2 == root) return -1; + + //for (VirtualFile root : myEntries) { + // if (r1 == root) return 1; + // if (r2 == root) return -1; + //} + //return 0; + + Integer index1 = myEntries.get(r1); + Integer index2 = myEntries.get(r2); + + if (index1 == null) { + return index2 == null ? 0 : -1; } - return 0; + + if (index2 == null) { + return 1; + } + + return index2.compareTo(index1); } public boolean isSearchInModuleContent(@NotNull Module aModule) {