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.

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

Lessons