Reputation: 1587
I have been looking into the Java JIT compiler and i cannot figure out why some of the code is still interpreted. Why doesn't the JIT compiler translate everything to native code? Interpretation is much slower, am I missing something?
Upvotes: 7
Views: 1097
Reputation: 133
JIT compiler optimizes code on the fly using program execution statistics. For example, "hotpath" optimization counts the number of hits in one part of the program and generates machine code only for those parts of the code where the program is executed frequently. This allows the JIT optimizer to place pieces of frequently executed machine code close together so that they fit into the processor's cache. This allows the JIT compiler to outperform precompiled machine code.
Upvotes: 0
Reputation: 106351
Two main reasons:
Hence the Java JIT takes a sensible strategy: don't compile until you observe that the same code is being run multiple times, at which point you have evidence that doing the compilation is probably worthwhile and you are able to make some additional optimisations.
Upvotes: 2
Reputation: 3314
It's all a matter of tradeoffs
Upvotes: 5
Reputation: 185852
If you are running a JVM like HotSpot, it JIT-compiles opportunistically, only focusing on code that executes frequently. It determines which code to optimise on the fly by counting frequency of each code block (or method — I'm not sure which). Consequently, at startup time, everything is interpreted.
The intent behind this is allow for much more aggressive and expensive optimisations by only requiring a small fraction of the code to be optimised.
Upvotes: 2