Skip to content

engine: threaded VM dispatch to cut branch mispredicts #601

Open

Description

@bpowers

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

Activity

  1. added
    enhancement
    engine
    on May 21, 2026
  2. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions