Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Sun's HotSpot JIT compiler automatically apply "final" to Java local variables?

I've heard that this is the case, but I couldn't find a definitive online source to confirm it.

Background: A colleague likes to make his local variables final. One of his reasons for doing so is performance. My contention is that Java's HotSpot Just In Time compiler will automatically detect invariant local variables, and make them final, so there is no performance benefit to doing that ourselves.

Note that I'm not asking whether it's good coding practice to make local variables final, because there are already plenty of (off-topic) SO questions about that.

EDIT: mrhobo makes a good point about optimization of the bytecode for integer literals. I should have given an example of the type of code I was talking about, with my question:

Object doSomething(Foo foo) {
    if (foo == null) {
        return null;
    }

    final Bar bar = foo.getBar();
    final Baz baz = this.bazMap.get(bar);

    return new MyObject(bar, baz);
}

Do you think the same type of optimization happens in this scenario, because bar and baz are both marked final? Or does HotSpot automatically detect that they're not changing within the scope of the method, and treat them as final anyway?

Similar Questions

  • Declaring local variable as final in a loop
    • same question, but derives the answer empirically (by looking at classfiles), with no documentation reference
  • Do javac or Hotspot automatically add 'final' as an optimisation of invariant variables?
    • same question for instance variables
  • Inlining in Java
    • same question for methods
  • Does use of final keyword in Java improve the performance?
    • similar question, with no consensus for local variables
like image 269
Steve K Avatar asked Mar 07 '14 16:03

Steve K


1 Answers

To understand why final for local variables is completely uninteresting for the compiler, some background at how compilers work will be helpful. Basically no compiler operates on the source code (or bytecode) itself, instead they parse it into some kind of intermediate representation (often several different ones while applying optimizations). And pretty much every compiler I know of uses some form of static-single assignment or short SSA form for its intermediate representation.

As the name says SSA form basically means that every variable is assigned a value only once. To make this clearer, assume we have the following simple code fragment:

y = 4
x = 5
y = 6
z = x + y

In SSA form this looks something like the following then:

y_1 = 4
x_1 = 5
y_2 = 6
z_1 = y_2 + x_1

So why do we do this? Because it makes many compiler optimizations a whole lot easier (it is for example trivial to see that the first write to y can be eliminated since we never read it). So if you want you may see this as every variable already being final for the compiler.

PS: loops and branches do make this a bit more complicated, but I don't want to sidetrack the discussion too much there - the wiki article has a short explanation on how to solve those problems though.

like image 116
Voo Avatar answered Oct 19 '22 06:10

Voo