Computed goto for efficient dispatch tables
a day ago
- #optimization
- #programming
- #virtual-machines
- Python's bytecode VM uses GCC's computed gotos for performance improvements.
- A simple VM implemented with computed gotos is 25% faster than using a switch statement.
- The speed advantage comes from reduced bounds checking and better hardware branch prediction.
- Computed gotos expose CPU indirect jumps, allowing per-opcode branch prediction.
- Other VMs like Ruby 1.9 and Dalvik use computed gotos, while Lua 5.2 uses a switch.
- The switch version does extra bounds checking per iteration due to C standard requirements.
- Branch predictors work better with multiple separate jumps (computed goto) than a single shared jump (switch).