Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the rationale for JIT to not compile huge methods?

Tags:

java

jit

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?

like image 251
Rafael Winterhalter Avatar asked Jul 24 '14 07:07

Rafael Winterhalter


People also ask

What does JIT do during compilation process?

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.

What reasons are there to not JIT?

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.

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.


1 Answers

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.

like image 176
apangin Avatar answered Oct 22 '22 09:10

apangin