Los Angeles, CA
Guidelines for C++ template-based micro-optimization
- Start with the lowest level, most frequently executed code first.
- Having chosen a fixed set of data types, implement the code in assembly.
- Reimplement the code in plain C, not using any abstract data types, so that it compiles into the same assembly instructions.
- Repeat steps 2 and 3 with a few different sets of data types.
- Take all of the C code from steps 2 and 4, and reimplement using C++ templates. Determine the template arguments.
- Identify all of the non-type-based validations needed by the template function.
- Identify all of the type-based validations needed by the template function.
- Package the template function into a class template method, and implement the non-type-based validations as its methods.
- Add the type-based validations to the class template.
- Identify any types, ones that a programmer-user might be tempted to instantiate the class template with, and test whether this would result in undesirable effects. (See note for step 10.)
- Implement additional type-based validations to guard against the scenarios identified in step 10.
Note for step 10.
- Compiler errors (failure to build) is a desirable effect, if the usage is indeed ill-formed.
- A successful build that results in an incorrect executable (one that produces incorrect result when executed) is the most severe undesirable effect.
- An unsuccessful build that results in hard-to-understand compiler error messages is a mildly undesirable effect.
- A source file that triggers compiler crashes (which may be possibly vendor-specific) is also a mildly undesirable effect.