Does Java JIT compile the bytecode with the same optimizations at every run on the same machine?
Does it take into consideration dynamic factors like CPU usage at a given moment, or it will make the same optimizations every time regardless of temporary factors?
To help the JIT compiler analyze the method, its bytecodes are first reformulated in an internal representation called trees, which resembles machine code more closely than bytecodes. Analysis and optimizations are then performed on the trees of the method. At the end, the trees are translated into native code.
The JIT compiler is enabled by default, and is activated when a Java method is called. The JIT compiler compiles the bytecodes of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it.
A JIT compiler can be faster because the machine code is being generated on the exact machine that it will also execute on. This means that the JIT has the best possible information available to it to emit optimized code.
A JIT Compiler translates byte code into machine code and then execute the machine code. Interpreters read your high level language (interprets it) and execute what's asked by your program. Interpreters are normally not passing through byte-code and jit compilation.
No, the optimizations are non-deterministic. Even if you run the exact same single-threaded, fully deterministic program, the sampler used by the JIT to determine which methods to optimize could choose a different set.
Another thing that can change the generated machine code is the actual memory locations of certain constants that are referenced by the code. The JIT can emit machine instructions that directly access these memory locations, resulting in additional differences between the machine code on different passes.
Researchers using the Jikes RVM have addressed this problem for their benchmarks by using a feature called Compiler Replay.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With