Performance and design

This part of the C++ course makes the program faster without making it worse. A measurement is repeated and reported before a line is changed, a coverage build finds the hot function, and the layout of an array decides how much of the cache it uses. Copy elision and constexpr move work to compile time, and consteval refuses to run later. The part ends on design: the pimpl idiom, type erasure, the visitor, the strategy, the observer, the factories and the rules that keep an API safe to call.

11.1 Measuring performance steady_clock, a benchmark loop, warm-up and a measurement that is repeated Open 11.2 Profiling with gcov build with --coverage, run gcov, read the execution count of every line and find the hot function Open 11.3 Cache and data layout an array of structs against a struct of arrays, false sharing and a cache-friendly loop Open 11.4 Copy elision and RVO return value optimisation, guaranteed elision in C++17, and a move that never happens Open 11.5 constexpr a constexpr function, a constexpr variable, static_assert on the result and the limits of the evaluator Open 11.6 consteval and constinit immediate functions, constinit against static initialisation order, and when constexpr is not enough Open 11.7 The pimpl idiom hide the implementation behind a pointer, keep the header stable and pay for one allocation Open 11.8 Type erasure std::function, a hand-written wrapper with a virtual concept and a std::any alternative Open 11.9 The visitor pattern double dispatch with virtual functions, std::visit over a variant and a shape area Open 11.10 Strategy and policy swap an algorithm at run time with std::function, and at compile time with a template parameter Open 11.11 The observer pattern subscribers, weak references, notification order and an observer that unsubscribes Open 11.12 Factories and builders a factory function, a builder with named steps and an object that is valid when it is built Open 11.13 API design const correctness, [[nodiscard]], noexcept, stable headers and a misuse that no longer compiles Open

Lessons