default random access list spliterator

Michael Hixson michael.hixson at
Mon Mar 7 09:02:34 UTC 2016

The default List.spliterator() is iterator-based.  Could this be
improved for random access lists, using List.get(int) to fetch
elements instead of List.iterator()?

I think it could improve most on Spliterator.trySplit().  The current
implementation allocates a new array for split-off elements.  I see
almost twice the throughput from list.parallelStream().forEach(...)
with a custom get(int)-based implementation over the default one.

For example, instead of this:

    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, Spliterator.ORDERED);

I'm suggesting something like this:

    default Spliterator<E> spliterator() {
        return (this instanceof RandomAccess)
                ? Spliterators.randomAccessListSpliterator(this)
                : Spliterators.spliterator(this, Spliterator.ORDERED);

where randomAccessListSpliterator is new code that looks a lot like


More information about the core-libs-dev mailing list