Does Java JIT cheat when running JDK code?

后端 未结 2 393
礼貌的吻别
礼貌的吻别 2020-12-04 04:49

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

2条回答
  •  渐次进展
    2020-12-04 05:24

    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.

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

    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.

    P.S. Here is a list of other HotSpot intrinsic methods.

提交回复
热议问题