8072114: javac performance should be improved
authorjlahoda
Wed, 08 Mar 2017 20:42:17 +0100
changeset 44185 309b455e3ccc
parent 44184 606c14ee20ed
child 44186 fe848a208b20
8072114: javac performance should be improved Summary: Avoiding unnecessary use of Stream.empty(). Reviewed-by: mcimadamore
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Scope.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Iterators.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Scope.java	Wed Mar 08 14:21:13 2017 +0000
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Scope.java	Wed Mar 08 20:42:17 2017 +0100
@@ -29,8 +29,6 @@
 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 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 @@
                         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 @@
                         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 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);
         }
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Iterators.java	Wed Mar 08 14:21:13 2017 +0000
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Iterators.java	Wed Mar 08 20:42:17 2017 +0100
@@ -28,6 +28,7 @@
 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 @@
             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;
+            }
+        };
+    }
+
 }