Java JIT在运行JDK代码时是否作弊? [英] Does Java JIT cheat when running JDK code?

查看:207
本文介绍了Java JIT在运行JDK代码时是否作弊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对某些代码进行基准测试,但我无法像 java.math.BigInteger ,即使使用完全相同的算法也是如此。
所以我复制了 java.math.BigInteger 源代码进入我自己的软件包并尝试过:

I was benchmarking some code, and I could not get it to run as fast as with java.math.BigInteger, even when using the exact same algorithm. So I copied java.math.BigInteger source into my own package and tried this:

//import java.math.BigInteger;

public class MultiplyTest {
    public static void main(String[] args) {
        Random r = new Random(1);
        long tm = 0, count = 0,result=0;
        for (int i = 0; i < 400000; i++) {
            int s1 = 400, s2 = 400;
            BigInteger a = new BigInteger(s1 * 8, r), b = new BigInteger(s2 * 8, r);
            long tm1 = System.nanoTime();
            BigInteger c = a.multiply(b);
            if (i > 100000) {
                tm += System.nanoTime() - tm1;
                count++;
            }
            result+=c.bitLength();
        }
        System.out.println((tm / count) + "nsec/mul");
        System.out.println(result); 
    }
}

当我运行时(jdk 1.8.0_144-b01)在MacOS上)它输出:

When I run this (jdk 1.8.0_144-b01 on MacOS) it outputs:

12089nsec/mul
2559044166

当我使用取消注释的导入行运行它时:

When I run it with the import line uncommented:

4098nsec/mul
2559044166

使用JDK版本的速度几乎快3倍BigInteger与我的版本相比,即使它使用完全相同的代码。

It's almost three times as fast when using the JDK version of BigInteger versus my version, even if it's using the exact same code.

我用javap检查了字节码,并在运行选项时比较了编译器输出:

I've examined the bytecode with javap, and compared compiler output when running with options:

-Xbatch -XX:-TieredCompilation -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions 
-XX:+PrintInlining -XX:CICompilerCount=1

两个版本似乎生成相同的代码。
那么使用一些我在代码中无法使用的预先计算优化的热点是什么?我一直都明白他们没有。
是什么解释了这种差异?

and both versions seem to generate the same code. So is hotspot using some precomputed optimisations that I can't use in my code? I always understood that they don't. What explains this difference?

推荐答案

是的,HotSpot JVM有点作弊,因为它有一个特殊的在Java代码中找不到的某些 BigInteger 方法的版本。这些方法称为 JVM内在函数

Yes, HotSpot JVM is kind of "cheating", because it has a special version of some BigInteger methods that you won't find in Java code. These methods are called JVM intrinsics.

特别是, BigInteger.multiplyToLen 是HotSpot中的内在方法。有一个特殊的 JVM源代码库中的手工编码汇编实现,但仅适用于x86-64架构。

In particular, BigInteger.multiplyToLen is instrinsic method in HotSpot. There is a special hand-coded assembly implementation in JVM source base, but only for x86-64 architecture.

您可以使用禁用此instrinsic -XX:-UseMultiplyToLenIntrinsic 选项,强制JVM使用纯Java实现。在这种情况下,性能将与您复制的代码的性能类似。

You may disable this instrinsic with -XX:-UseMultiplyToLenIntrinsic option to force JVM to use pure Java implementation. In this case the performance will be similar to the performance of your copied code.

PS 这是列表

这篇关于Java JIT在运行JDK代码时是否作弊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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