changeset 8112:2532d144a93d

Change AbstractTask to use left and right child.
author psandoz
date Mon, 15 Apr 2013 11:42:47 +0200
parents 98e2285ff91f
children 10ad6bc63cbd
files src/share/classes/java/util/stream/AbstractShortCircuitTask.java src/share/classes/java/util/stream/AbstractTask.java src/share/classes/java/util/stream/FindOps.java src/share/classes/java/util/stream/Nodes.java src/share/classes/java/util/stream/ReduceOps.java src/share/classes/java/util/stream/SliceOps.java
diffstat 6 files changed, 52 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/AbstractShortCircuitTask.java	Sun Apr 14 17:59:37 2013 -0400
+++ b/src/share/classes/java/util/stream/AbstractShortCircuitTask.java	Mon Apr 15 11:42:47 2013 +0200
@@ -173,16 +173,19 @@
 
     /**
      * Cancels all tasks which succeed this one in the encounter order.  This
-     * includes canceling all the current task's later siblings, as well as the
-     * later siblings of all its parents.
+     * includes canceling all the current task's right sibling, as well as the
+     * later right siblings of all its parents.
      */
     protected void cancelLaterNodes() {
-        K parent = getParent();
-        for (K sibling = this.nextSibling; sibling != null; sibling = sibling.nextSibling)
-            if (!sibling.canceled)
-                sibling.canceled = true;
-        // Go up the tree, cancel later siblings of all parents
-        if (parent != null)
-            parent.cancelLaterNodes();
+        // Go up the tree, cancel right siblings of this node and all parents
+        for (K parent = getParent(), node = (K) this; parent != null;
+             node = parent, parent = parent.getParent()) {
+            // If node is a left child of parent, then has a right sibling
+            if (parent.leftChild == node) {
+                K rightSibling = parent.rightChild;
+                if (!rightSibling.canceled)
+                    rightSibling.canceled = true;
+            }
+        }
     }
 }
--- a/src/share/classes/java/util/stream/AbstractTask.java	Sun Apr 14 17:59:37 2013 -0400
+++ b/src/share/classes/java/util/stream/AbstractTask.java	Mon Apr 15 11:42:47 2013 +0200
@@ -105,13 +105,18 @@
     protected final long targetSize;
 
     /**
-     * This task's first child.  Children are stored in a linked list, using
-     * the {@code nextSibling} field as the link to the next child.
+     * The left child.
+     * null if no children
+     * if non-null rightChild is non-null
      */
-    protected K children;
+    protected K leftChild;
 
-    /** Next sibling of this task */
-    protected K nextSibling;
+    /**
+     * The right child.
+     * null if no children
+     * if non-null leftChild is non-null
+     */
+    protected K rightChild;
 
     /** The result of this node, if completed */
     private R localResult;
@@ -233,7 +238,7 @@
      * positive.
      */
     protected boolean isLeaf() {
-        return children == null;
+        return leftChild == null;
     }
 
     /**
@@ -281,13 +286,11 @@
                 return;
             }
             else {
-                K leftChild = task.makeChild(split);
-                K rightChild = task.makeChild(task.spliterator);
-                task.children = leftChild;
-                leftChild.nextSibling = rightChild;
+                K l = task.leftChild = task.makeChild(split);
+                K r = task.rightChild = task.makeChild(task.spliterator);
                 task.setPendingCount(1);
-                leftChild.fork();
-                task = rightChild;
+                l.fork();
+                task = r;
             }
         }
     }
@@ -302,7 +305,7 @@
     @Override
     public void onCompletion(CountedCompleter<?> caller) {
         spliterator = null;
-        children = null;
+        leftChild = rightChild = null;
     }
 
     /**
@@ -329,7 +332,7 @@
         K node = (K) this;
         while (node != null) {
             K parent = node.getParent();
-            if (parent != null && parent.children != node)
+            if (parent != null && parent.leftChild != node)
                 return false;
             node = parent;
         }
--- a/src/share/classes/java/util/stream/FindOps.java	Sun Apr 14 17:59:37 2013 -0400
+++ b/src/share/classes/java/util/stream/FindOps.java	Mon Apr 15 11:42:47 2013 +0200
@@ -289,7 +289,8 @@
         @Override
         public void onCompletion(CountedCompleter<?> caller) {
             if (op.mustFindFirst) {
-                for (FindTask<P_IN, P_OUT, O> child = children; child != null; child = child.nextSibling) {
+                    for (FindTask<P_IN, P_OUT, O> child = leftChild, p = null; child != p;
+                         p = child, child = rightChild) {
                     O result = child.getLocalResult();
                     if (result != null && op.presentPredicate.test(result)) {
                         setLocalResult(result);
--- a/src/share/classes/java/util/stream/Nodes.java	Sun Apr 14 17:59:37 2013 -0400
+++ b/src/share/classes/java/util/stream/Nodes.java	Mon Apr 15 11:42:47 2013 +0200
@@ -2331,14 +2331,9 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
-                int numChildren = 0;
-                for (CollectorTask<P_IN, P_OUT> cur = children; cur != null; cur = cur.nextSibling)
-                    ++numChildren;
-                @SuppressWarnings("unchecked")
-                Node<P_OUT>[] nodes = (Node<P_OUT>[]) new Node[numChildren];
-                int idx = 0;
-                for (CollectorTask<P_IN, P_OUT> cur = children; cur != null; cur = cur.nextSibling)
-                    nodes[idx++] = cur.getLocalResult();
+                Node<P_OUT>[] nodes = (Node<P_OUT>[]) new Node[2];
+                nodes[0] = leftChild.getLocalResult();
+                nodes[1] = rightChild.getLocalResult();
                 setLocalResult(new ConcNode<>(nodes));
             }
             super.onCompletion(caller);
@@ -2373,14 +2368,9 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
-                int numChildren = 0;
-                for (IntCollectorTask<P_IN> cur = children; cur != null; cur = cur.nextSibling)
-                    ++numChildren;
-                Node.OfInt[] nodes = new Node.OfInt[numChildren];
-                int idx = 0;
-                for (IntCollectorTask<P_IN> cur = children; cur != null; cur = cur.nextSibling)
-                    nodes[idx++] = cur.getLocalResult();
-
+                Node.OfInt[] nodes = new Node.OfInt[2];
+                nodes[0] = leftChild.getLocalResult();
+                nodes[1] = rightChild.getLocalResult();
                 setLocalResult(new IntConcNode(nodes));
             }
             super.onCompletion(caller);
@@ -2415,14 +2405,9 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
-                int numChildren = 0;
-                for (LongCollectorTask<P_IN> cur = children; cur != null; cur = cur.nextSibling)
-                    ++numChildren;
-                Node.OfLong[] nodes = new Node.OfLong[numChildren];
-                int idx = 0;
-                for (LongCollectorTask<P_IN> cur = children; cur != null; cur = cur.nextSibling)
-                    nodes[idx++] = cur.getLocalResult();
-
+                Node.OfLong[] nodes = new Node.OfLong[2];
+                nodes[0] = leftChild.getLocalResult();
+                nodes[1] = rightChild.getLocalResult();
                 setLocalResult(new LongConcNode(nodes));
             }
             super.onCompletion(caller);
@@ -2458,14 +2443,9 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
-                int numChildren = 0;
-                for (DoubleCollectorTask<P_IN> cur = children; cur != null; cur = cur.nextSibling)
-                    ++numChildren;
-                Node.OfDouble[] nodes = new Node.OfDouble[numChildren];
-                int idx = 0;
-                for (DoubleCollectorTask<P_IN> cur = children; cur != null; cur = cur.nextSibling)
-                    nodes[idx++] = cur.getLocalResult();
-
+                Node.OfDouble[] nodes = new Node.OfDouble[2];
+                nodes[0] = leftChild.getLocalResult();
+                nodes[1] = rightChild.getLocalResult();
                 setLocalResult(new DoubleConcNode(nodes));
             }
             super.onCompletion(caller);
--- a/src/share/classes/java/util/stream/ReduceOps.java	Sun Apr 14 17:59:37 2013 -0400
+++ b/src/share/classes/java/util/stream/ReduceOps.java	Mon Apr 15 11:42:47 2013 +0200
@@ -718,16 +718,11 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
-                ReduceTask<P_IN, P_OUT, R, S> child = children;
-                S result = child.getLocalResult();
-                child = child.nextSibling;
-                for (; child != null; child = child.nextSibling) {
-                    S otherResult = child.getLocalResult();
-                    result.combine(otherResult);
-                    child.setLocalResult(null); // GC otherResult
-                }
-                setLocalResult(result);
+                S leftResult = leftChild.getLocalResult();
+                leftResult.combine(rightChild.getLocalResult());
+                setLocalResult(leftResult);
             }
+            // GC spliterator, left and right child
             super.onCompletion(caller);
         }
     }
--- a/src/share/classes/java/util/stream/SliceOps.java	Sun Apr 14 17:59:37 2013 -0400
+++ b/src/share/classes/java/util/stream/SliceOps.java	Mon Apr 15 11:42:47 2013 +0200
@@ -329,9 +329,7 @@
         @Override
         public final void onCompletion(CountedCompleter<?> caller) {
             if (!isLeaf()) {
-                thisNodeSize = 0;
-                for (SliceTask<P_IN, P_OUT> child = children; child != null; child = child.nextSibling)
-                    thisNodeSize += child.thisNodeSize;
+                thisNodeSize = leftChild.thisNodeSize + rightChild.thisNodeSize;
                 completed = true;
 
                 if (isRoot()) {
@@ -366,7 +364,8 @@
                 return 0;
             else {
                 long leftSize = 0;
-                for (SliceTask<P_IN, P_OUT> child = children; child != null; child = child.nextSibling) {
+                for (SliceTask<P_IN, P_OUT> child = leftChild, p = null; child != p;
+                     p = child, child = rightChild) {
                     if (child.completed)
                         leftSize += child.thisNodeSize;
                     else {
@@ -380,7 +379,8 @@
 
         private void visit(List<Node<P_OUT>> results, int offset) {
             if (!isLeaf()) {
-                for (SliceTask<P_IN, P_OUT> child = children; child != null; child = child.nextSibling) {
+                for (SliceTask<P_IN, P_OUT> child = leftChild, p = null; child != p;
+                     p = child, child = rightChild) {
                     child.visit(results, offset);
                     offset += child.thisNodeSize;
                 }