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.

111 Measuring performance Instant, a benchmark loop, warm-up and a measurement that is repeated (C++ 118) Open 112 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 113 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 114 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 115 Hiding the implementation private fields, a public API over them, and Box<dyn Trait> where C++ used pimpl (C++ 124) Open 116 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 117 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 118 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 119 Observers and callbacks a list of callbacks, a subscriber that unsubscribes, and weak references where ownership is shared (C++ 128) Open 120 Factories and builders a factory function, a builder with named steps and an object that is valid when it is built (C++ 129) Open 121 API design in Rust newtypes, #[must_use], Result in the signature, and a misuse that no longer compiles (C++ 130) Open 122 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