java – Why you can not compile the bytecode once on the final machine

Question:

What does jvm do? Taking into account all the features of the environment in which it works (since it was written specifically for it), it interprets the bytecode, that is, it dynamically turns it into machine code.

So, why is it impossible once, when "installing" the program (that is, the first hit) on a computer using jvm, to compile the bytecode into machine code and then no longer use jvm?

After all, the acceleration will be serious and a bit of memory may be freed up due to the lack of jvm. After all, there is a JIT that does the same thing, but during the execution of the program and only in pieces.

Answer:

So, why is it impossible once, when "installing" the program (that is, the first hit) on a computer using jvm, to compile the bytecode into machine code and then no longer use jvm?

Because then it will not be possible to use speculations, due to which JIT-compiled code can quite legally overtake AOT-compiled code. For example, we might have a method with the following signature:

void push(Consumer<Integer> consumer)

In this case, AOT will have nothing left but to use the Consumer interface and calculate the actually called method at runtime. However, if the JIT at the time of compilation sees that the method has been called five thousand times with the same Consumer implementation, it can discard everything superfluous, call the previously known method directly and precede it with the so-called. trap in case another implementation falls into the method and it needs to be recompiled.

Also, JIT allows efficient inlining of code, which also takes place based on the number of calls and the size of the method. AOT cannot know which section will be hot without the help of a programmer (who, in turn, can make mistakes), but JIT does.

After all, there is a JIT that does the same thing, but during the execution of the program and only in pieces.

JIT does not do this in chunks, all code will sooner or later be compiled into machine code, just until the compiler has worked, the interpreter works. Thanks to this, we can, for example, get an assembler listing.

Scroll to Top