JVM何时使用内在函数 [英] When will JVM use intrinsics

查看:111
本文介绍了JVM何时使用内在函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在JVM内部类中出现的某些代码模式会转换为内部函数,而从我自己的类调用时相同的模式则不然。

Why certain code patterns when present within JVM internal classes are turned into an intrinsic function, whereas the same patterns when called from my own class are not.

示例:

bitCount函数,当从Integer.bitCount(i)内调用时将变为固有。但是当复制到我的类中然后调用将需要更长的时间来执行。

bitCount function, when called from within Integer.bitCount(i) will be turned into an intrinsic. But when copied into my class and then called will take much longer to execute.

比较

Integer.bitCount(i) 
MyClass.bitCount(i) 


public static int bitCount(int i) {
    // HD, Figure 5-2
    i = i - ((i >>> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
    i = (i + (i >>> 4)) & 0x0f0f0f0f;
    i = i + (i >>> 8);
    i = i + (i >>> 16);
    return i & 0x3f;
}


推荐答案

答案很简单:内部函数是以这种方式定义的,因为存在获得函数结果的更快的本机方式,并且由于指定的映射而应用它。

The answer is simple: an intrinsic function is defined in this way because a faster, native way to obtain the result of the function exists and it is applied in case thanks to a specified mapping.

这不是与编译有关的东西。 Integer.bitCount 是特殊的,因为实现被标记为可以用本机asm指令替换 POPCNT 。基本上,当使用 Integer.bitCount 函数(如果CPU支持该指令)时,使用此本机指令,当您声明自己的函数副本时,使用正常实现。

That's not something related to compilation at all. Integer.bitCount is special in the sense that implementation is marked to be replaceable with a native asm instruction POPCNT. Basically this native instruction is used when using the Integer.bitCount function (if the CPU supports that instruction), when you declare your own copy of the function the normal implementation is used.

为什么JVM能够识别出可以优化该功能?因为它是硬编码的某处< JDK中的/ a>与代码的相似性无关。

Why JVM is able to recognize that the function can be optimized? Because it's hardcoded somewhere in the JDK, that has nothing to do with similarity to the code.

这篇关于JVM何时使用内在函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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