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

2

34

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)?

Cricoid answered 22/2, 2013 at 8:51 Comment(7)
can you run a realistic, synthetic warmup of that method or does it do stuff that is not possible to fake?Boxcar
This method is used to parse large configuration files right after startup, and this is what I want to speed up. The artificial warmup would probably do no good at this point, I guess...Cricoid
could you maybe simplify your configuration? change it from xml to a simpler properties format or anything to reduce the amount of code needed to read it? maybe split the configuration into a "boot" part, small as possible just to start up and a main part that you could load in the background? maybe parallelize the work so even though the code is interpreted you might gain speed from threading it?Aschim
@Jean-PhilippePellet JIT won't improve your File I/O. Do you have a sense of how much of the execution-time for your method is reading the data from the file and how much is actual processing of the data?Cavell
It's parsing JSON. The first time the file is parsed, it takes about 2 seconds. The subsequent times, less than a second.Cricoid
have you tried making the conf into a *.peroperties file just to see if things improve? a lot less code involved in reading properties filesAschim
@Jean-PhilippePellet Are you reading the same file multiple times?Fitter
C
44

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.

Cavell answered 22/2, 2013 at 10:7 Comment(3)
Thanks for the nice listing of all these options. Too bad they can't be set on a finer level…Cricoid
i think TieredCompilation wont do too much damage as it wont really change anything in the long runAschim
I'd strongly advise against using -Xcomp. YMMV, but in my real-world example, it performed more 10-30 times as bad as "standard mixed mode".Jeanniejeannine
A
2

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.

Aschim answered 22/2, 2013 at 9:31 Comment(1)
Interesting. It seems that this only improves class loading and doesn't touch the JIT compiler, though. Or am I missing something? I'd like to avoid JNI and having to compile native code on all platforms where we deploy...Cricoid

© 2022 - 2024 — McMap. All rights reserved.