Java JIT 循环展开策略?

发布于 2024-12-02 08:40:24 字数 376 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-12-09 08:40:24

如果 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.

╭ゆ眷念 2024-12-09 08:40:24

查看循环中是否正在执行循环展开的另一种方法是将 -XX:LoopUnrollLimit=1 指定为 运行代码时 JVM 的参数

如果您有一个可执行 jar,那么如何使用它的示例是:

java -XX:LoopUnrollLimit=1 -jar your-jar.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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文