[PATCH] Inefficient ArrayList.subList().toArray()

Martin Buchholz martinrb at google.com
Fri Jan 26 06:20:52 UTC 2018


Yes.

        public Object[] toArray() {
            checkForComodification();
            return Arrays.copyOfRange(root.elementData, offset, offset +
size);
        }

        @SuppressWarnings("unchecked")
        public <T> T[] toArray(T[] a) {
            checkForComodification();
            if (a.length < size)
                return (T[]) Arrays.copyOfRange(
                        root.elementData, offset, offset + size,
a.getClass());
            System.arraycopy(root.elementData, offset, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }

It's still possible to find simple performance improvements in classes like
ArrayList.

On Thu, Jan 25, 2018 at 4:41 PM, John Rose <john.r.rose at oracle.com> wrote:

> On Jan 25, 2018, at 2:02 PM, Сергей Цыпанов <sergei.tsypanov at yandex.ru>
> wrote:
> >
> > +                return (T[]) Arrays.copyOfRange(root.elementData,
> offset, size, a.getClass());
>
>
> Giving this a quick glance:
> I think you may want s/size/offset+size/.
> There should be calls to checkForComodification.
>
>


More information about the core-libs-dev mailing list