Modernizing Finite State Machines in C++ - Empower Simplicity & Boost Performance With std::variant
summary
Implements a turnstile FSM (five states: locked, unlocked, payment processing, success, failed) using std::variant and std::optional, replacing virtual-state classes and enums, and adds a pluggable external transition table/strategy. The implementation removes heap allocation and virtual calls, keeps state-local data, and templates a TFSM engine that visits the active state. Benchmarks show std::variant about 90% faster than State pattern; after removing a timeout manager artifact it runs roughly 2% faster than enums and about 24× faster than the State pattern.
AI-written summary · about
chapters (9)
- 0:00Introduction and speaker background
- 7:09Finite State Machines overview
- 12:54FSM implementation with enums
- 23:15Problems with enum-based FSMs
- 25:03State pattern for FSMs
- 35:24Modernizing FSMs with std::variant
- 43:40Performance comparison and benchmarks
- 55:34Advanced strategies and policy-based design
- 1:01:06Q&A and closing remarks
related talks
29mEffective replacement of dynamic polymorphism with std::variant
22mEffective replacement of dynamic polymorphism with std::variant
01h 00m“State Machines Battlefield - Naive vs STL vs Boost”
05m[BOOST].SML STATE MACHINE LANGUAGE
44mManaging Complex Application State with Finite-State Machines
09mA State Machine walks into a Svelte Component
28mState machines for frontend state management
20mTaming UI complexity with Typed State Machines
