I just read the Java Magazine article Loop Unrolling. There the authors demonstrate that simple for
loops with an int
counter are compiled with loop unrolling optimization:
private long intStride1()
{
long sum = 0;
for (int i = 0; i < MAX; i += 1)
{
sum += data[i];
}
return sum;
}
However, they follow by showing that everything changes by switching the counter type to long
:
private long longStride1()
{
long sum = 0;
for (long l = 0; l < MAX; l++)
{
sum += data[(int) l];
}
return sum;
}
This changes the output assembly by:
- Introducing Safepoints
- Not performing unrolling
This has an effect of drastically reducing throughput performance.
Why doesn't 64 bit HotSpot VM perform loop unrolling for long
counters? Why does the second case need safepoints, but the first one doesn't?