changeset 7791:90a7fdf1be54

Adjust target size estimate for decomposition; eliminate numChildren field from AbstractTask. Contributed-by: dl@cs.oswego.edu
author briangoetz
date Mon, 01 Apr 2013 15:43:05 -0400
parents e274aca6852b
children 2c96c1334dac
files src/share/classes/java/util/stream/AbstractTask.java src/share/classes/java/util/stream/NodeUtils.java
diffstat 2 files changed, 23 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/AbstractTask.java	Mon Apr 01 15:25:22 2013 -0400
+++ b/src/share/classes/java/util/stream/AbstractTask.java	Mon Apr 01 15:43:05 2013 -0400
@@ -82,6 +82,14 @@
 abstract class AbstractTask<P_IN, P_OUT, R, T extends AbstractTask<P_IN, P_OUT, R, T>>
         extends CountedCompleter<R> {
 
+    /**
+     * Default target number of leaf tasks for parallel decomposition.
+     * To allow load balancing, we over-partition, currently to approximately
+     * four tasks per processor, which enables others to help out
+     * if leaf tasks are uneven or some processors are otherwise busy.
+     */
+    static final int LEAF_TARGET = ForkJoinPool.getCommonPoolParallelism() << 2;
+
     /** The pipeline helper, common to all tasks in a computation */
     protected final PipelineHelper<P_IN, P_OUT> helper;
 
@@ -94,9 +102,6 @@
     /** Target leaf size */
     protected final long targetSize;
 
-    /** How many children does this task have? */
-    protected int numChildren;
-
     /**
      * This task's first child.  Children are stored in a linked list, using
      * the {@code nextSibling} field as the link to the next child.
@@ -161,8 +166,8 @@
 
     /** Suggests a target leaf size based on the initial size estimate */
     public static long suggestTargetSize(long sizeEstimate) {
-        long est = sizeEstimate / (ForkJoinPool.getCommonPoolParallelism() << 3);
-        return est > 0L ? est : 1L; // slack of 3; at least one
+        long est = sizeEstimate / LEAF_TARGET;
+        return est > 0L ? est : 1L;
     }
 
     /**
@@ -284,10 +289,9 @@
             else {
                 T leftChild = task.makeChild(split);
                 T rightChild = task.makeChild(task.spliterator);
-                task.setPendingCount(1);
-                task.numChildren = 2;
                 task.children = leftChild;
                 leftChild.nextSibling = rightChild;
+                task.setPendingCount(1);
                 leftChild.fork();
                 task = rightChild;
             }
@@ -304,7 +308,6 @@
     public void onCompletion(CountedCompleter<?> caller) {
         spliterator = null;
         children = null;
-        numChildren = 0;
     }
 
     /**
--- a/src/share/classes/java/util/stream/NodeUtils.java	Mon Apr 01 15:25:22 2013 -0400
+++ b/src/share/classes/java/util/stream/NodeUtils.java	Mon Apr 01 15:43:05 2013 -0400
@@ -330,6 +330,9 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
+                int numChildren = 0;
+                for (CollectorTask<T, U> cur = children; cur != null; cur = cur.nextSibling)
+                    ++numChildren;
                 @SuppressWarnings("unchecked")
                 Node<U>[] nodes = (Node<U>[]) new Node[numChildren];
                 int idx = 0;
@@ -513,6 +516,9 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
+                int numChildren = 0;
+                for (IntCollectorTask<T> cur = children; cur != null; cur = cur.nextSibling)
+                    ++numChildren;
                 Node.OfInt[] nodes = new Node.OfInt[numChildren];
                 int idx = 0;
                 for (IntCollectorTask<T> cur = children; cur != null; cur = cur.nextSibling)
@@ -693,6 +699,9 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
+                int numChildren = 0;
+                for (LongCollectorTask<T> cur = children; cur != null; cur = cur.nextSibling)
+                    ++numChildren;
                 Node.OfLong[] nodes = new Node.OfLong[numChildren];
                 int idx = 0;
                 for (LongCollectorTask<T> cur = children; cur != null; cur = cur.nextSibling)
@@ -872,6 +881,9 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (!isLeaf()) {
+                int numChildren = 0;
+                for (DoubleCollectorTask<T> cur = children; cur != null; cur = cur.nextSibling)
+                    ++numChildren;
                 Node.OfDouble[] nodes = new Node.OfDouble[numChildren];
                 int idx = 0;
                 for (DoubleCollectorTask<T> cur = children; cur != null; cur = cur.nextSibling)