Sometimes javac Does Optimize

Sometimes, javac apparently decides not to be utterly stupid. Sometimes it optimizes. Here’s the special case: When a program locks this, which is always present in local variable 0 in non-static methods, it does not allocate an additional local variable. I expected the following:


GeSHi Error: GeSHi could not find the language jvm (using path /homepages/42/d86928234/htdocs/blog/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

But in this case, javac is smart enough to realize that aload_0 already contains this, so it generates


GeSHi Error: GeSHi could not find the language jvm (using path /homepages/42/d86928234/htdocs/blog/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

But, of course, if the user declares a local variable, like in the following example, javac does not recognize this. This Java code

1
2
3
4
final Object temp = someField;
synchronized(temp) {
    ...
}

mercilessly generates this bytecode:


GeSHi Error: GeSHi could not find the language jvm (using path /homepages/42/d86928234/htdocs/blog/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

Now I have to put more conditionals into my code. Yuck. I hope there aren’t more of these rare optimizations hidden somewhere. All of this gives me a bad feeling about relying on my perception of what javac generates. It would be much more prudent to just rely on the VM specifications, as I did before.

Share

About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in Concurrent Unit Testing. Bookmark the permalink.

Leave a Reply