6.6 Performance Checklist

Most of these suggestions apply only after a bottleneck has been identified:

  • Include all error-condition checking in blocks guarded by if statements.

  • Avoid throwing exceptions in the normal code path of your application.

  • Investigate whether a try-catch in the bottleneck imposes any extra cost.

  • Use instanceof instead of making speculative class casts in a try-catch block.

  • Consider throwing exceptions without generating a stack trace by reusing a previously created instance.

  • Include any exceptions generated during the normal flow of the program when running performance tests.

  • Assertions add overhead even when disabled, though an optimizing JIT compiler can eliminate the overhead (only HotSpot server mode succeeded in 1.4.0).

  • Beware of adding assertions to quick, frequently called methods.

  • Minimize casting.

  • Avoid casts by creating and using type-specific collection classes.

  • Use temporary variables of the cast type, instead of repeatedly casting.

  • Type variables as precisely as possible.

  • Use local variables rather than instance or static variables for faster manipulation.

  • Use temporary variables to manipulate instance variables, static variables, and array elements.

  • Use ints in preference to any other data type.

  • Avoid long and double instance or static variables.

  • Use primitive data types instead of objects for temporary variables.

  • Consider accessing instance variables directly rather than through accessor methods. (But note that this breaks encapsulation.)

  • Add extra method parameters when that would allow a method to avoid additional method calls.