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 ?
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.
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:
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.
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