Published February 25, 2020
by Doug Klugh

Tuning Well-Factored Code

Refactor code before optimizing to make your software more adaptable to performance tuning.  Building software that is well-factored without attention to performance will produce finer granularity for performance analysis — providing effective identification of performance hot spots.  Even when refactoring impacts performance, you can apply more effective performance-tuning enhancements with well-factored code.  First, write clean code that facilitates performance tuning, then optimize for time and footprint.

3 Approaches to Performance Optimization
Budgeting

This approach allocates a budgeted amount of resources for time and footprint.  As components are designed and implemented, they must not exceed their budget for resources.  However, if the capacity is avavilable, budgeted resources may be exchanged as needed.

Constant Attention

This approach requires continuous effort throughout the development lifecycle to always keep the performance within required limits.  This is a time-consuming approach that usually does not work well.  Performance improvements are usually made throughout the code when performance is largely impacted by only a very small portion of the code — assuming it is well factored.  A lot of time is often wasted optimizing code that does not need to be optimized.

Profiling

This approach utilizes a profiler to monitor a system to understand where it is consuming time and space.  This indicates small parts of the system that are performance hot spots — expending large amounts of resources.  Performance improvements can then be made using the same methods as in the Constant Attention approach.  But since we are focusing on a specific hot spot, we will gain much greater improvements within a small portion of code — making far better use of our time and avoiding rewriting code that has little to no impact on performance.  Profiling ensures we focus our efforts on code that truely affects performance — regardless of our intuition.

As with refactoring, we always recompile, retest, and rerun the profiler after making each small change.  And if performance is not improved with any single change, then back out that change and continue identifying and tuning hot spots until your system performs as expected.