Java JIT loop unrolling policy?

痞子三分冷 提交于 2019-12-19 16:54:19

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/7243221/java-jit-loop-unrolling-policy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!