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.
In conclusion: you need a java compiler installed to compile java - even when maven starts the compiler for you.
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 .
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.
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.
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.
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