Performance problem or design problem?

What to consider before you optimize

When I travel around the globe speaking to developers, performance is always a hot topic in relation to the Java platform. The main issues usually focus on which JVM to use, how to increase the speed of bytecode execution, how to create efficient scalable code, and the availability of Java 2 on Linux. In this column I will delineate performance issues that routinely surface during my evangelical travels.

Performance: What’s the big fuss?

Part of the problem with Java performance is the JVM and compiler technology that comprise a portion of the platform. That is, interpreted languages are inherently slower than compiled native execution. Over the last four years, the Java platform’s performance has improved with the advent of JIT (just-in-time) compiler technology, improved garbage collection, adaptive compilation, and the greater speed of underlying hardware. However, the advances in speed have in part been offset by the complexity of the Java applications that are being developed. The speed of the Java platform is actually increasing while developers are designing applications that require more complexity, speed, and scalability. Some issues that are labeled as Java performance problems are actually design problems that have nothing to do with Java.

Java platform performance will continue to improve in light of the competition from various vendors that produce JVMs, JIT compilers, and performance tools. Given the performance landscape, developers aren’t freed from using sound and tested programming techniques. Poorly designed code will always execute slowly, regardless of how many variables are hidden in hardware registers or how many loops are unrolled.

Developers have sole responsibility for designing code that produces the smallest amount of bytecode and that executes as fast as possible. With any optimization technique, the key is to understand that using the technique in a vacuum may not improve performance noticeably. But the combination of several optimization techniques can produce significant performance gains.

Design, algorithms, and data structures

Before diving into specific optimization techniques, here are some basic heuristics to remember regarding optimizing code:

  • Don’t optimize for optimization sake; only do so if you have demonstrated that the optimization is necessary. Verify the need for optimization with proven timing techniques or a reliable execution profiler that identifies specific performance issues.
  • Optimizations should be applied carefully; otherwise, bugs probably will be introduced. Remember, slow robust code is better than faster unstable code.
  • After performing an optimization, profile again to prove that it has had the desired effect. This is especially necessary when implementing optimizations in a system of software in which various integrated modules have or share optimizations.
  • There is no universal performance model for JVM technology. Therefore, a technique that produces faster code on one JVM might not do so on another.

Design problems typically spring up because developers need to produce speedy code. Moreover, in their haste, they can neglect solid design principals for perceived (and unrealized) performance improvements. This might result in faster code, but it will also produce inefficient code and designs that lack robustness and extensibility. The bigger issue is this: applications designed in this manner require that developers predict potential performance problems without the benefit of the working code that produces measurable execution paths.

One golden rule for producing fast code is to optimize only what is being executed. Time spent optimizing code that does not have a substantial impact on performance is wasted. Typically, 80 to 90 percent of the execution time of an application is spent executing only 10 to 20 percent of its code. The 10 to 20 percent of your code that needs improvement is best found by using performance profilers.

Finally, efficient, fast code is closely related to good design, sound algorithms, and a good understanding of data structures and their implementation.

Reggie Hutcherson is a Sun technology
evangelist. He evangelizes Sun’s Java 2 Platform technologies
around the world, concentrating on J2SE and the HotSpot performance
engine.

Source: www.infoworld.com