From 07a29bd9cbaeb48dd191cc3d7b94c7f9bdc30ff4 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 30 Oct 2013 09:07:05 +0100 Subject: [PATCH] RootIndex: less vfs lookups when searching for non-existing packages, don't cache empty results --- .../roots/impl/DirectoryIndexImpl.java | 2 +- .../openapi/roots/impl/RootIndex.java | 54 ++++++++----------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/openapi/roots/impl/DirectoryIndexImpl.java b/platform/lang-impl/src/com/intellij/openapi/roots/impl/DirectoryIndexImpl.java index 0ce56f9172c0..dadbffc9f8f6 100644 --- a/platform/lang-impl/src/com/intellij/openapi/roots/impl/DirectoryIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/openapi/roots/impl/DirectoryIndexImpl.java @@ -466,7 +466,7 @@ public class DirectoryIndexImpl extends DirectoryIndex { RootIndex rootIndex = getRootIndex(); if (rootIndex != null) { - Collection riResult = rootIndex.getDirectoriesByPackageName(packageName, includeLibrarySources).findAll(); + Collection riResult = rootIndex.getDirectoriesByPackageName(packageName, includeLibrarySources); Collection standard = standardResult.findAll(); if (!new HashSet(riResult).equals(new HashSet(standard))) { for (VirtualFile file : standard) { diff --git a/platform/lang-impl/src/com/intellij/openapi/roots/impl/RootIndex.java b/platform/lang-impl/src/com/intellij/openapi/roots/impl/RootIndex.java index ceed40285cbe..4136f48ed03a 100644 --- a/platform/lang-impl/src/com/intellij/openapi/roots/impl/RootIndex.java +++ b/platform/lang-impl/src/com/intellij/openapi/roots/impl/RootIndex.java @@ -29,8 +29,6 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.newvfs.events.VFileEvent; -import com.intellij.util.CollectionQuery; -import com.intellij.util.Query; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.MultiMap; import gnu.trove.TObjectIntHashMap; @@ -347,54 +345,44 @@ class RootIndex { } @NotNull - public Query getDirectoriesByPackageName(@NotNull final String packageName, final boolean includeLibrarySources) { + List getDirectoriesByPackageName(@NotNull final String packageName, final boolean includeLibrarySources) { Map> cacheMap = includeLibrarySources ? myDirectoriesByPackageNameCacheWithLibSrc : myDirectoriesByPackageNameCache; final List cachedResult = cacheMap.get(packageName); if (cachedResult != null) { - return new CollectionQuery(cachedResult); + return cachedResult; } final ArrayList result = ContainerUtil.newArrayList(); - for (Map.Entry> entry : myPackagePrefixRoots.entrySet()) { - if (!packageName.startsWith(entry.getKey())) { - continue; - } - - if (packageName.equals(entry.getKey())) { - for (VirtualFile file : entry.getValue()) { - if (isValidPackageDirectory(includeLibrarySources, file)) { - result.add(file); - } - } - continue; - } - - final String nestedPackageName = entry.getKey().isEmpty() ? packageName : packageName.substring( entry.getKey().length()); - final List nestedPackages = StringUtil.split(nestedPackageName, "."); - - for (final VirtualFile root : entry.getValue()) { - VirtualFile file = root; - for (String name : nestedPackages) { - file = file.findChild(name); - if (file == null) { - break; - } - } - + Set packagePrefixRoots = myPackagePrefixRoots.get(packageName); + if (packagePrefixRoots != null) { + for (VirtualFile file : packagePrefixRoots) { if (isValidPackageDirectory(includeLibrarySources, file)) { result.add(file); } } } - cacheMap.put(packageName, result); - return new CollectionQuery(result); + if (StringUtil.isNotEmpty(packageName)) { + String parentPackage = StringUtil.getPackageName(packageName); + String shortName = StringUtil.getShortName(packageName); + for (VirtualFile parentDir : getDirectoriesByPackageName(parentPackage, includeLibrarySources)) { + VirtualFile child = parentDir.findChild(shortName); + if (isValidPackageDirectory(includeLibrarySources, child)) { + result.add(child); + } + } + } + + if (!result.isEmpty()) { + cacheMap.put(packageName, result); + } + return result; } private boolean isValidPackageDirectory(boolean includeLibrarySources, @Nullable VirtualFile file) { - if (file != null) { + if (file != null && file.isDirectory()) { DirectoryInfo info = getInfoForDirectory(file); if (info != null) { if (includeLibrarySources || !info.isInLibrarySource() || info.isInModuleSource() || info.hasLibraryClassRoot()) {