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

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

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:

      Description

      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).

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: