RFR 8037106: Optimize Arrays.asList(...).forEach
Paul Sandoz
paul.sandoz at oracle.com
Fri Mar 14 16:10:48 UTC 2014
On Mar 14, 2014, at 4:46 PM, Martin Buchholz <martinrb at google.com> wrote:
> Looks good to me.
>
Thanks.
> I'm willing to believe for-loop over array is as efficient as fortran-style loop
>
> + for (E e : a) {
> + action.accept(e);
> + }
>
Yeah, i previously went through a whole bunch of code replacing such fortran-style loops with 'foreach' style based on automated code analysis.
> but then you could change the other loops as well, e.g. in indexOf
>
> + 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++)
>
I copied over code from ArrayList, but presumably the following is sufficient:
@Override
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
final E[] a = this.a;
for (int i = 0; i < a.length; i++) {
a[i] = operator.apply(a[i]);
}
}
? no need for a local size variable.
I changed my local patch to do the above.
Paul.
More information about the core-libs-dev
mailing list