Skip to content

Commit d753c90

Browse files
authored
Merge pull request #8948 from mbien/cp-hint-init-performance
Refactoring: Improve listClassPathHints() performance
2 parents 684c668 + e9c19eb commit d753c90

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

java/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/DeclarativeHintRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Collection<? extends HintDescription> computeHints(final ClassPath cp, At
106106
}
107107

108108
public static Collection<? extends HintDescription> join(Map<HintMetadata, Collection<? extends HintDescription>> hints) {
109-
List<HintDescription> descs = new LinkedList<>();
109+
List<HintDescription> descs = new ArrayList<>();
110110

111111
for (Collection<? extends HintDescription> c : hints.values()) {
112112
descs.addAll(c);

java/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/Utilities.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import java.lang.ref.WeakReference;
9595
import java.net.URI;
9696
import java.nio.CharBuffer;
97+
import java.util.ArrayList;
9798
import java.util.Arrays;
9899
import java.util.Collection;
99100
import java.util.Collections;
@@ -163,8 +164,6 @@
163164
import org.openide.util.NbCollections;
164165
import org.openide.util.WeakListeners;
165166
import org.openide.util.lookup.ServiceProvider;
166-
167-
import static com.sun.source.tree.CaseTree.CaseKind.STATEMENT;
168167
import org.netbeans.api.annotations.common.NullAllowed;
169168
import org.netbeans.modules.java.hints.providers.spi.HintMetadata;
170169
import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
@@ -175,6 +174,8 @@
175174
import org.openide.util.Parameters;
176175
import org.openide.windows.TopComponent;
177176

177+
import static com.sun.source.tree.CaseTree.CaseKind.STATEMENT;
178+
178179
/**
179180
*
180181
* @author Jan Lahoda
@@ -288,32 +289,36 @@ public static List<HintDescription> listAllHints(Set<ClassPath> cps) {
288289
}
289290

290291
public static List<HintDescription> listClassPathHints(Set<ClassPath> sourceCPs, Set<ClassPath> binaryCPs) {
291-
List<HintDescription> result = new LinkedList<>();
292-
Set<FileObject> roots = new HashSet<>();
293292

293+
// deduplicate before running queries
294+
Set<FileObject> unique = new HashSet<>(128);
294295
for (ClassPath cp : binaryCPs) {
295-
for (FileObject r : cp.getRoots()) {
296-
Result2 src = SourceForBinaryQuery.findSourceRoots2(r.toURL());
296+
unique.addAll(Arrays.asList(cp.getRoots()));
297+
}
297298

298-
if (src != null && src.preferSources()) {
299-
roots.addAll(Arrays.asList(src.getRoots()));
300-
} else {
301-
roots.add(r);
302-
}
299+
Set<FileObject> roots = new HashSet<>();
300+
301+
for (FileObject fo : unique) {
302+
Result2 src = SourceForBinaryQuery.findSourceRoots2(fo.toURL());
303+
if (src != null && src.preferSources()) {
304+
roots.addAll(Arrays.asList(src.getRoots()));
305+
} else {
306+
roots.add(fo);
303307
}
304308
}
305309

306310
Set<ClassPath> cps = new HashSet<>(sourceCPs);
307-
308311
cps.add(ClassPathSupport.createClassPath(roots.toArray(FileObject[]::new)));
309312

310313
ClassPath cp = ClassPathSupport.createProxyClassPath(cps.toArray(ClassPath[]::new));
311314

312-
for (ClassPathBasedHintProvider p : Lookup.getDefault().lookupAll(ClassPathBasedHintProvider.class)) {
313-
result.addAll(p.computeHints(cp, new AtomicBoolean()));
315+
List<HintDescription> descriptions = new ArrayList<>();
316+
317+
for (ClassPathBasedHintProvider prov : Lookup.getDefault().lookupAll(ClassPathBasedHintProvider.class)) {
318+
descriptions.addAll(prov.computeHints(cp, new AtomicBoolean()));
314319
}
315320

316-
return result;
321+
return descriptions;
317322
}
318323

319324
public static Tree parseAndAttribute(CompilationInfo info, String pattern, Scope scope) {

0 commit comments

Comments
 (0)