TSS Java Symposium Best Talk: Brian Goetz tackles Java Performance Myths

Brian Goetz’s talk on Java Performance Myth had to be the funniest of them all. Laughing at C programmers who insist on writing object pools and backwards for loops is both fun and easy. Let’s all write “clean, dumb code” was Goetz’s plea to everybody and insisted the JVM doesn’t suck and is smarter than those sorry, mangled-up minds that think public getters/setters is an overhead. Basically, the JVM puts some serious thought in the optimization of compiled code including dead code elimination, optimized garbage collection and resource allocation.

His assault on all those pricks who write micro-benchmarks and happily conclude that X is faster than Y was also refreshing. A counter-example goes a long way in debunking a myth and that’s what he resorted to when showing that in some cases it might be faster to compute 1+1 by doing 1+2-1. Pure nonsense, it turns out the JVM remembers execution paths and the order of operation does matter. Conclusion: most micro-benchmarks are useless since they ignore JVM optimizations and runtime correction.

My favorite part of the talk was a production code example he gave when iterating an array, I reproduce it here for it’s humorous value:

int i = 0;
try {
  while (true)
} catch (ArrayIndexOutOfBoundsException e) {
  /* exit loop */

Pure genius eh?

I’m starting to like this guy, he’s saying Java should be faster than C and since this is already true for resource allocation and memory management, it should be completely true in ten years when those JVM dudes figure out some more ways of stealing CPU cycles. Here’s a great example of some JVM optimization: Virtual methods have been a performance hit since the object hierarchy needs to be searched at runtime and since every method in Java is overridable and thus virtual, the JVM must do some searching. Or does it? Well, it doesn’t. If a method is not overridden, the JVM will treat it as mono-morphic and optimize it accordingly making it inline. Same goes true for declaring final methods – it doesn’t help, just clutters up the code base.

I’m glad that Vector has finally received a reprieve and can finally be used in Java code (local variables only please) without suffering embarrassment or humiliation. Synchronization for local variables is much different than members and Goetz authorized the use of Vectors with reckless abandon. I’m going to go and change all my ArrayLists to Vectors right now. No seriously, its good to know there’s no penalty where there shouldn’t be. JVMs are smart enough to make unshared synchronized collections as fast as unsychronized ones. But this leads me to a deeper question, why would you have an unshared synchronized collection, wouldn’t that be a paradox? Besides the point.

Poor Java does suffer from this label that it’s slow as molasses, but times have changed and so has the JVM, what might have been true 6 years ago is probably not anymore. Goetz’s claim that performance advice has a finite shelf life is 100% bang-on. The JVM now keeps multiple copies of compiled code and can swap it out based on runtime patterns, that’s a feature that .NET loves to pride itself on but it’s hardly even mentioned in the Java world.

Clever code is NOT faster code so stop trying to be a smart-ass and just write simple Java. Even if you have to make your code ugly to increase performance by a nanosecond, there’s a great chance that the next version of the JVM will make your efforts redundant. So unless you have some serious performance requirements, there’s no reason to write code that looks like shit.

Random Notes: International Terminal sucks, Blackjack is not easy, They make you walk through the casino to get to your room, Air Canada staff are idiots, Airport security is lax


One thought on “TSS Java Symposium Best Talk: Brian Goetz tackles Java Performance Myths

  1. darren

    you deserve a prize just for the amount of stuff you posted lately, pure words words words, although i do understand that the international terminal sucks, same here in malaysia


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s