Java JIT 循环展开策略?
JIT 的循环展开策略是什么?或者,如果没有简单的答案,那么有什么方法可以检查在循环中何时/何地执行循环展开?
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);
}
基本上,我上面的一段代码具有静态迭代次数(八次),当我按原样保留 for 循环时,它会表现得很糟糕。但是当我手动展开循环时,它的效果明显更好。我有兴趣了解 JIT 是否确实展开了循环,如果没有,那么为什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 JVM 展开循环,最好的答案可能是实际打印生成的程序集。请注意,这要求您的代码实际上作为热点执行(即 JVM 认为它值得进行昂贵的优化)。
为什么 JVM 以这种方式做出决定是一个更难的问题,可能需要深入分析 JIT 代码。
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.
查看循环中是否正在执行循环展开的另一种方法是将 -XX:LoopUnrollLimit=1 指定为 运行代码时 JVM 的参数。
如果您有一个可执行 jar,那么如何使用它的示例是:
此标志将
这将直接解决您的问题,而无需查看生成的程序集
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:
This flag will
And that'll directly address your question without needing to look at the generated assembly