-
Enhancement
-
Resolution: Fixed
-
P4
-
9
-
b105
-
Verified
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).
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).