Branch Hinting
branch-hinting provides likely() and unlikely() helpers plus a transform that emits the metadata.code.branch_hint custom section in the final wasm.
Import
ts
import { likely, unlikely } from "as-labs/branch-hinting";Example
ts
export function classify(n: i32): i32 {
if (likely(n > 0)) return 1;
if (unlikely(n < 0)) return -1;
return 0;
}Transform
sh
asc assembly/index.ts --transform as-labs/branch-hinting -o build/module.wasmBehavior
likely()emits a\01branch hintunlikely()emits a\00branch hint- the helper wrappers are erased during compilation
- the transform currently targets direct statement conditions such as
if,while,do, andfor
Verification
The reliable verification target is the binary itself.
- inspect the final
.wasmformetadata.code.branch_hint - do not rely on generic
wasm2watoutput to re-render hint annotations - if you want to inspect the annotation in text form, use transform-generated
.watoutput rather than decompiling the binary later
