Effective replacement of dynamic polymorphism with std::variant
summary
Shows replacing virtual-based FSMs with std::variant plus visitors and occasional CRTP, removing heap allocations and virtual calls and using std::optional for “no-op” transitions. Implements three concrete FSMs (single dispatch, double dispatch/visitor, and variant-based visitors) and runs microbenchmarks that show variant solutions are substantially faster and also produce measurable improvement in production latency. Code uses value semantics, fold-expression tests for transition sequences, and avoids dynamic polymorphism and runtime allocation.
AI-written summary · about
chapters (9)
- 0:00Introduction to low latency
- 2:26Alternatives to polymorphism
- 4:08Traditional dynamic dispatch
- 7:18Double dynamic dispatch
- 9:12Curiously recurring templates
- 11:21Performance of dispatch types
- 13:25Variant approach in design
- 16:09CRTP and variant combined
- 18:48Benchmarking and conclusion
related talks
29mEffective 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++
01h 23mRethink Polymorphism in C++
57mA Tour of Polymorphism Techniques in C++
40mBack to Basics: Virtual Dispatch and its Alternatives
01h 31mDesigning a Slimmer Vector of C++ Variants
01h 00m“State Machines Battlefield - Naive vs STL vs Boost”
