Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

estimateSize() on sequential Spliterator

I'm implementing a Spliterator that explicitly restricts parallelization by having trySplit() return null. Would implementing estimateSize() offer any performance improvements for a stream produced by this spliterator? Or is the estimated size only useful for parallelization?

EDIT: To clarify, I'm specifically asking about an estimated size. In other words, my spliterator does not have the SIZED characteristic.

like image 798
shmosel Avatar asked Jun 07 '15 09:06

shmosel


People also ask

What is Spliterator in Java?

Like Iterator and ListIterator, Spliterator is a Java Iterator, which is used to iterate elements one-by-one from a List implemented object. Some important points about Java Spliterator are: Java Spliterator is an interface in Java Collection API. Spliterator is introduced in Java 8 release in java.

What is stream Spliterator?

stream(spliterator(), false); } default Stream<E> parallelStream() { return StreamSupport. stream(spliterator(), true); } ... Spliterator is an internal iterator that breaks the stream into the smaller parts. These smaller parts can be processed in parallel.


1 Answers

Looking at the call hierarchy to the relevant spliterator characteristic reveals that it's at least relevant for stream.toArray() performance

enter image description here

Additionally there is an equivalent flag in the internal stream implementation that seems to be used for sorting:

enter image description here

So aside from parallel stream operations the size estimate seems to be used for those two operations.

I don't claim exhaustiveness for my search, so just take these as examples.


Without the SIZED characteristic I can only find calls to estimateSize() that are relevant to parallel execution of the stream pipeline.

Of course this might change in the future or another Stream implementation than the standard JDK one could act differently.

like image 135
the8472 Avatar answered Sep 20 '22 18:09

the8472