简体   繁体   English

JIT 编译器中的代码生成是否会生成更好的本机代码?

[英]Does the code generation in JIT compiler generate better native code?

I'm researching about JVM and JIT compiler, and want to figure out what difference between traditional compiler like gcc and JIT compiler.我正在研究 JVM 和 JIT 编译器,想弄清楚像 gcc 这样的传统编译器和 JIT 编译器有什么区别。

I know JIT compiler will investigate the hotspot of a code, and compile the instructions to machine code to improve the performance.我知道 JIT 编译器会调查代码的热点,并将指令编译为机器码以提高性能。

But in the situation below:但在以下情况下:

Q = a / b;
R = a % b;

In traditional compiler, this code will be translated to a single division if the hardware has the operator.在传统的编译器中,如果硬件有操作符,这段代码将被翻译成单除法。

But does the JIT compiler do the same thing as traditional compiler?但是 JIT 编译器是否与传统编译器做同样的事情?

the interpreter like JVM executes the bytecode one by one line.像JVM这样的解释器会一行一行地执行字节码。 If the division and modulo above are the hotspot, does JIT compiler compile it to one single machine code to get the value?如果上面的除法和取模是热点,JIT 编译器是否将其编译为单个机器码以获取值? or just compile them to two divisions?或者只是将它们编译为两个部门?

In my point, maybe the javac will do some optimizations first and then do the JIT jobs.在我看来,也许javac会先做一些优化,然后再做 JIT 工作。 But I do the experiment below:但我做了下面的实验:

javac test.java
javap -c test.class

and it prints out the bytecode.它打印出字节码。 I can see there are irem and idiv in the bytecode.我可以看到字节码中有iremidiv Then I know there is not such a pre-optimization or any else.然后我知道没有这样的预优化或其他任何东西。

There is no fundamental difference between an offline compiler and a JIT compiler as both transforms code from a more abstract representation (eg source code or byte code) into a more concrete representation (most commonly native code).离线编译器和 JIT 编译器之间没有根本区别,因为两者都将代码从更抽象的表示(例如源代码或字节码)转换为更具体的表示(最常见的是本机代码)。

The main difference is in the trade offs when deciding on what optimizations to perform and the information present when performing the optimizations.主要区别在于在决定要执行哪些优化以及执行优化时提供的信息时的权衡。 In an offline compiler you can, typically, spend more time on optimizing the code as this is done once and for all and a long compile time will not impact on the performance of your program.在离线编译器中,您通常可以花更多时间优化代码,因为这是一劳永逸的,而且长时间的编译时间不会影响程序的性能。 On the other hand an offline compiler often know very little about how a program is used and must guess which paths are hot and which loops are tight.另一方面,离线编译器通常对程序的使用方式知之甚少,并且必须猜测哪些路径是热的,哪些循环是紧密的。

A JIT compiler on the other hand is run on demand by the runtime system of the language while the program is running, this implies that the compile time is amortized over runtime of the program and a slow compiler will impact its performance.另一方面,JIT 编译器在程序运行时由语言的运行时系统按需运行,这意味着编译时间在程序的运行时分摊,而慢速编译器将影响其性能。 On the other hand it a JIT compiler can know more about the runtime behavior of the program and direct its effort to hot paths and tight loops.另一方面,JIT 编译器可以更多地了解程序的运行时行为,并将其工作指向热路径和紧密循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM