Optimizing Away C++ Virtual Functions May Be Pointless - Shachar Shemesh - CppCon 2023

Discover the truth behind optimizing virtual C++ functions, separating fact from myth, and maximizing performance for realistic use cases, profiling, compilation flags, and cache considerations matter.

Key takeaways
  • Benchmarks are meaningless: Benchmarks can be misleading and don’t always reflect real-world performance.
  • Inlining is important: Inlining can have a significant impact on performance.
  • Virtual functions may not be slower: The cost of virtual functions is often exaggerated, and they may not be slower in all cases.
  • Profiling is essential: Profiling is necessary to understand the performance of your code.
  • Design for the specific problem: Don’t use a general-purpose design even if it’s common wisdom.
  • Caches play a crucial role: Caches can greatly affect the performance of your code.
  • Compilation flags and compiler versions matter: Different compilation flags and compiler versions can result in different performance profiles.
  • Hardware can also affect performance: The behavior of your code can differ based on the hardware it’s running on.
  • Profile, don’t benchmark: Profiling is a more reliable way to understand the performance of your code than benchmarking.
  • Inlining can explain performance differences: Inlining can explain why some code runs faster or slower than expected.
  • REAL-WORLD USE CASES: Don’t test hypotheses or general ideas, focus on real-world use cases.
  • CACHING IS COMPLEX: Caching is a complex topic and can greatly affect the performance of your code.