I'm working on a high-performance Android application (a game), and though I try to code for readability first, I like to keep in the back of my mind a picture of what is happening under the hood. With C++, I've developed a fairly good intuition about what the compiler will and won't do for me. I'm trying to do the same for Java/Android.
Hence this question. I could find very little about this topic on the web. Will the Java compiler, Dalvik converter (dx) and/or JITter (on Android 2.2+) perform optimizations like the following?
Method inlining. Under what conditions?
private
methods can always safely be inlined; will this be done? How aboutpublic final
methods? Methods on objects of other classes?static
methods? What if the runtime type of the object can easily be deduced by the compiler? Should I declare methods asfinal
orstatic
wherever possible?Common subexpression elimination. For example, if I access
someObject.someField
twice, will the lookup be done only once? What if it's a call to a getter? What if I use some arithmetic expression twice; will it be evaluated only once? What if I use the result of some expression, whose value I know not to change, as the upper bound of afor
loop?Bounds checking on array lookups. Will the toolchain eliminate this in certain conditions, like the archetypical
for
loop?Value inlining. Will accesses to some
public static final int
always be inlined? Even if they're in another class? Even if they're in another package?Branch prediction. How big an issue is this even? Is branching a large performance hit on a typical Android device?
Simple arithmetic. Will
someInt * 2
be replaced bysomeInt << 1
?
Etcetera...