Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java stream limit and skip behaviour when unordered and parallel

So I have been playing around with running streams in parallel and monitoring their behaviour based on the API documentation and other supporting material I have read.

I create two parallel streams and run distinct(), one where the stream is ordered and one where it is unordered. I then print the results using forEachOrdered() (to ensure I see the resulting encounter order of the stream after distinct has run), and can clearly see that the unordered version does not maintain the original ordering, but with a large dataset, would obviously enhance parallel performance.

There are API notes suggesting that the limit() and skip() operations should also run more efficiently in parallel when the stream is unordered as rather than having to retrieve the first n elements, you can get any n elements. I have tried to simulate this in the same way as above, but the result when ran in parallel with both ordered and unordered streams is always the same. In other words, when I print out the result after running limit, even for an unordered (parallel) stream, it has still always picked for first n elements?

Can anyone explain this? I tried varying the size of my input dataset and the value of n and it made no difference. I would have thought that it would grab any n elements and optimise for parallel performance? Has anyone actually seen this happen in practice, and could possibly provide a solution that showcases this behaviour consistently?

like image 498
Tranquility Avatar asked Jan 22 '16 11:01

Tranquility


People also ask

What is the difference between Skip() and limit() in Java?

Difference between skip () and limit () The limit (N) method returns first N elements in the encounter order of the Stream. The skip (N) discards the first N elements of the Stream. 4. Conclusion

What is the use of stream skip in Java?

Stream skip() method in Java with examples. The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements.

How do you limit a stream in Java?

Java Stream limit() method example. We can use Stream.limit(long maxSize) method to retrieve elements while they must not be greater than a certain maximum count. limit() method returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

What is skip n in Java?

The skip () Method The skip (n) method is an intermediate operation that discards the first n elements of a stream. The n parameter can't be negative, and if it's higher than the size of the stream, skip () returns an empty stream.


1 Answers

You probably tried to create the stream from SIZED/SUBSIZED source (like arrayList.stream(), Arrays.stream(array), IntStream.range(), etc.) and immediately issue limit or skip operation. This case is specially optimized in limit/skip implementation (see SliceOps) and runs with the same speed for both ordered and unordered stream (and actually runs very fast). If you remove such characteristics (for example, adding filtering step), you will see that making the stream unordered after that really helps. Write test like this:

input.stream().parallel().filter(x -> true).skip(..)...
input.stream().parallel().unordered().filter(x -> true).skip(..)...
input.stream().parallel().filter(x -> true).limit(..)...
input.stream().parallel().unordered().filter(x -> true).limit(..)...

Alternatively you may test with non SUBSIZED source (for example, TreeSet or HashSet).

like image 181
Tagir Valeev Avatar answered Jan 02 '23 12:01

Tagir Valeev