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.
- Software Engineering 16.2k 16.2k 33 gold badges3232 silver badges7878 bronze badges
- Stack Overflow 5.9k 5.9k 11 gold badge2121 silver badges4545 bronze badges
- Signal Processing 522 522 44 silver badges1313 bronze badges
- Electrical Engineering 221 221 11 silver badge77 bronze badges
- Physics 201 201 11 gold badge22 silver badges55 bronze badges
- View network profile
Top network posts
- 38 Why was the Itanium processor difficult to write a compiler for?
- 36 OpenCV - approxPolyDP for edge maps (not contours)
- 33 why is 1>>32 == 1?
- 19 Is passing arguments as const references premature optimization?
- 18 How to resolve class interdependence in my C++ code?
- 17 Is an empty 'while' loop bad practice?
- 17 Fostering a time period where everyone can try any ideas to make software run faster?
- View more network posts →
This user doesn’t have any gold badges yet.
This user doesn’t have any silver badges yet.
Keeping a low profile.
This user hasn't posted yet.