Computed goto for efficient dispatch tables

2026-06-17

Link: https://eli.thegreenplace.net/2012/07/12/computed-goto-for-efficient-dispatch-tables

HN Discussion: 1 points, 0 comments

Eli Bendersky's 2012 post on computed goto is one of those quiet classics that resurfaces every few years because the technique it explains never stops being relevant. The article walks through a GCC extension — labels as values — that lets you take the address of a label with &&label and jump to it via goto *ptr. That sounds like a curiosity until you realize it's the trick that powers the interpreter loops in CPython, Ruby, LuaJIT, and most serious bytecode VMs.

The core problem: a naive switch-based dispatch loop compiles to a single indirect branch at the bottom of the loop. Modern CPU branch predictors keep per-branch history, so funneling every opcode through one branch destroys prediction accuracy. With computed goto, the dispatch jump is inlined at the end of every handler. Now the predictor sees a distinct branch site for each opcode, learns the bigram patterns ("ADD is often followed by STORE"), and your interpreter gets a substantial speedup — often cited as 15–25% on real workloads — without any algorithmic change.

What makes Bendersky's writeup worth the click:

Why it matters in 2026: the technique is showing up in surprising places. WASM interpreters, eBPF verifiers, regex engines, and the dispatch loops inside tokenizers all benefit from the same trick. Python 3.11+ adopted a specialized adaptive interpreter that leans heavily on computed goto. If you're building anything that loops over a stream of opcodes, tokens, or events, this is the first optimization to reach for after you've gotten the data structures right.

It's also a great teaching example for understanding how compiler extensions, CPU microarchitecture, and high-level language design quietly collude to make modern dynamic languages fast.

Why it deserves more upvotes: A concise, disassembly-backed explanation of the single optimization that quietly makes every major bytecode interpreter fast — still the canonical reference 14 years later.

All newsletters