Allocating array that don't need to be initialized to 0?
Peter B. Kessler
Peter.B.Kessler at Oracle.COM
Thu Jun 27 18:47:53 PDT 2013
I'm trying to write Java code that will make it obvious to the runtime compiler that it doesn't need to zero all the elements because I'm going to be initializing them all myself. I thought our compiler was smart enough to do that.
Even something as "obvious" as
public static double[] initializedDouble(int len) {
final double[] result = new double[len];
for (int i = 0; i < result.length; i += 1) {
result[i] = 3.14159;
}
return result;
}
generates
059 LEA EDI,[EBX + #16]
05c MOV ECX,EBP
05e ADD ECX,#2
061 AND ECX,#536870911
067 ADD ECX,#-2
06a SHL ECX,1 # Convert doublewords to words
XOR EAX,EAX
REP STOS # store EAX into [EDI++] while ECX--
(Where I think the math on ECX is a non-overflowing conversion from the count of the number of 64-bit doubles to a count of the number of 32-bit stores that need to be done.) But the effect seems to write 0's to all the allocated memory. That's what I'm trying to avoid. Those instructions are followed almost immediately by
08b MOVSD XMM0a,[constant table base + #0] # load from constant table: double=#3.141590
....
0c0 B11: # B11 B12 <- B10 B11 Loop: B11-B11 inner main of N115 Freq: 4.99973
0c0 MOVSD [EBX + #16 + EDX << #3],XMM0a
0c6 MOVSD [EBX + #24 + EDX << #3],XMM0a
0cc MOVSD [EBX + #32 + EDX << #3],XMM0a
0d2 MOVSD [EBX + #40 + EDX << #3],XMM0a
0d8 ADD EDX,#4
0db CMP EDX,EDI
0dd Jl,s B11 # Loop end P=0.833326 C=97255.000000
0dd
0df B12: # B14 B13 <- B10 B11 Freq: 0.833325
0df CMP EDX,EBP
0e1 Jge,s B14 P=0.500000 C=-1.000000
nop # 1 bytes pad for loops and calls
0e4 B13: # B13 B14 <- B12 B13 Loop: B13-B13 inner post of N154 Freq: 0.833325
0e4 MOVSD [EBX + #16 + EDX << #3],XMM0a
0ea INC EDX
0eb CMP EDX,EBP
0ed Jl,s B13 # Loop end P=0.500000 C=97255.000000
An unrolled loop of stride 4 followed by loop of stride 1 to set the elements of the array to 3.14159.
Is there a way to write my Java code so that I don't drag the array through the memory system twice?
Thanks for any advice you can give me on this.
... peter
P.S. I'm using JDK-7u25. If this has gotten better in a later update, I could try that. If I need some compiler flag to enable this optimization, I could try that if it's a product flag.
More information about the hotspot-compiler-dev
mailing list