Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8148250

Stream.limit() parallel tasks with ordered non-SUBSIZED source should short-circuit

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 9
    • core-libs

      Currently individual tasks spawned by Stream.limit() operation (for ordered stream without SUBSIZED optimization) do not short-circuit, so even if individual task covers much more elements than explicitly specified in limit(), then processes all of them storing into memory buffer which increases execution time and memory consumption.

      Consider the following code, for example:

      AtomicInteger counter = new AtomicInteger();
      int[] result = IntStream.range(0, 1_000_000).parallel().filter(x -> true)
              .peek(x -> counter.incrementAndGet()).limit(10).toArray();
      System.out.println(Arrays.toString(result));
      System.out.println(counter.get());

      This code prints usually between 375000 and 625000 on 4 core system, while it has only 16 parallel tasks, so it's unreasonable to process more than 160 elements (at most 10 per every parallel task).

            tvaleev Tagir Valeev
            tvaleev Tagir Valeev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: