与非JIT编译器相比,JIT编译器具体做什么?有人能给出简洁易懂的描述吗?


当前回答

即时(JIT)编译(也称为动态翻译或运行时编译)是一种执行计算机代码的方式,它涉及在程序执行期间(在运行时)而不是在执行之前进行编译。

IT编译结合了两种传统的机器代码转换方法——预先编译(AOT)和解释,并结合了两者的一些优点和缺点。JIT编译结合了编译代码的速度和解释的灵活性。

让我们考虑在JVM中使用的JIT,

例如,HotSpot JVM JIT编译器生成动态优化。换句话说,它们在Java应用程序运行时做出优化决策,并生成针对底层系统架构的高性能本机机器指令。

When a method is chosen for compilation, the JVM feeds its bytecode to the Just-In-Time compiler (JIT). The JIT needs to understand the semantics and syntax of the bytecode before it can compile the method correctly. To help the JIT compiler analyze the method, its bytecode are first reformulated in an internal representation called trace trees, which resembles machine code more closely than bytecode. Analysis and optimizations are then performed on the trees of the method. At the end, the trees are translated into native code.

跟踪树是在编程代码的运行时编译中使用的数据结构。跟踪树用于一种“即时编译器”,它跟踪在热点期间执行的代码并编译它。提到这一点。

参考:

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html https://en.wikipedia.org/wiki/Just-in-time_compilation

其他回答

一开始,编译器负责将高级语言(定义为比汇编程序更高级别)转换成目标代码(机器指令),然后(通过链接器)将其链接成可执行文件。

At one point in the evolution of languages, compilers would compile a high-level language into pseudo-code, which would then be interpreted (by an interpreter) to run your program. This eliminated the object code and executables, and allowed these languages to be portable to multiple operating systems and hardware platforms. Pascal (which compiled to P-Code) was one of the first; Java and C# are more recent examples. Eventually the term P-Code was replaced with bytecode, since most of the pseudo-operations are a byte long.

即时(JIT)编译器是运行时解释器的一个特性,它不是在每次调用方法时都解释字节码,而是将字节码编译为运行机器的机器代码指令,然后调用此目标代码。理想情况下,运行目标代码的效率将克服每次运行时重新编译程序的低效率。

Jit代表即时编译器 Jit是一个将Java字节代码转换为可以直接发送到处理器的指令的程序。

在特定的系统平台上使用java即时编译器(实际上是第二个编译器)将字节码遵从为特定的系统代码,一旦代码被jit编译器重新编译,它通常会在计算机中运行得更快。

即时编译器随虚拟机一起提供,可选使用。它将字节码编译为立即执行的特定于平台的可执行代码。

JIT编译器只在第一次执行时将字节码编译为等效的本机代码。在每次连续执行时,JVM仅使用已编译的本机代码来优化性能。

如果没有JIT编译器,JVM解释器将逐行转换字节码,使其看起来就像正在执行本机应用程序一样。

正如其他人提到的

JIT代表Just-in-Time,这意味着代码在需要时才编译,而不是在运行时之前。

为了给上面的讨论补充一点,JVM维护一个函数执行时间的计数。如果该计数超过预定义的限制,JIT将代码编译为处理器可以直接执行的机器语言(不像一般情况下,javac将代码编译为字节码,然后java -解释器逐行解释这个字节码,将其转换为机器代码并执行)。

另外,下次计算此函数时,将再次执行相同的编译代码,而不像常规解释那样逐行重新解释代码。这使得执行速度更快。

JVM actually performs compilation steps during runtime for performance reasons. This means that Java doesn't have a clean compile-execution separation. It first does a so called static compilation from Java source code to bytecode. Then this bytecode is passed to the JVM for execution. But executing bytecode is slow so the JVM measures how often the bytecode is run and when it detects a "hotspot" of code that's run very frequently it performs dynamic compilation from bytecode to machinecode of the "hotspot" code (hotspot profiler). So effectively today Java programs are run by machinecode execution.