Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java optimizations : bytecode-only vs JIT

Developping games for android devices, I need to target devices that have no JIT at all, and only rely on bytecode optimizations. I wonder if the set of these optimization is empty or not...

Actually, does the java compiler (the hard one, javac, not a JIT) makes any optimization like transforming a / 4 into a >> 2 ? Or is every optimization a work for the JIT ?

like image 796
Aurelien Ribon Avatar asked Jan 27 '11 14:01

Aurelien Ribon


2 Answers

The standard Java compiler does some optimizations, but it leaves most of them to the JIT.

The JIT knows on which processor exactly the program is running and also has access to runtime information, and therefore it can do more optimizations than the Java compiler could do in advance. Also, doing extensive optimizations in advance could "obfuscate" the byte code somewhat, making it harder for the JIT to optimize it.

I don't know what Google's compiler does when it translates your Java byte code to Dalvik code - it might be doing more extensive optimizations.

Maybe this tool will be useful for you: Dalvik Optimization and Verification With dexopt

By the way, the example you mention is not always valid; transforming a / 4 into a >> 2 is not guaranteed to make your program run faster on any processor. I read an article somewhere once (sorry, can't find it right now...) that explained that on (I think) modern x86 processors, a >> 2 might even be slower than a / 4.

In any case, don't do premature optimizations like transforming a / 4 to a >> 2 by hand in your source code unless you have real evidence (from performance measurements) that it's worthwhile to do it.

like image 148
Jesper Avatar answered Sep 18 '22 03:09

Jesper


If your execution platform is really executing bytecodes, your intuitions about things like a / 4 being faster than a >> 2 are likely to be wrong. You need to do some serious application profiling to figure out:

  • whether it is worthwhile optimizing at all,
  • where to focus your efforts, and
  • what (micro-)optimizations actually work.

FWIW, the javac compiler is unlikely to attempt to micro-optimize arithmetic. The optimal native code depends on the hardware of the actual execution platform, and if javac attempted to optimize the bytecodes it is likely to make the task of the JIT compiler more difficult.

like image 35
Stephen C Avatar answered Sep 19 '22 03:09

Stephen C