void myMethod(Object arg)
{
arg.getThing().method1();
arg.getThing().method2();
}
Will a basic Java 6 installation optimize this into a single call to the accessor (probably by storing the reference in a local). I realize if there are multiple threads the optimizer might have to refrain from doing it. Is there a list of common optimizations that I can expect from almost any Java 6 JVM?
example:
{
Object local = arg.getThing();
local.method1();
local.method2();
}
I wouldn’t expect any optimisation here by the javac compiler, because the value of
getThing()can change over time and between calls (for example, a random value, or thepop()operation on a stack, or the current time. You get the idea).Maybe the JIT compiler can optimize this if it sees that the
getThing()method returns always the same thing, but I wouldn’t count on that. Even the most simplereturn thing;statement can return a different value, if the value ofthingis changed by another thread.