什么是Java和.NET之间的差异在JIT差异、Java、NET、JIT

2023-09-04 00:53:29 作者:一颗心、只为你安分守己

我知道微软.NET使用CLR的JIT编译器,而Java有热点。它们之间有什么区别呢?

I know Microsoft .NET uses the CLR as a JIT compiler while Java has the Hotspot. What Are the differences between them?

推荐答案

他们是非常不同的东西。正如人们所指出的,CLR编译成机器code才执行了一块MSIL的。这使得它除了典型的死区code消除和内联关士兵的优化利用目标机器的特定CPU架构的优势(虽然我不知道是否它的话)。这也导致命中为每个类(虽然编译器是相当快的,许多平台库都只是薄薄的一层在Win32 API的)。

They are very different beasts. As people pointed out, the CLR compiles to machine code before it executes a piece of MSIL. This allows it in addition to the typical dead-code elimination and inlining off privates optimizations to take advantage of the particular CPU architecture of the target machine (though I'm not sure whether it does it). This also incurs a hit for each class (though the compiler is fairly fast and many platform libraries are just a thin layer over the Win32 API).

的HotSpot虚拟机正在采取不同的方法。它规定,大部分的code很少执行,因此不值得花时间编译它。所有字节code开头间preTED模式。在VM保持统计在调用点和尝试识别这是所谓的次数多于一个predefined数的方法。然后,只编译这些方法具有快速JIT编译器(C1)和交换在运行时的方法(这是HS的特制的酱汁)。之后,C1-编译方法被调用多一些时间,同样的方法编译时慢,但成熟的编译器和code是飞再次交换。

The HotSpot VM is taking a different approach. It stipulates that most of the code is executed rarely, hence it's not worth to spend time compiling it. All bytecode starts in interpreted mode. The VM keeps statistics at call-sites and tries to identify methods which are called more than a predefined number of times. Then it compiles only these methods with a fast JIT compiler (C1) and swaps the method while it is running (that's the special sauce of HS). After the C1-compiled method has been invoked some more times, the same method is compiled with slow, but sophisticated compiler and the code is swapped again on the fly.

由于热点可以换,而他们正在运行的方法,虚拟机的编译器可以执行一些投机性的优化是不安全的静态编译code。一个典型的例子是单态调用静态调度/内联(多态的方法只有一个实现)。这是如果VM看到,该方法总是解析为相同的目标进行。曾经被认为是复杂的调用被降低到几CPU指令后卫,这是pdicted和流水线由现代CPU $ P $。当保护条件不再是真实的,虚拟机可以采取不同的code路径,甚至回落到除preting模式。基于统计和程序工作量,所生成的机器code可被在不同的时间不同。许多优化依赖于程序执行过程中收集的信息,而且不可能的,如果你一次编译焕你加载类。

Since HotSpot can swap methods while they are running, the VM compilers can perform some speculative optimizations that are unsafe in statically compiled code. A canonical example is static dispatch / inlining of monomorphic calls (polymorphic method with only one implementation). This is done if the VM sees that this method always resolves to the same target. What used to be complex invocation is reduced to a few CPU instructions guard, which are predicted and pipelined by modern CPUs. When the guard condition stops being true, the VM can take a different code path or even drop back to interpreting mode. Based on statistics and program workload, the generated machine code can be different at different time. Many of these optimizations rely on the information gathered during the program execution and are not possible if you compile once whan you load the class.

这就是为什么你需要热身JVM和效仿逼真的工作量时,基准算法(偏斜数据可能导致优化不现实assesment)。其他的优化​​是锁省略,自适应自旋锁,逃生分析和堆栈分配等。

This is why you need to warm-up the JVM and emulate realistic workload when you benchmark algorithms (skewed data can lead to unrealistic assesment of the optimizations). Other optimizations are lock elision, adaptive spin-locking, escape analysis and stack allocation, etc.

也就是说,热点只是一个虚拟机。 JRockit的,Azul公司,IBM的J9和复位RVM, - 。每个人都有不同的性能特征

That said, HotSpot is only one of the VMs. JRockit, Azul, IBM's J9 and the Resettable RVM, - all have different performance profiles.