java parallel stream splitter

Further to the issue with default JDK implementation with IteratorSpliterator, https://lwpro2.dev/2020/12/21/files-list-parallel-stream/

I have created a custom Spliterator to evenly split the stream and with threshold for the split size.

public class CacheSplitter<T> implements Spliterator<T> {
//    private final Collection<T> collection;
    private final T[] values;
    private int start;
    private int end;
    private final int THRESHOLD;
    public CacheSplitter(T[] values, int threshold) {
        this(values, 0, values.length, threshold);
    }
    public CacheSplitter(T[] values, int start, int end, int threshold) {
        this.values = values;
        this.start = start;
        this.end = end;
        this.THRESHOLD = threshold;
    }

    @Override
    public boolean tryAdvance(Consumer action) {
        if(start< end){
            action.accept(values[start++]);
            return true;
        }
        return false;
    }

    @Override
    public Spliterator trySplit() {
        if(end - start < THRESHOLD){
            return null;
        }
        int mid = (start + end)/2;
        return new CacheSplitter(values, start, start= mid+1, THRESHOLD);
    }

    @Override
    public long estimateSize() {
        return end - start;
    }

    @Override
    public int characteristics() {
        return ORDERED | SIZED | SUBSIZED | NONNULL;
    }
}

https://github.com/1wpro2/jdk-patch/blob/main/FixedSizeSpliterator.java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s