6

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?

user2246674
  • 7,621
  • 25
  • 28
Radu Gancea
  • 773
  • 10
  • 19
  • Which Java JIT compiler? – SLaks Jun 28 '13 at 17:21
  • 1
    I'm interested to know for HotSpot – selig Jun 28 '13 at 17:24
  • If your program has randomness and one run calls method A and another run calls method B, then no, you won't get the same optimizations. – Louis Wasserman Jun 28 '13 at 17:34
  • But what if the exact same code paths are taken? – selig Jun 28 '13 at 17:41
  • I am not sure if this is helpful to you, but you can force it to optimize your function in different ways. See: http://stackoverflow.com/questions/19852847/high-cost-of-polymorphism-in-java-hotspot-server/19886520#19886520 –  Nov 12 '13 at 09:40

1 Answers1

5

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.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280