Los Angeles, CA

Guidelines for C++ template-based micro-optimization

  1. Start with the lowest level, most frequently executed code first.
  2. Having chosen a fixed set of data types, implement the code in assembly.
  3. Reimplement the code in plain C, not using any abstract data types, so that it compiles into the same assembly instructions.
  4. Repeat steps 2 and 3 with a few different sets of data types.
  5. Take all of the C code from steps 2 and 4, and reimplement using C++ templates. Determine the template arguments.
  6. Identify all of the non-type-based validations needed by the template function.
  7. Identify all of the type-based validations needed by the template function.
  8. Package the template function into a class template method, and implement the non-type-based validations as its methods.
  9. Add the type-based validations to the class template.
  10. 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.)
  11. 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.
Top Questions
1 2 3

Top Answers
1 2 3 4