Skip to content

The performance cost of C++ abstractions ​

This chapter is vol3's performance mirror. vol3 covers "why components like vector/string/function are designed the way they are" (design motivation); this vol6 chapter covers "what happens on the hardware after you use them that makes things fast or slow". The thesis is Carruth's There Are No Zero-Cost Abstractions: there are no zero-cost abstractions — every C++ abstraction maps to a hardware cost.

But a contrarian spirit runs through the whole chapter: "has a cost" does not mean "paid every time". The compiler often eliminates the cost for you — devirtualization turns a virtual call into a direct call, the zero-cost model keeps the normal path of exceptions free, and RVO makes return-by-value zero-copy. So the advice this chapter repeats over and over is "measure first; don't hand-write around it in advance".

Five articles:

  • 06-01 Virtual functions and devirtualization: a virtual call through a pointer costs 0.55ns (2.5x CRTP), but the compiler often devirtualizes. Don't CRTP-ify ahead of time.
  • 06-02 The zero-cost model of exceptions: the normal path costs 0.25ns (zero cost nailed down), throwing costs 857ns (3400x). Use exceptions only for truly exceptional cases.
  • 06-03 std::function's SBO: calls are 6x slower; construction goes through SBO when small and heap-allocates when large (8.5x). Watch out for repeated construction plus a large capture on hot paths.
  • 06-04 The cost cheat sheet: a roll-up plus variable storage types, bit-fields, enum class being zero-cost, and sizeof.
  • 06-05 RVO, NRVO, and move: return by value is zero-copy and zero-move (verified with the copy/move counter method); return std::move(local) is an anti-pattern.

Boundary: the design mechanisms of components (vector's three pointers, the SSO implementation, EBO) belong to vol3/vol4; vol6 only covers "the cost of running them on the hardware". High-frequency Zhihu questions (are virtual functions slow / are exceptions slow / function heap allocation / the move counter-example / return std::move) are folded into each article's entry point, none gets a standalone article.

In this chapter ​

pdf-latest-4-g85128cc · 85128cc · 2026-10-05