mirror of
https://github.com/openjdk/jdk.git
synced 2026-06-02 08:42:39 +00:00
8072114: javac performance should be improved
Avoiding unnecessary use of Stream.empty(). Reviewed-by: mcimadamore
This commit is contained in:
parent
5d4a22554a
commit
3d264c5a76
@ -29,8 +29,6 @@ import com.sun.tools.javac.code.Kinds.Kind;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import com.sun.tools.javac.code.Symbol.CompletionFailure;
|
||||
import com.sun.tools.javac.code.Symbol.TypeSymbol;
|
||||
@ -40,6 +38,8 @@ import com.sun.tools.javac.util.List;
|
||||
|
||||
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
|
||||
import static com.sun.tools.javac.code.Scope.LookupKind.RECURSIVE;
|
||||
import static com.sun.tools.javac.util.Iterators.createCompoundIterator;
|
||||
import static com.sun.tools.javac.util.Iterators.createFilterIterator;
|
||||
|
||||
/** A scope represents an area of visibility in a Java program. The
|
||||
* Scope class is a container for symbols which provides
|
||||
@ -898,7 +898,11 @@ public abstract class Scope {
|
||||
return tsym.members().getSymbols(sf, lookupKind);
|
||||
}
|
||||
};
|
||||
return si.importFrom((TypeSymbol) origin.owner) :: iterator;
|
||||
List<Iterable<Symbol>> results =
|
||||
si.importFrom((TypeSymbol) origin.owner, List.nil());
|
||||
return () -> createFilterIterator(createCompoundIterator(results,
|
||||
Iterable::iterator),
|
||||
s -> filter.accepts(origin, s));
|
||||
} catch (CompletionFailure cf) {
|
||||
cfHandler.accept(imp, cf);
|
||||
return Collections.emptyList();
|
||||
@ -918,7 +922,11 @@ public abstract class Scope {
|
||||
return tsym.members().getSymbolsByName(name, sf, lookupKind);
|
||||
}
|
||||
};
|
||||
return si.importFrom((TypeSymbol) origin.owner) :: iterator;
|
||||
List<Iterable<Symbol>> results =
|
||||
si.importFrom((TypeSymbol) origin.owner, List.nil());
|
||||
return () -> createFilterIterator(createCompoundIterator(results,
|
||||
Iterable::iterator),
|
||||
s -> filter.accepts(origin, s));
|
||||
} catch (CompletionFailure cf) {
|
||||
cfHandler.accept(imp, cf);
|
||||
return Collections.emptyList();
|
||||
@ -942,22 +950,19 @@ public abstract class Scope {
|
||||
public SymbolImporter(boolean inspectSuperTypes) {
|
||||
this.inspectSuperTypes = inspectSuperTypes;
|
||||
}
|
||||
Stream<Symbol> importFrom(TypeSymbol tsym) {
|
||||
List<Iterable<Symbol>> importFrom(TypeSymbol tsym, List<Iterable<Symbol>> results) {
|
||||
if (tsym == null || !processed.add(tsym))
|
||||
return Stream.empty();
|
||||
return results;
|
||||
|
||||
Stream<Symbol> result = Stream.empty();
|
||||
|
||||
if (inspectSuperTypes) {
|
||||
// also import inherited names
|
||||
result = importFrom(types.supertype(tsym.type).tsym);
|
||||
results = importFrom(types.supertype(tsym.type).tsym, results);
|
||||
for (Type t : types.interfaces(tsym.type))
|
||||
result = Stream.concat(importFrom(t.tsym), result);
|
||||
results = importFrom(t.tsym, results);
|
||||
}
|
||||
|
||||
return Stream.concat(StreamSupport.stream(doLookup(tsym).spliterator(), false)
|
||||
.filter(s -> filter.accepts(origin, s)),
|
||||
result);
|
||||
return results.prepend(doLookup(tsym));
|
||||
}
|
||||
abstract Iterable<Symbol> doLookup(TypeSymbol tsym);
|
||||
}
|
||||
|
||||
@ -28,6 +28,7 @@ package com.sun.tools.javac.util;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/** Utilities for Iterators.
|
||||
*
|
||||
@ -92,4 +93,32 @@ public class Iterators {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
public static <E> Iterator<E> createFilterIterator(Iterator<E> input, Predicate<E> test) {
|
||||
return new Iterator<E>() {
|
||||
private E current = update();
|
||||
private E update () {
|
||||
while (input.hasNext()) {
|
||||
E sym = input.next();
|
||||
if (test.test(sym)) {
|
||||
return sym;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return current != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
E res = current;
|
||||
current = update();
|
||||
return res;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user