本文翻译自:Does Java JIT cheat when running JDK code?
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. 我正在对一些代码进行基准测试,即使使用完全相同的算法,也无法像java.math.BigInteger
那样快速运行。 So I copied java.math.BigInteger
source into my own package and tried this: 所以我将java.math.BigInteger
源复制到我自己的包中并尝试了这个:
//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);
}
}
When I run this (jdk 1.8.0_144-b01 on MacOS) it outputs: 当我运行它(在MacOS上为jdk 1.8.0_144-b01)时,它输出:
12089nsec/mul
2559044166
When I run it with the import line uncommented: 当我使用取消注释的导入行运行它时:
4098nsec/mul
2559044166
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 . 使用BigInteger的JDK版本与我的版本相比,它几乎快三倍,即使它使用完全相同的代码 。
I've examined the bytecode with javap, and compared compiler output when running with options: 我用javap检查了字节码,并在运行选项时比较了编译器输出:
-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? 是什么解释了这种差异
#1楼
参考:https://stackoom.com/question/36dwM/Java-JIT在运行JDK代码时是否作弊
#2楼
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. 是的,HotSpot JVM有点“作弊”,因为它有一些你在Java代码中找不到的BigInteger
方法的特殊版本。 These methods are called JVM intrinsics . 这些方法称为JVM内在函数 。
In particular, BigInteger.multiplyToLen
is an instrinsic method in HotSpot. 特别是, BigInteger.multiplyToLen
是HotSpot中的一种内在方法。 There is a special hand-coded assembly implementation in JVM source base, but only for x86-64 architecture. JVM源代码库中有一个特殊的手工编码程序集实现 ,但仅适用于x86-64体系结构。
You may disable this instrinsic with -XX:-UseMultiplyToLenIntrinsic
option to force JVM to use pure Java implementation. 您可以使用-XX:-UseMultiplyToLenIntrinsic
选项禁用此-XX:-UseMultiplyToLenIntrinsic
以强制JVM使用纯Java实现。 In this case the performance will be similar to the performance of your copied code. 在这种情况下,性能将与您复制的代码的性能类似。
PS Here is a list of other HotSpot intrinsic methods. PS这是其他HotSpot内在方法的列表 。
#3楼
In Java 8 this is indeed an intrinsic method; 在Java 8中,这确实是一种内在的方法; a slightly modified version of the method: 稍微修改过的方法版本:
private static BigInteger test() {
Random r = new Random(1);
BigInteger c = null;
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);
c = a.multiply(b);
}
return c;
}
Running this with: 运行此:
java -XX:+UnlockDiagnosticVMOptions
-XX:+PrintInlining
-XX:+PrintIntrinsics
-XX:CICompilerCount=2
-XX:+PrintCompilation
<YourClassName>
This will print lots of lines and one of them will be: 这将打印许多行,其中一行将是:
java.math.BigInteger::multiplyToLen (216 bytes) (intrinsic)
In Java 9 on the other hand that method seems to not be an intrinsic anymore, but in turn it calls a method that is an intrinsic: 另一方面,在Java 9中 ,该方法似乎不再是内在的,而是反过来它调用一个内在的方法:
@HotSpotIntrinsicCandidate
private static int[] implMultiplyToLen
So running the same code under Java 9 (with the same parameters) will reveal: 因此,在Java 9下运行相同的代码(具有相同的参数)将揭示:
java.math.BigInteger::implMultiplyToLen (216 bytes) (intrinsic)
Underneath it's the same code for the method - just a slightly different naming. 在它下面是该方法的相同代码 - 只是稍微不同的命名。