Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java JIT compile bytecode deterministically - same optimizations for every run on the same machine?

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?

like image 785
Radu Gancea Avatar asked Jun 28 '13 17:06

Radu Gancea


People also ask

How does JIT optimization work?

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.

How does Java JIT compiler work?

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.

Is JIT faster than compiled?

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.

What is JIT How is it different than compiler or interpreter?

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.


1 Answers

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.

like image 110
Sam Harwell Avatar answered Oct 14 '22 02:10

Sam Harwell