Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force the JVM to natively compile a given method?

I have a performance-critical method called often when my app starts up. Eventually, it gets JIT-compiled, but not after some noticeable time being run in the interpreter.

Is there any way I can tell the JVM that I want this method compiled right from the start (without tweaking other internals with stuff like -XX:CompileThreshold)?

like image 345
Jean-Philippe Pellet Avatar asked Feb 22 '13 08:02

Jean-Philippe Pellet


People also ask

Can you compile Java to native code?

Yes! Native Image The native image feature with the GraalVM SDK helps improve the startup time of Java applications and gives them a smaller footprint.

Does JVM compile source code?

JVM does not compile the code, it interprets. The instructions in this byte-code cannot be directly run by the CPU. Therefore some other 'program' is needed which can interpret the code, and give the CPU machine level instructions which it can execute. This program is the 'JVM' (Java Virtual Machine).

What is Java native compilation?

When Java is loaded into the server it is compiled to byte code before execution. The process of native compilation converts Java stored procedures to native code shared libraries which are linked into the kernel resulting in performance increases for the procedural code.

Which compiler is used by JVM to improve the performance?

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.


2 Answers

The only way I know of is the -Xcomp flag, but that is not generally advisable to use. It forces immediate JIT compilation of ALL classes and methods first time they are run. The downside is that you will see a performance decrease on initial startup (due to increased JIT activity). The other major limitation with this flag is that it appears to disable the incremental profiling-based optimization that JIT would normally do. In standard mixed mode, the JIT compiler can (and will) deoptimize and re-compile parts of the code continually based on profiling and run-time information collected. This allows it to "correct" faulty optimizations like boundary checks that were omitted but turned out to be needed, sub-optimal inlinings etc. -Xcomp disables the profiling-based optimization and depending on program, can cause significant performance losses overall for only a small or no real gain in startup, which is why it's not recommended to use.

Beyond to -Xcomp (which is pretty brutal) and -XX:CompileThreshold (which controls how many executions of a given method the JIT will run in intepreted mode to gather stats before compiling/optimizing it), there is also -Xbatch. This forces JIT compilation to the "foreground", essentially blocking calls to methods until it's been compiled, rather than compiling it in the background as it normally does.

You didn't specify which Java version you are using, but if Java 7 is an option for you, it introduces a new JIT model called "Tiered compilation" (activated with the -XX:+TieredCompilation switch). What tiered compilation does is that it allows an initial, smaller compilation pass on the first use of a method and than an additional, larger compilation/optimization later, based on collected profiling data. Sounds like it should be interesting to you.

It supposedly requires some additional tweaking and parameters/configurations, but I've not got around to checking it out further.

like image 160
pap Avatar answered Oct 23 '22 05:10

pap


im not sure if it'll completely precompile the code, but you could add your class with the critical method to the JVM's shared data dump. see this question for more details.

also, have you considered JNI? if your method is very CPU intensive it might speed things up considerably.

like image 25
radai Avatar answered Oct 23 '22 07:10

radai