demo · v137

WebAssembly Branch Hints

Wasm modules can now annotate branches as likely or unlikely. Engines use the annotations to pick code layout that helps the CPU branch predictor. The benchmark below loads two Wasm modules that compute the same thing — one with hints, one without — and runs them millions of times.

probing…
module: no hints
module: with hints
speedup
no run yet

the code

// Producer (e.g. binaryen) emits the metadata.code.branch_hint custom section:
//   if (cond)        if (hint:likely cond)
//     fast_path()      fast_path()
//   else             else
//     slow_path()      slow_path()

// Decoder sees the hint and arranges native code so fast_path is fall-through.
// Browser feature-detected via the Wasm validation API.

see also