JIT and AOT in Java

We all know that Java is a semi-compiled and semi-interpreted language. Its compiled part is similar to the C++ language, and the interpreted part is similar to the Python language. Java is a language that combines the two methods.

1. Compilation and interpretation

1.1 Compiled Languages

The so-called compilation is to convert the source code of the program into a binary file that can be directly run. The binary file generated after compilation of each program is different. Even if the source code of the program has only been slightly changed, the changed source code must be modified. All code files must be recompiled. If there is a lot of source code and the compilation speed is very slow, debugging and running will become a trouble, resulting in low program development efficiency. This is a shortcoming of compiled languages. But in contrast, the compiled binary file can be run directly on the corresponding operating system, and the program's performance will be very high. A typical compiled language is C++, and C++ programs are very efficient. In order to further speed up the startup speed of the program, AOT technology was developed.

1.2 Interpreted Languages

The so-called interpretation means that the virtual machine (a kind of software) analyzes the content of a piece of text or a file and responds accordingly. Since an interpreted language only interprets text or files, there is no compilation process. Therefore, if the source code file is modified a little, the difference is not very big for the interpreter, and the debugging and running speed will not be greatly affected. differences, thereby increasing the speed of program development. This is the advantage of interpreted languages. But on the contrary, the running speed and efficiency of the program will be reduced, because the text or file to be interpreted by the interpreter actually needs to be converted into intermediate code (sometimes called bytecode) that the interpreter can efficiently interpret, and then let the interpreter The running efficiency of such a program is very low. A typical interpreted language is Python, and the software Python is a virtual machine. In order to further speed up the running speed of the program, JIT technology was developed.

1.3 Virtual machine

A virtual machine refers to a software used to interpret program source code. Why is it called a virtual machine? Because the opposite is a non-virtual machine, which is a real machine, such as our computer, which is a real machine, and the operating system is a "virtual machine" running on this real machine, and Java, Python, etc. are " Virtual machine in "Virtual Machine". No matter what program it is, it will eventually be turned into machine code for the machine to execute. In the process of virtual machine within a virtual machine, the deeper the nesting, the "further away" the program will be from the real physical machine. The execution efficiency is slower because there are layers of conversion processes during the execution process. We all know that in terms of program running efficiency, C++ > Java > Python. This is because C++ is "close" to the real physical machine, while Python is "far" away.

1.4 Why can Java be "compile once, run anywhere"?

From the above we can know that Java is a semi-compiled and semi-interpreted language. When Java compiles the source code, the compiler javac compiles the java file into a bytecode file with a class suffix, and then interprets it. The JVM interprets class bytecode files to achieve cross-platform purposes.

2. JIT and AOT

2.1 Principles of JIT

Just In Time (Just In Time), referred to as JIT, means that the program compiles hot code while it is running to achieve the purpose of speeding up the running speed.

If an interpreted language wants to run fast, it can only develop in the direction of compilation, but it cannot be completely compiled, so there is JIT technology. During the interpretation and running process, if code that is executed very frequently (we call it Hot Spot Code) is found, this part of the code will be compiled and cached, and the compiled file will be called when used, so The purpose of acceleration is achieved. As we mentioned before, Java is a semi-compiled and semi-interpreted language. When Java compiles the source code, the compiler javac compiles the java file into a bytecode file with a class suffix, and then lets The interpreter JVM interprets class bytecode files to achieve cross-platform purposes. This process involves both compilation and interpretation. In the process of explanation, JIT comes into play.

JIT speeds up the process

JIT is not only used in the Java language, but also in languages ​​such as Python. In general, JIT has high throughput, has runtime performance bonus, the program can run faster, and can dynamically generate code, etc., but it is relatively slow to start and requires a certain amount of time and call frequency to trigger. JIT layering mechanism.

Of course, the application of JIT technology in Java is not only the compilation of hot code, but also optimization in many other aspects.

2.2 Hotspot code

Regarding how JIT determines which codes are hot codes, a brief introduction is given below. Simply put, when a piece of code is executed many times, it will be identified as hot code by the JIT. Specifically, there are two ways to determine:

Sample Based Hot Spot Detection

Counter Based Hot Spot Detection

2.3 Principle of AOT

Ahead of Time (Ahead Of Time), referred to as AOT, means that the program compiles the code before running it to speed up the startup.

Pre-compilation, also called static compilation, means analyzing the structure of the code in advance, performing corresponding optimizations, and then directly compiling it into a binary file. It does not need to be interpreted by an interpreter and is run directly by the platform. Programs running AOT technology have low memory usage, fast startup speed, and can be run directly, but there is no performance bonus during runtime, and further optimization cannot be performed based on the running conditions of the program. Typically, o1, o2 and o3 in gcc are optimized. They are compilation optimizations that improve the quality of the compiled program.

Guess you like

Origin blog.csdn.net/weixin_62651706/article/details/132274149