Title: Java Performance Tuning Author: Jack Shirazi Publisher: September 2000, O'Reilly Books Pages: 426, softbound ISBN: 0-596-00015-4 Price: $34.95 So you bit the bullet and wrote that big application in Java, and now the boss says it runs too slow. Youll probably want to get this book. It presents a wide variety of potential optimizations for Java programs, and chances are one or two will help you speed things up. A note to the truly desperate, however: this book is not (nor does it purport to be) a miracle cure. Of the many optimizations it presents, perhaps only a few will be appropriate for any single application. Also note that many of these optimizations may bring only modest performance gains, on the order of 10-30 per cent. That is just the nature of the optimization game. If youre going to play, you cant be an idealist.
The book begins with a discussion of tuning strategies and profiling. This seems appropriate, because it is where Java tuners should begin as well. Tuning a Java application without profiling it first is virtually guaranteed to be a waste of time. Here the author gives us a simple piece of advice: fix one thing at a time, and profile again. Thats because hot spots (the places that burn up the most time) tend to move around as you modify the code. If you do a lot of performance tuning, you probably knew that already, but it may not be obvious to newbies.
The bulk of the book is dedicated to the biggest performance problem areas in Java, with roughly one chapter given over to each culprit. There is a chapter each on object creation; exceptions, casts, and variables; loops and switches; I/O; sorting; threading; algorithms and data structures; distributed computing; and Strings the only Java data type that gets a chapter all to itself.
Since Strings seem to be such a big problem area, you might naturally wonder if there is something wrong their design. I would say not necessarily. Remember that strings are among the most frequently used objects in any of the programming languages that support them. So even minor performance problems in a string class will tend to be magnified. And the Java String type is apt to be used naively, since it supplies a couple seemingly innocent functions which are in fact deadly in terms of performance. If Sun can be said to have erred, it was in making String so easy to misuse.
Using This Book
In many ways, tuning a Java app appears to be a thankless task. If youre going to do it and enjoy it, you have to appreciate small victories, and you must have a flair for configuration management. Thats because many of the optimizations possible can be rendered either more effective, or ineffective, by choice of JVM, choice of compiler, and the multiple options under which they may be run. Note that this configuration dependence severely undercuts one of big Javas selling points its portability. So one way to use this book (admittedly, probably not a way the author intended) is to read it first to decide how badly you want to implement that next project in Java. However, this book does present some optimizations that appear to be fairly independent of configuration; its just too bad they are not organized as such. My first suggestion would be to go through this book with a highlighter and mark all the optimizations that are configuration independent. You could save yourself a lot of headaches down the road.
The author does provide checklists at the end of each chapter, of all the optimizations you might try, along with dos and donts. A table might have been better, that organized optimizations by configuration sensitivity and projected performance gains (ballpark, of course).
No Religion Here
This is a fairly pragmatic book. At one point, when an optimization does not produce the results expected for a particular compiler, the author simply notes it and moves on, commenting that he doesnt wish to get wrapped up in analyzing a particular JDKs implementation. I think this is a sensible attitude; if you need better performance, you probably need it bad; theres no time to obsess over something that doesnt work.
In keeping with its pragmatic nature, the book contains numerous small tables that show the results of particular optimizations on specific compilers/platforms typically JDK 1.1.6, JDK 1.2 with and without JIT, JDK 1.3, and HotSpot 1.0. These tables are useful and often contain surprising results, such as JDK 1.1.6 sometimes outperforming HotSpot 1.0. (Although the books discussions of HotSpot are few and far between, I found them very interesting; in general, the HotSpot results bring a sobering dose of reality to notions that some sort of wonder JVM, or even a hardware solution, might fix all of Javas performance problems.)
I appreciate the authors neutral stance with respect to Java. Java Performance Tuning takes no position on whether or not Java is a good language, or whether you should have used it in the first place. Instead, the book pretty much says, I dont care how you got here, but now that you are, here are some things that can help. Thats a nice change of pace from all the hype.
Marc Briand is the former editor of C/C++ Users Journal. His interests include computer languages, graphics, animation, and just about any area where technical and aesthetic problems meet. He can be reached at marcbriand@cs.com.