Interpreter, JIT, AOT and application scenarios

Interpreter (interpreter) is a program execution method thatinterprets source code line by line into machine language and executes it directly . The Interpreter execution method has some advantages, such as easier implementation, easier debugging, avoiding compilation errors, etc., but the disadvantage is that the execution speed is relatively slow , because each execution needs to interpret the code, and the interpreter usually cannot perform code optimization .


JIT (just-in-time compiler) is acompiler that dynamically compiles bytecode into local machine code when the program is running. It can optimize according to the situation when the program is running, such as optimizing hot code to avoid unnecessary code Compile to improve the performance and efficiency of the program. JIT can achieve the advantages of fast startup and fast response, but it takes a certain amount of time to compile when it runs for the first time. Therefore, if the program is only run once or rarely, the advantage of JIT is not obvious. (save space, time consuming)


AOT (precompiler) is acompiler that compiles the bytecodeof the entire program into local machine code at one time when the program is installed . It can optimize the static structure of the program, such as constant folding, dead code elimination, etc. Static optimization, but the dynamic running of the program cannot be optimized. The advantage of AOT is that it runs fast, but it requires additional time and space to complete compilation and storage, so if the program is only used occasionally or the input and environment variables at runtime often change, the advantages of AOT are not obvious. (fast speed, large space occupation)


The difference between Interpreter, JIT and AOT is mainly reflected in the compilation timing and optimization method. Interpreter interprets the code line by line and executes it when executing the code. JIT dynamically compiles bytecode into local machine code according to the actual situation when the program is running. AOT compiles the bytecode of the entire program once when the program is installed. compiles to native machine code. JIT can optimize according to the situation when the program is running, while AOT can only optimize the static structure of the program, and Interpreter does not compile and optimize.

In terms of application scenarios, Interpreter is usually used to develop prototypes, dynamic language and other scenarios, and JIT is suitable for application scenarios that require fast startup and fast response, such as Java virtual machine, JavaScript engine, etc. AOT is suitable for application scenarios that need to run quickly and consume less resources, such as Android applications, WebAssembly applications, etc.


In general, machine code does take up more space than bytecode. This is because machine code is a binary instruction that can be directly understood by the processor, while bytecode is an intermediate language that needs to be interpreted and executed by a virtual machine. Therefore, compared with bytecode, the instruction set of machine code is richer and more complex, and requires more storage space.

However, for some cases, machine code may have a smaller footprint than bytecode. This is because many instructions can be highly optimized and compressed in machine code. For example, machine code can use instruction abbreviations, bit field compression, instruction merging, etc. to reduce the number of instructions and space occupied. In addition, in some special scenarios, due to the higher performance of direct operation of machine code, a part of space and time costs can be saved.

Guess you like

Origin blog.csdn.net/weixin_47592544/article/details/129667404