Computed goto for efficient dispatch tables (2012)
- Programming
- Compilers
- Infrastructure
- Performance
The article is a 2012 walkthrough of "computed goto" in GNU C, a nonstandard extension that lets an interpreter jump directly to opcode handlers through a table of label addresses instead of re-entering a switch on every bytecode. It shows the classic result: in tight virtual machine dispatch loops, this often beats a switch, partly because the compiler emits less bookkeeping and partly because the CPU can predict the control flow better.
If you maintain an interpreter, state machine, or hot dispatch loop, measure dispatch shape on modern CPUs instead of assuming the compiler or a switch will do the right thing. The win here depends on branch prediction behavior, opcode layout, compiler output, and how much complexity you are willing to trade for a few percent to double-digit speedup.
- eli.thegreenplace.net
- Discuss on HN