Performance and design

The last chapter before the projects measures and then shapes. These twelve lessons time a hot loop and profile it with a counter and gdb, compare a vector of structs with a struct of vectors, and show where false sharing costs. Compile-time work with const fn follows, then the design lessons: a private field behind a public API, type erasure with dyn and Any, and the visitor, strategy, observer and builder patterns written the way Rust writes them. The chapter ends on API design and on no_std, where the same language runs without an operating system.

11.1 Measuring performance Instant, a benchmark loop, warm-up and a measurement that is repeated (C++ 118) Open 11.2 Profiling with counters and gdb a counter in the hot path, gdb on a slow loop, and the tools this toolchain does not have (C++ 119) Open 11.3 Cache, layout and false sharing a vector of structs against a struct of vectors, size_of a row, and false sharing between threads (C++ 120, 121) Open 11.4 const fn and compile-time work a const fn, a const item computed at compile time, a static array and the limits of the evaluator (C++ 122, 123) Open 11.5 Hiding the implementation private fields, a public API over them, and Box<dyn Trait> where C++ used pimpl (C++ 124) Open 11.6 Type erasure with dyn and Any Box<dyn Fn>, a hand-written trait object, Any and downcast, and what each one costs (C++ 125) Open 11.7 match instead of the visitor double dispatch with a trait, the same walk as a match over an enum, and a shape area (C++ 126) Open 11.8 Strategy at compile time and at run time a generic parameter for the policy, a Box<dyn Trait> for the same choice at run time (C++ 127) Open 11.9 Observers and callbacks a list of callbacks, a subscriber that unsubscribes, and weak references where ownership is shared (C++ 128) Open 11.10 Factories and builders a factory function, a builder with named steps and an object that is valid when it is built (C++ 129) Open 11.11 API design in Rust newtypes, #[must_use], Result in the signature, and a misuse that no longer compiles (C++ 130) Open 11.12 no_std and embedded Rust #![no_std], core against std, a panic handler and where Rust runs without an operating system (no C++ counterpart) Open

Lessons