RFR 8037106: Optimize Arrays.asList(...).forEach (original) (raw)
Paul Sandoz paul.sandoz at oracle.com
Mon Mar 17 16:36:25 UTC 2014
- Previous message: RFR 8037106: Optimize Arrays.asList(...).forEach
- Next message: RFR 8037106: Optimize Arrays.asList(...).forEach
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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:
void x() {
for (Object o : a) {
System.out.println(o);
}
}
0: aload_0
1: getfield #2 // Field a:[Ljava/lang/Object;
4: astore_1
5: aload_1
6: arraylength
7: istore_2
8: iconst_0
9: istore_3
10: iload_3
11: iload_2
12: if_icmpge 34
15: aload_1
16: iload_3
17: aaload
18: astore 4
20: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
23: aload 4
25: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
28: iinc 3, 1
31: goto 10
34: return
void y() {
Object[] a = this.a;
for (Object o : a) {
System.out.println(o);
}
}
0: aload_0
1: getfield #2 // Field a:[Ljava/lang/Object;
4: astore_1
5: aload_1
6: astore_2
7: aload_2
8: arraylength
9: istore_3
10: iconst_0
11: istore 4
13: iload 4
15: iload_3
16: if_icmpge 39
19: aload_2
20: iload 4
22: aaload
23: astore 5
25: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
28: aload 5
30: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
33: iinc 4, 1
36: goto 13
39: return
Paul.
On Mar 17, 2014, at 5:07 PM, Sergey Bylokhov <Sergey.Bylokhov at oracle.com> wrote:
On 3/17/14 7:41 PM, Paul Sandoz wrote:
On Mar 15, 2014, at 12:17 AM, Ulf Zibis <Ulf.Zibis at CoSoCo.de> wrote:
Am 14.03.2014 17:10, schrieb Paul Sandoz: 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 wouldn't this help a little more? : + final E[] a = this.a; + for (E e : a) { + action.accept(e); + } Thanks, i changed it to that. ? Why this line is needed: final E[] a = this.a; Since according to specification foreach over array should be transformed to: JLS 14.14.2: T[] #a = Expression; L1: L2: ... Lm: for (int #i = 0; #i < #a.length; #i++) { VariableModifiersopt TargetType Identifier = #a[#i]; Statement } So your code will be transformed to: + final E[] a = this.a; + E[] #a = a; + for (E e : #a) { + action.accept(e); + } Or I miss something? I more like the given style with less spaces: 3854 for (int i=0; i<a.length; i++) It better visualizes the 3 parts of the for statement. Subjectively that irritates my eyes :-) non-subjectively it is inconsistently applied. Paul. -- Best regards, Sergey.
- Previous message: RFR 8037106: Optimize Arrays.asList(...).forEach
- Next message: RFR 8037106: Optimize Arrays.asList(...).forEach
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]