Effective replacement of dynamic polymorphism with std::variant
summary
Compares three FSM implementations — single dynamic dispatch, double dispatch (visitor/CRTP) and several std::variant designs — by building a TCP-like state machine and microbenchmarking percentiles; variant implementations use std::visit, return std::optional next-state, avoid heap allocation and use value semantics, and show much lower latency and far tighter tail behavior (notably at the 99.9 percentile) than virtual-function approaches, so variant-based FSMs are faster, more deterministic, and practical where allocations and worst-case time matter.
AI-written summary · about
chapters (9)
- 0:00Introduction and motivation
- 3:49Finite state machines overview
- 7:51Single dynamic dispatch
- 12:13Double dynamic dispatch
- 12:34Introduction to std::variant
- 16:57Variant-based FSM engine
- 21:19Performance comparison
- 23:31Comparison summary
- 26:39Q&A session
related talks
22mEffective replacement of dynamic polymorphism with std::variant
01h 13mModernizing Finite State Machines in C++ - Empower Simplicity & Boost Performance With std::variant
55mHow to "OO" in C++
40mBack to Basics: Virtual Dispatch and its Alternatives
01h 23mRethink Polymorphism in C++
57mA Tour of Polymorphism Techniques in C++
05mGeneralised Double Dispatch
01h 31mDesigning a Slimmer Vector of C++ Variants
