You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
engine: threaded VM dispatch to cut branch mispredicts #601
The VM dispatches every opcode through one match in Vm::eval_bytecode, which compiles to a single indirect jump.
On large models that one jump mispredicts a lot.
Threaded dispatch, where each opcode handler jumps directly to the next one, spreads the jump across many sites so the CPU can predict it per opcode.
In Rust that needs guaranteed tail calls (become), which are still unstable: the repo's pinned Rust 1.97.1 rejects become as experimental.
Why it matters
On an AMD Ryzen 9950X profile of C-LEARN, about 68% of branch misses were in this dispatch jump.
On an Apple M-series machine the same run was not limited by mispredicts (about 1% miss rate), so the payoff depends on the CPU.
Fusing common opcode sequences into single opcodes (already done) cut instructions and branches a lot but branch misses only a little, because the dispatches it removed were the easy-to-predict ones.
This is the one remaining idea aimed at mispredicts directly.
Fix
Wait for become to stabilize, then prototype threaded dispatch for the hot opcodes and measure C-LEARN and WORLD3 on more than one CPU, using retired instructions and branch misses rather than wall-clock alone.
Do not switch to nightly Rust for this.
Alternative: a register-based VM cuts dispatch count further than any dispatch-mechanism change, but it is a large rewrite of code generation.
Pointers
src/simlin-engine/src/vm.rs: Vm::eval_bytecode
docs/design/engine-performance.md: "R3. Faster dispatch" and "Measuring a change"
changed the title [-]engine: threaded VM dispatch (guaranteed tail calls) to cut the dispatch branch-miss bottleneck[/-][+]engine: threaded VM dispatch to cut branch mispredicts[/+]on Oct 12, 2026
Activity
[-]engine: threaded VM dispatch (guaranteed tail calls) to cut the dispatch branch-miss bottleneck[/-][+]engine: threaded VM dispatch to cut branch mispredicts[/+]