Feeds

Time to overcome Java misconceptions

Program, profile, repeat

Boost IT visibility and business value

Myths and legends It doesn't really matter which version of the Java platform you use, does it? Well I know for sure that thread context switching is expensive. Isn't it? OK, but there's no doubt that 32-bit Java Virtual Machines (JVMs) are faster than 64-bit JVMs. Right?

The truth is, most of the things we think we know about Java performance are actually pretty hard to quantify. That's the conclusion Google engineers Jeremy Manson and Paul Tyma have come to through what they humbly describe as "an unending search for the truth".

"We had been writing a lot of code - server code, fast code, Java code, C++ code - and we realized one day that there are a lot of myths out there about Java performance, beliefs that we just knew weren't true, but had never been tested," Manson said at this Spring's Software Development West.

"We came to see that these unexamined beliefs actually make everybody's life more difficult," Tyma added. "They make you write lousy, broken code, because you're working around something that might not even be true. And then your code becomes unreadable."

Not precisely myth-busters, Manson and Tyma took on the roles of assumption challengers and shared their insights with SD West attendees, during a joint preso that touched on some timeless themes.

"This isn't about optimizing your code," Manson said. "It's about challenging those assumptions you make while you're writing it."

And what are those assumptions?

Number one: versions don't matter.

"A lot of people think that they can keep on using 1.4 forever, and it won't affect performance," Tyma said. "Well, it turns out they've been improving Java steadily over the past 13 years, and 1.6 is a lot faster than 1.5, and 1.5 is a lot faster than 1.4. And remember, you can run 1.4 byte code on 1.6 VMs."

Number two: 64-bit code is slower than 32-bit code.

"I've heard this from a lot of people," Manson said. "And the reason those people seem absolutely convinced of this is that the pointers are twice as large, and all of a sudden you're taking up between one and two megs of memory... and now that you're using more memory, your code isn't going to fit into the L1 and L2 cache, which will slow down your code."

But this scenario doesn't always manifest in the x86 world, he said, because 64-bit code has twice as many registers, so there's less register spillage, and you can have longer pipelines in your processor. So code that is very intensive in terms of registers and integer performance can be a lot faster in 64-bit.

Number three: thread context switching is expensive.

In the Linux 2.6 NPTL library, which is where Tyma and Manson ran their tests, context switching was not expensive at all. Even with a thousand threads competing for Core Duo CPU cycles, the context-switching wasn't even noticeable.

Number four: locking is expensive.

If they're uncontended, no. Uncontended synchronization is cheap, and can even be free. Add a thread, and that picture changes. Synchronized locking with contention gets more expensive; the cost goes up significantly. But increase the number of threads, and the cost doesn't change much after that, Tyma noted. "So yes, contended synchronization does cost more, but [that cost] doesn't tend to scale," he said.

"The moral here," said Manson, "is don't avoid synchronization. When doing tricky things to avoid synchronization, you end up writing really bad code."

So what's Tyma and Manson's bottom-line advice to developers seeking to write faster Java code? "If you want your code to be faster, don't waste your time trying to take advantage of any of these kinds of performance myths," Manson said. "Just write the best code you can. Profile it to find the bottlenecks. Remove the bottlenecks. Rinse and repeat."

You can read Manson's summarized takeaways here. ®

The essential guide to IT transformation

More from The Register

next story
Munich considers dumping Linux for ... GULP ... Windows!
Give a penguinista a hug, the Outlook's not good for open source's poster child
The Return of BSOD: Does ANYONE trust Microsoft patches?
Sysadmins, you're either fighting fires or seen as incompetents now
Intel's Raspberry Pi rival Galileo can now run Windows
Behold the Internet of Things. Wintel Things
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Eat up Martha! Microsoft slings handwriting recog into OneNote on Android
Freehand input on non-Windows kit for the first time
Time to move away from Windows 7 ... whoa, whoa, who said anything about Windows 8?
Start migrating now to avoid another XPocalypse – Gartner
You'll find Yoda at the back of every IT conference
The piss always taking is he. Bastard the.
prev story

Whitepapers

5 things you didn’t know about cloud backup
IT departments are embracing cloud backup, but there’s a lot you need to know before choosing a service provider. Learn all the critical things you need to know.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.
Rethinking backup and recovery in the modern data center
Combining intelligence, operational analytics, and automation to enable efficient, data-driven IT organizations using the HP ABR approach.
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.