changeset 6388:e8517ea52d33

Improved short-circuiting in Find{Any,First}Op, MatchOp
author briangoetz
date Mon, 12 Nov 2012 13:25:21 -0500
parents 969458b5ee9d
children b3fc2ac88d4c
files src/share/classes/java/util/Iterators.java src/share/classes/java/util/streams/ops/FindAnyOp.java src/share/classes/java/util/streams/ops/FindFirstOp.java src/share/classes/java/util/streams/ops/MatchOp.java
diffstat 4 files changed, 34 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Iterators.java	Mon Nov 12 12:45:50 2012 -0500
+++ b/src/share/classes/java/util/Iterators.java	Mon Nov 12 13:25:21 2012 -0500
@@ -136,7 +136,7 @@
         };
     }
 
-    public static<T> Enumeration<T> asEnumeration(Iterator<T> iterator) {
+    public static<T> Enumeration<T> asEnumeration(Iterator<? extends T> iterator) {
         return new Enumeration<T>() {
             @Override
             public boolean hasMoreElements() {
--- a/src/share/classes/java/util/streams/ops/FindAnyOp.java	Mon Nov 12 12:45:50 2012 -0500
+++ b/src/share/classes/java/util/streams/ops/FindAnyOp.java	Mon Nov 12 13:25:21 2012 -0500
@@ -89,6 +89,15 @@
         }
 
         @Override
+        public void compute() {
+            // Have we already found an answer?
+            if (answer.get() != null)
+                helpComplete();
+            else
+                super.compute();
+        }
+
+        @Override
         protected Optional<T> doLeaf() {
             Iterator<T> iterator = helper.wrapIterator(spliterator.iterator());
             if (iterator.hasNext()) {
--- a/src/share/classes/java/util/streams/ops/FindFirstOp.java	Mon Nov 12 12:45:50 2012 -0500
+++ b/src/share/classes/java/util/streams/ops/FindFirstOp.java	Mon Nov 12 13:25:21 2012 -0500
@@ -86,12 +86,17 @@
             return new FindFirstTask<>(this, spliterator);
         }
 
+        private boolean taskCancelled() {
+            boolean cancel = canceled;
+            if (!cancel)
+                for (FindFirstTask<S, T> parent = getParent(); !cancel && parent != null; parent = parent.getParent())
+                    cancel = parent.canceled;
+            return cancel;
+        }
+
         @Override
         public void compute() {
-            boolean cancel = canceled;
-            for (FindFirstTask<S, T> parent = getParent(); !cancel && parent != null; parent = parent.getParent())
-                cancel = parent.canceled;
-            if (cancel) {
+            if (taskCancelled()) {
                 setRawResult(Optional.<T>empty());
                 helpComplete();
             }
@@ -129,21 +134,12 @@
 
         private void cancelLaterSiblings() {
             FindFirstTask<S, T> parent = getParent();
-            if (parent != null) {
-                boolean foundMe = false;
-                for (FindFirstTask<S, T> child = parent.children; child != null; child = child.nextSibling) {
-                    if (child == this) {
-                        foundMe = true;
-                        continue;
-                    }
-                    if (foundMe)
-                        child.canceled = true;
-                }
-                // If we are the leftmost child of the parent, then we should cancel the parent's later siblings too
-                // We could be more aggressve, and actually complete the parent here when the leftmost child completes
-                if (parent.children == this)
-                    parent.cancelLaterSiblings();
-            }
+            for (FindFirstTask<S, T> sibling = this.nextSibling; sibling != null; sibling = sibling.nextSibling)
+                sibling.canceled = true;
+            // If we are the leftmost child of the parent, then we should cancel the parent's later siblings too
+            // We could be more aggressve, and actually complete the parent here when the leftmost child completes
+            if (parent != null && parent.children == this)
+                parent.cancelLaterSiblings();
         }
     }
 }
--- a/src/share/classes/java/util/streams/ops/MatchOp.java	Mon Nov 12 12:45:50 2012 -0500
+++ b/src/share/classes/java/util/streams/ops/MatchOp.java	Mon Nov 12 13:25:21 2012 -0500
@@ -105,6 +105,15 @@
         }
 
         @Override
+        public void compute() {
+            // Have we already found an answer?
+            if (answer.get() != null)
+                helpComplete();
+            else
+                super.compute();
+        }
+
+        @Override
         protected Boolean doLeaf() {
             boolean b = op.evaluate(helper.wrapIterator(spliterator.iterator()));
             if (b == op.matchKind.shortCircuitResult) {