10

What is the loop unrolling policy for JIT? Or if there is no simple answer to that, then is there some way i can check where/when loop unrolling is being performed in a loop?

GNode child = null;
for(int i=0;i<8;i++){
   child = octree.getNeighbor(nn, i, MethodFlag.NONE);
   if(child==null)
      break;
   RecurseForce(leaf, child, dsq, epssq);
}

Basically, i have a piece of code above that has a static number of iterations (eight), and it does bad when i leave the for loop as it is. But when i manually unroll the loop, it does significantly better. I am interested in finding out if the JIT actually does unroll the loop, and if not, then why.

R.K
  • 153
  • 1
  • 7
  • 7
    That's a *very, very* specific question that you'll need to analyze for a *specific version* of a *specific JVM* on a *specific platform*. And I guess they "why" can only ever be answered by in-depth analysis of the JIT code. – Joachim Sauer Aug 30 '11 at 12:37
  • You left out one crucial detail: Which JVM are you using? – aioobe Aug 30 '11 at 12:38
  • @aioobe Its Java Hotspot 64 bit, 1.6.x – R.K Aug 30 '11 at 14:02

2 Answers2

6

If the JVM unrolls the loop is probably best answered by actually printing the generated assembly. Note that this requires your code to actually be executed as a hot spot (i.e. the JVM considers it worthy of the expensive optimizations).

Why the JVM decides one way or another is a much harder question and probably requires in-depth analysis of the JIT code.

agillgilla
  • 859
  • 1
  • 7
  • 22
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Thanks. Printing the assembly would be a possible way, but I was wondering if there was any exposed API to look into this. Something like a method that is called whenever loop unrolling is done by the JIT, and i can count the number of times it is called as well as the context within which it is called. I looked through the JVMTI and did not find anything. – R.K Aug 30 '11 at 14:07
  • I'm pretty sure that there is no such API, simply because this is **highly** implementation-dependent and only of limited use for *most* applications. – Joachim Sauer Aug 30 '11 at 14:37
  • Thanks. I guess i was expecting too much after playing with JVMTI and Eclipse JDT. – R.K Aug 30 '11 at 14:47
  • 2
    Actually the whole thing is not only implementation and code specific, one can even influence it with command line options. Goo look at `-XX:LoopUnrollLimit=`. – Voo Aug 30 '11 at 22:57
1

Another way to see if loop unrolling is being performed in the loop is to specify -XX:LoopUnrollLimit=1 as an argument to the JVM when running your code.

If you have an executable jar, then an example of how you can use this is:

java -XX:LoopUnrollLimit=1 -jar your-jar.jar

This flag will

Unroll loop bodies with server compiler intermediate representation node count less than this value

And that'll directly address your question without needing to look at the generated assembly

Cuga
  • 17,668
  • 31
  • 111
  • 166