Back to smallsm

Benchmarks

On random events, smallsm takes 1.8 times as long as a hand-written switch, or 1.76 ns more per event.

I compared dispatch with a hand-written enum and switch on the same graph of 3 states, 4 events and 5 transitions. The test used a Ryzen 9 7950X and MSVC at /O2. Each figure is the median nanoseconds per event over 15 runs.

Scenario switch smallsm Ratio
Fixed cycle, events known at compile time 0.32 0.18 0.6x
Random runtime events 2.12 3.88 1.8x
Size switch smallsm
sizeof machine 1 byte 4 bytes

Random runtime events: this is the realistic comparison. It uses 2^22 event codes read from memory, and most events are rejected. The timing includes the switch that turns each code into a typed event, because real programs need that step too.

Fixed cycle: this repeats start, pause, resume, stop 2^20 times, and the optimiser collapses most of the loop. I don't count it as faster than a switch.

Both loops add accepted events to a volatile sink, which stops the compiler removing them. Neither machine has hooks.

The extra time is fine for per-frame game logic, protocols and UI, and may be too much for the inner loop of a lexer. These numbers come from one machine and one compiler.