CPU cache essentials

Code cache tips:

  • Avoid iteration over heterogeneous sequence of objects with virtual calls. If we have sequence of heterogeneous objects the best thing would be to sort them by type so that executing virtual function of one object will lead to fetching code which can be used by the next object.
  • Make fast paths using branch-free sequences of code Inline cautiously.
    • Pros: reduce branches which will lead to speedup, compiler optimizations now possible
    • Cons: code duplication reduces code cache use
  • Use Profile-guided Optimizations (PGO) and Whole Program Optimizations (WPO) tools - these are automatic tools which will help you to optimize your code