1.4 A Tuning Strategy

Here's a strategy I have found works well when attacking performance problems:

  1. Identify the main bottlenecks (look for about the top five bottlenecks, but go higher or lower if you prefer).

  2. Choose the quickest and easiest one to fix, and address it (except for distributed applications where the top bottleneck is usually the one to attack: see the following paragraph).

  3. Repeat from Step 1.

This procedure gets your application tuned the quickest. The advantage of choosing the "quickest to fix" of the top few bottlenecks rather than the absolute topmost problem is that once a bottleneck has been eliminated, the characteristics of the application change, and the topmost bottleneck may not need to be addressed any longer. However, in distributed applications I advise you target the topmost bottleneck. The characteristics of distributed applications are such that the main bottleneck is almost always the best to fix and, once fixed, the next main bottleneck is usually in a completely different component of the system.

Although this strategy is simple and actually quite obvious, I nevertheless find that I have to repeat it again and again: once programmers get the bit between their teeth, they just love to apply themselves to the interesting parts of the problems. After all, who wants to unroll loop after boring loop when there's a nice juicy caching technique you're eager to apply?

You should always treat the actual identification of the cause of the performance bottleneck as a science, not an art. The general procedure is straightforward:

  1. Measure the performance by using profilers and benchmark suites and by instrumenting code.

  2. Identify the locations of any bottlenecks.

  3. Think of a hypothesis for the cause of the bottleneck.

  4. Consider any factors that may refute your hypothesis.

  5. Create a test to isolate the factor identified by the hypothesis.

  6. Test the hypothesis.

  7. Alter the application to reduce the bottleneck.

  8. Test that the alteration improves performance, and measure the improvement (include regression-testing the affected code).

  9. Repeat from Step 1.

Here's the procedure for a particular example:

  1. You run the application through your standard profiler (measurement).

  2. You find that the code spends a huge 11% of its time in one method (identification of bottleneck).

  3. Looking at the code, you find a complex loop and guess this is the problem (hypothesis).

  4. You see that it is not iterating that many times, so possibly the bottleneck could be outside the loop (confounding factor).

  5. You could vary the loop iteration as a test to see if that identifies the loop as the bottleneck. However, you instead try to optimize the loop by reducing the number of method calls it makes: this provides a test to identify the loop as the bottleneck and at the same time provides a possible solution. In doing this, you are combining two steps, Steps 5 and 7. Although this is frequently the way tuning actually goes, be aware that this can make the tuning process longer: if there is no speedup, it may be because your optimization did not actually make things faster, in which case you have neither confirmed nor eliminated the loop as the cause of the bottleneck.

  6. Rerunning the profile on the altered application finds that this method has shifted its percentage time down to just 4%. This method may still be a candidate for further optimization, but nevertheless it's confirmed as the bottleneck and your change has improved performance.

  7. (Already done, combined with Step 5.)

  8. (Already done, combined with Step 6.)