I was wondering why the JVM's JIT compiler ignores "huge methods" from compilation. (Unless the DontCompileHugeMethods
flag is set to false.) At the same time, most talks about Java's JIT compiler state that inlining is an uber-optimization as it allows to grow the mass of instructions that are subject to compilation. And this larger compilation context allows for a better optimization of the executed code. With this, I would assume that a huge method is not much different to a heavily inlined method and should be a great target for JIT compilation. What am I missing here?
The JIT compiler helps improve the performance of Java programs by compiling bytecodes into native machine code at run time. The JIT compiler is enabled by default. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it.
precompiled binaries can use high levels of optimization that takes days in order achieve the best performance, you wouldn't want that in a JIT compiler. the initial JIT compile can take longer than direct interpretation with unnoticeable differences on subsequent runs for the common cases.
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.
Basically the ROI of compiling huge methods is low.
Hot pieces of code are usually short.
Even if a huge method is executed frequently, the hot part is unlikely to cover the whole method.
E.g. consider a large switch
statement where only a few case
labels are executed often.
However the compilation unit is a method - so the bigger part of JITted code would be a waste.
Compilation of huge methods takes much time and space. Moreover, the compilation time does not grow linearly. It would be more profitable to compile several small methods instead.
Too long sheet of machine code pollutes instruction cache.
Certain optimizations are better applied to smaller pieces of code, e.g. register allocation or loop unrolling.
If a single method is larger than 8K bytecodes, it seems to be poorly written anyway.
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