28

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.

Community
  • 1
  • 1
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • 1
    I didn't know about this flag. Perhaps the work I do with Java doesn't require this sort of optimization. I'd recommend evaluating it in isolation and see how it performs, since the answer may be "it depends on what you're doing". – Makoto Jan 05 '14 at 00:04
  • Didn't the accepted answer also answer your question? There seems to be no reason to use the flag, if the JIT compiler is doing all the work in the end (and better). – Birb Jan 05 '14 at 00:14
  • 4
    Inlining is never, for any language, going to be an optimization in every case. For some situations it may make sense. For most others, probably not. As with nearly every optimization ever, the solution is to benchmark it and see if it works for your situation. – Chris Hayes Jan 05 '14 at 00:18
  • @Izmaki No, it didn't entirely, I thought that maybe it's good to help JIT and already do the inlining, because JIT turns on after some time. – Adam Stelmaszczyk Jan 05 '14 at 00:21
  • 1
    Can the downvoter please comment what's wrong with this question? – Adam Stelmaszczyk Jan 05 '14 at 00:22

3 Answers3

38

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.

eis
  • 51,991
  • 13
  • 150
  • 199
assylias
  • 321,522
  • 82
  • 660
  • 783
7

I have been working with Java almost since its inception. I have built many systems, some of them high-performance, some of them extreme performance, and I have never, ever, found this flag useful. I think that it may have once had a use, but I have never needed to care.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
6

I don't think It does any optimization as you think:

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

Please have a look into these questions:

Update

You won't find this in any Oracle documentation because it effectively does nothing at all (no-op). Moreover your link is a very very old documentation:

Go up two directories in your link - It's the documentation for jdk 1.1.3. It's 13 - 14 years old! We are now on Jdk 7 and eagerly waiting for Jdk 8.

Community
  • 1
  • 1
avijendr
  • 3,958
  • 2
  • 31
  • 46
  • 1
    Well, you just copy-pasted the quote from the question. I saw the links you gave (when writing a question SO shows similar ones already existing). There is even no one mention about `-O` option in these links. – Adam Stelmaszczyk Jan 05 '14 at 00:29
  • 1
    @AdamStelmaszczyk - my point was what -O does (It does no real optimization, rather than the description in the link). The other questions/answers explain why there can't be any optimizations done on a Java compiler and some close/small alternatives. And thanks for Down-voting, when I had up-voted your question! – avijendr Jan 05 '14 at 00:32
  • We all know what it does, it was written. And `-O` does optimize, it's written, *optimizes compiled code by inlining*. – Adam Stelmaszczyk Jan 05 '14 at 00:37
  • 1
    @AdamStelmaszczyk - Have you seen the comment by assylias? It's a No-op which means it that effectively does nothing at all! And that's the reason why you don't find it in any Oracle documentation! The documentation you have provided is for 1.1.3 mate. See my update in my answer. – avijendr Jan 05 '14 at 01:13
  • 2
    Yes, I've accepted his answer. Hahah 14 years old, my god, what I've found :D If one writes `javac "-O" option` in Google that old page pops out as a 5th result! But recent `javac -O` still "works" (now we know it does nothing). I'm undoing -1, cheers. – Adam Stelmaszczyk Jan 05 '14 at 01:43
  • But why on earth are you using Google to find current Java documentation? It ships with the JDK. It should be installed on your own computer, and you should have it permanently open in a browser tab. – user207421 Jan 05 '14 at 03:02