Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use javac -O option to optimize?

javac has an interesting -O option:

Optimizes compiled code by inlining static, final and private methods. Note that your classes may get larger in size.

This option seems to not be popular (hidden?), I've just discovered it today, on CodeCup 2014 page.

-O is not mentioned in the official documentation nor in man javac... Strange.

In accepted answer to similar question, we can read that:

Optimization in Java is mostly done by the JIT compiler at runtime. Hence there is no point trying to instruct it to optimize a certain way at compile time (when it is creating only bytecode anyway). The JIT will almost surely make better decisions on the spot, knowing the exact environment and observing the actual patterns of execution of specific parts of your code.

My question is:

Should I always use the -O option or not? In other words, the code always run faster with -O or does it make no difference at all?

Maybe classes size can increase so much that the overall performance will drop? Or JVM will do the inlining anyway so it's better to leave it to that?

Similar story was with gcc -O3 flag.

like image 272
Adam Stelmaszczyk Avatar asked Jan 05 '14 00:01

Adam Stelmaszczyk


People also ask

Is javac needed?

In conclusion: you need a java compiler installed to compile java - even when maven starts the compiler for you.

What is javac option?

Description. The javac command reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. The javac command can also process annotations in Java source files and classes. There are two ways to pass source code file names to javac .

Does the Java compiler optimize?

The compiler don't optimize the bytecode because it is optimized at run time by the JIT optimizer. If the type of runtime you are targeting don't have a JIT optimizer (even if it had a JIT compiler), or you are AOT compiling, I recommend using an optimizing obfuscator like Proguard or Allatori.

Which compiler improves the performance of Java?

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.


1 Answers

It is a no-op according to a comment in the source code around line 553.

It was probably useful when the JIT compiler was not efficient yet or when there was no JIT compiler at all.

like image 136
assylias Avatar answered Sep 23 '22 12:09

assylias