Skip to content

Commit 8bd3ab8

Browse files
committed
Find Usage performance: moved classpath merging out of the inner loop
instead of merging entries into the ClassPath one by one, we collect them first to build the ClassPath as final step. this method is mostly used by the find usage action
1 parent c3bc2a9 commit 8bd3ab8

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

java/refactoring.java/src/org/netbeans/modules/refactoring/java/RefactoringUtils.java

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -599,17 +599,18 @@ public static ClasspathInfo getClasspathInfoFor(boolean dependencies, FileObject
599599
* @param files
600600
* @return
601601
*/
602-
@SuppressWarnings("CollectionContainsUrl")
603602
public static ClasspathInfo getClasspathInfoFor(boolean dependencies, boolean backSource, FileObject... files) {
604603
assert files.length > 0;
605604
Set<URL> dependentSourceRoots = new HashSet<>();
606605
Set<URL> dependentCompileRoots = new HashSet<>();
607-
ClassPath nullPath = ClassPathSupport.createClassPath(new FileObject[0]);
608-
ClassPath boot = null;
609-
ClassPath moduleBoot = null;
610-
ClassPath compile = null;
611-
ClassPath moduleCompile = null;
612-
ClassPath moduleClass = null;
606+
ClassPath nullPath = ClassPath.EMPTY;
607+
608+
List<ClassPath> bootCPs = new ArrayList<>();
609+
List<ClassPath> moduleBootCPs = new ArrayList<>();
610+
List<ClassPath> compileCPs = new ArrayList<>();
611+
List<ClassPath> moduleCompileCPs = new ArrayList<>();
612+
List<ClassPath> moduleClassCPs = new ArrayList<>();
613+
613614
for (FileObject fo : files) {
614615
ClassPath cp = null;
615616
FileObject ownerRoot = null;
@@ -621,30 +622,30 @@ public static ClasspathInfo getClasspathInfoFor(boolean dependencies, boolean ba
621622
}
622623
if (cp != null && ownerRoot != null && FileUtil.getArchiveFile(ownerRoot) == null) {
623624
for (FileObject src : cp.getRoots()) { // Keep all source roots from cp. Needed if project has multiple source roots.
624-
URL sourceRoot = URLMapper.findURL(src, URLMapper.INTERNAL);
625-
if (dependencies) {
626-
Set<URL> urls = SourceUtils.getDependentRoots(sourceRoot, false);
627-
Set<ClassPath> cps = GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE);
628-
Set<URL> toRetain = new HashSet<URL>();
629-
for (ClassPath path : cps) {
630-
for (ClassPath.Entry e : path.entries()) {
631-
toRetain.add(e.getURL());
625+
URL sourceRoot = URLMapper.findURL(src, URLMapper.INTERNAL);
626+
if (dependencies) {
627+
Set<URL> urls = SourceUtils.getDependentRoots(sourceRoot, false);
628+
Set<ClassPath> cps = GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE);
629+
Set<URL> toRetain = new HashSet<>();
630+
for (ClassPath path : cps) {
631+
for (ClassPath.Entry e : path.entries()) {
632+
toRetain.add(e.getURL());
633+
}
632634
}
635+
Set<URL> compileUrls = new HashSet<>(urls);
636+
urls.retainAll(toRetain);
637+
compileUrls.removeAll(toRetain);
638+
dependentSourceRoots.addAll(urls);
639+
dependentCompileRoots.addAll(compileUrls);
640+
} else {
641+
dependentSourceRoots.add(sourceRoot);
633642
}
634-
Set<URL> compileUrls = new HashSet<URL>(urls);
635-
urls.retainAll(toRetain);
636-
compileUrls.removeAll(toRetain);
637-
dependentSourceRoots.addAll(urls);
638-
dependentCompileRoots.addAll(compileUrls);
639-
} else {
640-
dependentSourceRoots.add(sourceRoot);
641-
}
642-
if (FileOwnerQuery.getOwner(fo) != null) {
643-
for (FileObject f : cp.getRoots()) {
644-
dependentCompileRoots.add(URLMapper.findURL(f, URLMapper.INTERNAL));
643+
if (FileOwnerQuery.getOwner(fo) != null) {
644+
for (FileObject f : cp.getRoots()) {
645+
dependentCompileRoots.add(URLMapper.findURL(f, URLMapper.INTERNAL));
646+
}
645647
}
646648
}
647-
}
648649
} else {
649650
for (ClassPath scp : GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE)) {
650651
for (FileObject root : scp.getRoots()) {
@@ -669,20 +670,19 @@ public static ClasspathInfo getClasspathInfoFor(boolean dependencies, boolean ba
669670
if (fcompile == null) {
670671
LOG.log(Level.WARNING, "No classpath for: {0} {1}", new Object[]{FileUtil.getFileDisplayName(fo), FileOwnerQuery.getOwner(fo)}); //NOI18N
671672
} else {
672-
compile = compile != null ? merge(compile, fcompile) : fcompile;
673+
compileCPs.add(fcompile);
673674
}
674-
675675
if (fboot != null) {
676-
boot = boot != null ? merge(boot, fboot) : fboot;
676+
bootCPs.add(fboot);
677677
}
678678
if (fmoduleboot != null) {
679-
moduleBoot = moduleBoot != null ? merge(moduleBoot, fmoduleboot) : fmoduleboot;
679+
moduleBootCPs.add(fmoduleboot);
680680
}
681681
if (fmodulecompile != null) {
682-
moduleCompile = moduleCompile != null ? merge(moduleCompile, fmodulecompile) : fmodulecompile;
682+
moduleCompileCPs.add(fmodulecompile);
683683
}
684684
if (fmoduleclass != null) {
685-
moduleClass = moduleClass != null ? merge(moduleClass, fmoduleclass) : fmoduleclass;
685+
moduleClassCPs.add(fmoduleclass);
686686
}
687687
}
688688
}
@@ -700,12 +700,18 @@ public static ClasspathInfo getClasspathInfoFor(boolean dependencies, boolean ba
700700
}
701701
}
702702
}
703-
704-
ClassPath rcp = ClassPathSupport.createClassPath(dependentSourceRoots.toArray(new URL[0]));
703+
704+
ClassPath compile = !compileCPs.isEmpty() ? merge(compileCPs) : null;
705+
ClassPath boot = !bootCPs.isEmpty() ? merge(bootCPs) : null;
706+
ClassPath moduleBoot = !moduleBootCPs.isEmpty() ? merge(moduleBootCPs) : null;
707+
ClassPath moduleCompile = !moduleCompileCPs.isEmpty() ? merge(moduleCompileCPs) : null;
708+
ClassPath moduleClass = !moduleClassCPs.isEmpty() ? merge(moduleClassCPs) : null;
709+
710+
ClassPath rcp = ClassPathSupport.createClassPath(dependentSourceRoots.toArray(URL[]::new));
705711
if (compile == null) {
706712
compile = nullPath;
707713
}
708-
compile = merge(compile, ClassPathSupport.createClassPath(dependentCompileRoots.toArray(new URL[0])));
714+
compile = merge(compile, ClassPathSupport.createClassPath(dependentCompileRoots.toArray(URL[]::new)));
709715
if (boot == null) {
710716
boot = JavaPlatform.getDefault().getBootstrapLibraries();
711717
}
@@ -1085,20 +1091,23 @@ public static FileObject getRootFileObject(URL url) throws IOException {
10851091
return result;
10861092
}
10871093

1088-
@SuppressWarnings("CollectionContainsUrl")
1089-
public static ClassPath merge(final ClassPath... cps) {
1090-
final Set<URL> roots = new LinkedHashSet<URL>(cps.length);
1091-
for (final ClassPath cp : cps) {
1094+
public static ClassPath merge(ClassPath... cps) {
1095+
return merge(Arrays.asList(cps));
1096+
}
1097+
1098+
public static ClassPath merge(List<ClassPath> cps) {
1099+
Set<URL> roots = new LinkedHashSet<>((int) Math.ceil(cps.size() / 0.75));
1100+
for (ClassPath cp : cps) {
10921101
if (cp != null) {
1093-
for (final ClassPath.Entry entry : cp.entries()) {
1094-
final URL root = entry.getURL();
1102+
for (ClassPath.Entry entry : cp.entries()) {
1103+
URL root = entry.getURL();
10951104
if (!roots.contains(root)) {
10961105
roots.add(root);
10971106
}
10981107
}
10991108
}
11001109
}
1101-
return ClassPathSupport.createClassPath(roots.toArray(new URL[0]));
1110+
return ClassPathSupport.createClassPath(roots.toArray(URL[]::new));
11021111
}
11031112

11041113
public static boolean isFromEditor(EditorCookie ec) {

0 commit comments

Comments
 (0)