java for循环中的分支预测 [英] Branch prediction in a java for loop

查看:315
本文介绍了java for循环中的分支预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在旁边看到此评论,如果条件:


//分支预测有利于最常用的条件

在<的源代码中a href =http://docs.oracle.com/javase/8/javase-clienttechnologies.htm =nofollow> JavaFX SkinBase class。

in the source code of the JavaFX SkinBase class.

protected double computeMinWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {

    double minX = 0;
    double maxX = 0;
    boolean firstManagedChild = true;
    for (int i = 0; i < children.size(); i++) {
        Node node = children.get(i);
        if (node.isManaged()) {
            final double x = node.getLayoutBounds().getMinX() + node.getLayoutX();
            if (!firstManagedChild) {  // branch prediction favors most often used condition
                minX = Math.min(minX, x);
                maxX = Math.max(maxX, x + node.minWidth(-1));
            } else {
                minX = x;
                maxX = x + node.minWidth(-1);
                firstManagedChild = false;
            }
        }
    }
    double minWidth = maxX - minX;
    return leftInset + minWidth + rightInset;
}

我相信开发人员想解释他为什么写了否定if

I believe that the developer want to explain why he wrote a negate if.

这个优化真的很有用吗?

is this optimization really useful ?

推荐答案

JavaFX团队非常注重性能,因此他们肯定知道切换if和else对分支预测没有实质性影响。

The guys of the JavaFX team are pretty focused on performance so they surely know that switching the if and the else has no material effect on branch prediction.

我想这表明它存在重构的性能没有显着改善:

I imagine that it is an indication that there is no significant performance improvement to refactor as:

double minX = Double.MAX_VALUE;
double maxX = Double.MIN_VALUE;

for (int i = 0; i < children.size(); i++) {
  Node node = children.get(i);
  if (node.isManaged()) {
    final double x = node.getLayoutBounds().getMinX() + node.getLayoutX();
    minX = Math.min(minX, x);
    maxX = Math.max(maxX, x + node.minWidth(-1));
  }
}

因为分支预测基本上将if / else变为类似的东西(给予或接受)。

Because branch prediction will essentially turn the if/else into something like that for you (give or take).

这个提交确认了这个解释,虽然我不确定他们为什么改变它 - 可能是因为它在<$ c $时有副作用c> isManaged 条件永远不是真的......

This commit confirms this explanation, although I'm not sure why they changed it - possibly because it had side effects when the isManaged condition was never true...

进一步调查,提交是指一个错误控件基准测试中高达50%的性能回归(您需要登录才能访问它)。

Investigating further, the commit refers to a bug "up to 50% performance regression in Controls benchmarks" (you need to login to access it).

似乎他们遇到了性能问题,而且调查时发现代码中存在错误(类似于我上面的示例)。他们修复了这个错误并添加了一条评论,以澄清该修复程序不会影响性能,这要归功于分支预测。

It seems that they had a performance issue and while investigating noticed that there was a bug in the code (that was similar to what my example above). They fixed the bug and added a comment to clarify that the fix did not affect performance thanks to branch prediction.

摘录:




[..]查看代码,我注意到在没有管理皮肤的孩子的情况下出现错误。在这种情况下,minX / Y和maxX / Y最终将为MAX / MIN_VALUE,我们真的希望它们为零。因此,此更改集修复了该问题。在测试中,我看到了性能上的小(~1 fps)改进,所以我认为这种改变并不能解决性能问题。无论如何,代码必须是它的方式。

[..] looking at the code, I noticed an error in the case where none of the skin's children are managed. In such a case, minX/Y and maxX/Y would end up being MAX/MIN_VALUE where we really want them to be zero. So this change set fixes that issue. In testing, I saw a small (~1 fps) improvement in performance, so I don't think this change fixes the performance problem. Regardless, the code must be the way it is.

[...]请注意,使用MIN / MAX时存在错误 - 这些值不是Float和Double的最大值和最小值(即与积分类型对称是有意义的,但它不是指定的方式)。要对浮点值执行最小/最大操作,您需要使用NEGATIVE / POSITIVE_INFINITY值来实现您要查找的结果。

[...] Note that there was a bug in the use of MIN/MAX - those values are not the largest and smallest values for Float and Double (that would have made sense wrt symmetry with the integral types, but it isn't the way they were specified). For doing min/max operations on floating point values you want to use the NEGATIVE/POSITIVE_INFINITY values instead to achieve the results you are looking for.

这篇关于java for循环中的分支预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆