Parallel Streams Behave Sequentially… to a Point

综合编程 2018-05-24

In this post, I would like to spotlight a bit of the internal behavior of parallel streams in Java, a feature added in JDK 8. I will start from the source code and then try to explain what really happens in the context of our test example.

Basically, the code below declares two array lists, one of size 8192 and the other of 8193, then creates parallel streams out of them, and, afterward, tries to sort the arrays.

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, timeUnit = TimeUnit.NANOSECONDS)
@Measurement(iterations = 5, timeUnit = TimeUnit.NANOSECONDS)
@Fork(value = 3, warmups = 1)
@State(Scope.Benchmark)
public class SortStreamJmh {

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(SortStreamJmh.class.getName())
                .verbosity(VerboseMode.SILENT)
                .build();

        new Runner(opt).run();
    }

    @Param({ "8192", "8193" })
    int arraySize;

    List list = new ArrayList();

    @Setup
    public void setupList(){
        Random random = new Random(26);
        for (int i = 0; i < arraySize; i ++) {
            String r = generateRandomWord(random, 2);
            list.add(r);
        }
    }

    @Benchmark
    public Object[] sort() {
        Object[] result = list.parallelStream()
                .sorted()
                .toArray();

        return result;
    }

    private static String generateRandomWord(Random random, int wordLength) {
        StringBuilder sb = new StringBuilder(wordLength);
        for(int i = 0; i < wordLength; i++) {
            char tmp = (char)('a' +  random.nextInt('z' - 'a')); // Generate a letter between a and z
            sb.append(tmp); // Add it to the String
        }
        return sb.toString();
    }

}

Test output:

Benchmark       (arraySize)     Mode      Cnt       Score   Error        Units
SortStreamJmh.sort         8192      avgt       15    1711.595 ± 51060.627    us/op
SortStreamJmh.sort         8193      avgt       15     944.169 ± 28012.014    us/op

Tests triggered using JDK 10 (latest JDK release at the moment) on my machine (CPU: Intel i7-6700HQ Skylake; MEMORY: 16GB DDR4 2133 MHz; OS: Ubuntu 16.04.2)

As we might notice, the bigger array (i.e. 8193) takes less time to sort the Strings (~2x faster) in comparison to the smaller one (i.e. 8192). However, even if the arrays' lengths are almost equal (i.e. their sizes differ by only one element: 8192 vs. 8193), the performance is noticeable! How can we explain this?

Let's jump into the JDK sources inside the java.util.Arrays.java
class:

public static <T extends Comparable> void parallelSort(T[] a) {
    int n = a.length, p, g;
    if (n <= MIN_ARRAY_SORT_GRAN ||  // where MIN_ARRAY_SORT_GRAN = 1 << 13
        (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
            // sequential sort
            TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);
    else
        // parallel sort
        new ArraysParallelSortHelpers.FJObject.Sorter 
            (null, a, (T[])Array.newInstance(a.getClass().getComponentType(), n),
                0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
                MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
}

The JDK source code reveals an interesting fact:

  • If the array length is below a certain granularity (e.g. MIN_ARRAY_SORT_GRAN = 1 << 13
    which corresponds to 8192
    ), the array is not partitioned anymore and is sequentially sorted using Arrays.sort()
    , even if at the code level the programmer explicitly requires a parallel stream!
  • Otherwise, the array is partitioned and a ForkJoin
    pool is used to execute parallel tasks

Getting back to our example, we can summarize:

  • The 8192 array length is sequentially sorted.
  • The 8193 array length is split into parallel sub-tasks handled by the ForkJoin pool.

Which explains why, despite a slightly larger length, the 8193 array is faster.

Back to a bit of theory, there are few recommendations from Brian Goetz
on his great article Parallel stream performance
about the rationale of splitting a source, including when it makes sense to go parallel and when to stick with the sequential approach. One of the guidelines includes the NQ model
, which states:

NQ Model: The larger the product of NxQ is, the more likely it is to get a parallel speedup!

N: Number of data elements

Q: Amount of work performed per element

Note:For problems with a trivially small Q (e.g. sorting, addition), generally N should be greater than 10,000 to get a speedup and to make sense to parallelize!

It might be a reasonable explanation for our test case as well, where JDK sources rely on an explicit threshold 1<<13
to avoid parallelizing Streams, where the size is below that certain specified value (e.g. 1 << 13 = 8193)!

Reference

Javalobby

责编内容by:Javalobby (源链)。感谢您的支持!

您可能感兴趣的

JDK 11 Early Access Build 8 In the message " JDK 11 Early Access build 8 available ," Muneer Kolarkunnu announces that " JDK 11 EA build 8 , under both the GPL and O...
JDK1.8 Cannot change version of project facet Dyna... Problem: Error:JDK1.8 Cannot change version of project facet Dynamic Web Module to 3.1.This project is called mybatis: maven webapp project:'myb...
JDK 10 本周将进入候选发布阶段,新特性抢先看... Java 平台首席架构师 Mark Reinhold 近日在 邮件列表 上表示,按计划,JDK 10 将于 2 月 8 日星期四进入候选发布(Release Candidate)阶段。他建议 JDK 10 在该阶段采用和 JDK 9 相同的 发布流程 ,将 Bug 修复分为 P1 - P5 五个不...
Immutable vs. Unmodifiable in JDK 10 Immutable vs. Unmodifiable in JDK 10 DZone's Guide to Immutable vs. Unmodifiable in JDK 10 See how Java 10 is preparing to more ful...
JDK 9: Creating a Java Runtime Image With Maven At the moment, JDK 9 is only available as Early Access (EA) to let the community take a look how it works and what can be improved. Apart from all...