inline IndexedSet, i.e. use ObjectIntHashMap<VirtualFile> directly in LibraryRuntimeClasspathScope

This commit is contained in:
Daniil Ovchinnikov
2018-07-13 20:10:38 +03:00
parent 14faa862a3
commit cd6a696aa7
2 changed files with 36 additions and 101 deletions
@@ -1,81 +0,0 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.module.impl.scopes;
import com.intellij.util.UnmodifiableIterator;
import com.intellij.util.containers.ObjectIntHashMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.function.Predicate;
class IndexedSet<E> extends LinkedHashSet<E> {
private final ObjectIntHashMap<E> myIndex = new ObjectIntHashMap<>();
/**
* @return index of element or -1 if element is not in this set
*/
int indexOf(E element) {
return myIndex.get(element);
}
@Override
public boolean add(E e) {
if (super.add(e)) {
myIndex.put(e, size() - 1);
return true;
}
else {
return false;
}
}
@Override
public Iterator<E> iterator() {
return new UnmodifiableIterator<>(super.iterator());
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
throw new UnsupportedOperationException();
}
@Override
public Object clone() {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
return myIndex.equals(((IndexedSet<?>)o).myIndex);
}
@Override
public int hashCode() {
return 31 * super.hashCode() + myIndex.hashCode();
}
}
@@ -9,7 +9,7 @@ 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 com.intellij.util.containers.ObjectIntHashMap;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -22,7 +22,7 @@ import java.util.*;
*/
public class LibraryRuntimeClasspathScope extends GlobalSearchScope {
private final ProjectFileIndex myIndex;
private final IndexedSet<VirtualFile> myEntries = new IndexedSet<>();
private final ObjectIntHashMap<VirtualFile> myEntries = new ObjectIntHashMap<>();
private int myCachedHashCode;
@@ -47,8 +47,8 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope {
public LibraryRuntimeClasspathScope(@NotNull Project project, @NotNull LibraryOrderEntry entry) {
super(project);
myIndex = ProjectRootManager.getInstance(project).getFileIndex();
Collections.addAll(myEntries, entry.getRootFiles(OrderRootType.CLASSES));
Collections.addAll(myEntries, entry.getRootFiles(OrderRootType.SOURCES));
addAll(myEntries, entry.getRootFiles(OrderRootType.CLASSES));
addAll(myEntries, entry.getRootFiles(OrderRootType.SOURCES));
}
public int hashCode() {
@@ -74,41 +74,43 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope {
@NotNull Condition<OrderEntry> condition) {
if (!processedModules.add(module)) return;
ModuleRootManager.getInstance(module).orderEntries().recursively().satisfying(condition).process(new RootPolicy<Set<VirtualFile>>() {
ModuleRootManager.getInstance(module).orderEntries().recursively().satisfying(condition).process(new RootPolicy<ObjectIntHashMap<VirtualFile>>() {
@Override
public Set<VirtualFile> visitLibraryOrderEntry(@NotNull final LibraryOrderEntry libraryOrderEntry,
final Set<VirtualFile> value) {
public ObjectIntHashMap<VirtualFile> visitLibraryOrderEntry(@NotNull final LibraryOrderEntry libraryOrderEntry,
final ObjectIntHashMap<VirtualFile> value) {
final Library library = libraryOrderEntry.getLibrary();
if (library != null && processedLibraries.add(library)) {
ContainerUtil.addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.CLASSES));
ContainerUtil.addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.SOURCES));
addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.CLASSES));
addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.SOURCES));
}
return value;
}
@Override
public Set<VirtualFile> visitModuleSourceOrderEntry(@NotNull final ModuleSourceOrderEntry moduleSourceOrderEntry,
final Set<VirtualFile> value) {
public ObjectIntHashMap<VirtualFile> visitModuleSourceOrderEntry(@NotNull final ModuleSourceOrderEntry moduleSourceOrderEntry,
final ObjectIntHashMap<VirtualFile> value) {
processedModules.add(moduleSourceOrderEntry.getOwnerModule());
ContainerUtil.addAll(value, moduleSourceOrderEntry.getRootModel().getSourceRoots());
addAll(value, moduleSourceOrderEntry.getRootModel().getSourceRoots());
return value;
}
@Override
public Set<VirtualFile> visitModuleOrderEntry(@NotNull ModuleOrderEntry moduleOrderEntry, Set<VirtualFile> value) {
public ObjectIntHashMap<VirtualFile> visitModuleOrderEntry(@NotNull ModuleOrderEntry moduleOrderEntry,
ObjectIntHashMap<VirtualFile> value) {
final Module depModule = moduleOrderEntry.getModule();
if (depModule != null) {
ContainerUtil.addAll(value, ModuleRootManager.getInstance(depModule).getSourceRoots());
addAll(value, ModuleRootManager.getInstance(depModule).getSourceRoots());
}
return value;
}
@Override
public Set<VirtualFile> visitJdkOrderEntry(@NotNull final JdkOrderEntry jdkOrderEntry, final Set<VirtualFile> value) {
public ObjectIntHashMap<VirtualFile> visitJdkOrderEntry(@NotNull final JdkOrderEntry jdkOrderEntry,
final ObjectIntHashMap<VirtualFile> value) {
final Sdk jdk = jdkOrderEntry.getJdk();
if (jdk != null && processedSdk.add(jdk)) {
ContainerUtil.addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.CLASSES));
ContainerUtil.addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.SOURCES));
addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.CLASSES));
addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.SOURCES));
}
return value;
}
@@ -136,8 +138,8 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope {
if (file1.equals(file2)) return 0;
final VirtualFile r1 = getFileRoot(file1);
final VirtualFile r2 = getFileRoot(file2);
final int i1 = myEntries.indexOf(r1);
final int i2 = myEntries.indexOf(r2);
final int i1 = myEntries.get(r1);
final int i2 = myEntries.get(r2);
if (i1 == i2) return 0;
if (i1 == -1) return -1;
if (i2 == -1) return 1;
@@ -147,7 +149,13 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope {
@TestOnly
@NotNull
public List<VirtualFile> getRoots() {
return new ArrayList<>(myEntries);
if (myEntries.isEmpty()) return Collections.emptyList();
VirtualFile[] result = new VirtualFile[myEntries.size()];
myEntries.forEachEntry((a, b) -> {
result[b] = a;
return true;
});
return Arrays.asList(result);
}
@Override
@@ -159,4 +167,12 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope {
public boolean isSearchInLibraries() {
return true;
}
private static void addAll(ObjectIntHashMap<VirtualFile> entries, VirtualFile[] files) {
for (VirtualFile file : files) {
if (!entries.contains(file)) {
entries.put(file, entries.size());
}
}
}
}