RFR 8037106: Optimize Arrays.asList(...).forEach

Ulf Zibis Ulf.Zibis at CoSoCo.de
Mon Mar 17 18:36:09 UTC 2014


Am 17.03.2014 17:36, schrieb Paul Sandoz:
> Hi Sergey,
>
> Thanks, you are right! I did not realize it copied the array into a local variable, but that makes sense.
>
> Here is the byte code generated by javac (9) for two different methods:
> .....

Thanks from me too, this is great work.

I floated my question because of ...

Am 14.03.2014 16:46, schrieb Martin Buchholz:
> +            final int size = a.length;
> +            for (int i = 0; i < size; i++) {
>
> I like to write this using the idiom
>
> for (int i = 0, size = ...; i < size; i++)

This is not thread-proof (Given there is no: final E[] a = this.a;). If a becomes replaced 
concurrently while looping, size becomes invalid as length of a.

Good to know, that foreach over array is always thread-proof and additionally performs at least 
equal (except of the initial local caching) than a plain fortran-style loop.

-Ulf




More information about the core-libs-dev mailing list