The Hidden Performance Price of C++ Virtual Functions
summary
Shows measured costs, not a rant: a short virtual call costs about 20% extra (153 ms vs 126 ms) while long calls are almost identical (32.09 s vs 31.84 s); poor memory layout turns a 200 ms run into ~1.5 s (≈7–8×); unpredictable types hurt branch prediction (~<200 ms → ~500 ms, ~2.5×). Experiments use vector of 20 million objects, pointer shuffles and a prototype polymorphic vector. Tools reported: perf and Intel VTune, multitime and likwid. Fixes that work: per-type vectors, std::variant, type-based processing; the prototype approaches show measurable wins.
AI-written summary · about
chapters (13)
- 0:00Introduction
- 2:07How virtual functions work?
- 2:35How virtual functions work - virtual tables
- 5:03How virtual functions work-function calls
- 7:21Initial Analysis - experiment
- 13:10Vector of pointers - experiment
- 16:03Vector of pointers - conclusion
- 20:03Compiler Optimizations - Example
- 22:33Compiler Optimizations - experiment
- 24:35Compiler Optimizations - conclusion
- 27:12Jump Destination Guessing - experiment
- 31:28Instruction Cache Evictions - experiment
- 36:08Instruction Cache Eviction - conclusion
related talks
32mOptimizing Away C++ Virtual Functions May Be Pointless
01h 03mHidden Overhead of a Function API
01h 10mstd::function: a deep dive behind the curtain
01h 04mCPU design effects
01h 01mWant fast C++? Know your hardware!
01h 10mstd::function: a deep dive behind the curtain
01h 05mIs std::function really the best we can do?
01h 01mC++ RVO: Return Value Optimization for Performance in Bloomberg C++ Codebases
