Using Array.newInstance still generates checkcast instructions

Vitaly Davidovich vitalyd at gmail.com
Wed Oct 28 21:04:38 UTC 2015


No difference in codegen.  I also wouldn't expect any here since the
Array.newInstance call and the read of the array in get() are
"disconnected" as far as JIT is concerned - once the constructor returns,
any type info inferred in the constructor of ArrayList is gone.  This is
different from Arrays.copyOfRange() where the array instantiation and read
from it are performed in one CFG.

On Wed, Oct 28, 2015 at 4:51 PM, John Rose <john.r.rose at oracle.com> wrote:

> On Oct 28, 2015, at 12:54 PM, Vitaly Davidovich <vitalyd at gmail.com> wrote:
>
>
> what should my example ArrayList look like
>
>
> Try changing your ArrayList class constructor:
>
>   -   ArrayList(final Class<T> klass, final int size) {
>   -      _items = (T[]) Array.newInstance(klass, size);
>   +   ArrayList(final Class<T[]> arrayKlass, final int size) {
>   +      _items = (T[]) Array.newInstance(arrayKlass.getComponentType(),
> size);
>
> That will make the source code shape more like Arrays.copyOfRange, and
> what the optimizer expects.
>
> YMMV
>
> — John
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20151028/ed1758cb/attachment.html>


More information about the hotspot-compiler-dev mailing list