Skip to content

perf(asm): cache the fall-through trace offset - #552

Open
quake wants to merge 5 commits into
nervosnetwork:developfrom
quake:perf/x64-trace-successor-develop
Open

quake wants to merge 5 commits into
nervosnetwork:developfrom
quake:perf/x64-trace-successor-develop

Conversation

@quake

@quake quake commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Change

Cache only the fall-through fixed trace slot on x86-64, AArch64 and riscv64. When building a trace, encode the byte offset for its sequential end PC in the upper word of the synthetic trace terminator. Its dedicated handler consumes that offset, then uses the existing destination-address, nonempty and cycle-budget checks. A colliding or empty slot takes the normal miss path.

Taken branches, direct and indirect jumps, VM entry and legacy trace ends use their original PC lookup. No taken-target hint, new trace field, struct-literal/API break, trace stride change or guest opcode. All supported ASM backends export ckb_vm_asm_linked_trace_end, so Rust emits the same terminator without architecture-specific code. No dependency on #550.

Both fixed and dynamic builders emit the terminator. Custom table sizes and the one-slot single-step invocation use the ordinary lookup instead.

Measurements (x86-64 only, before the handler-symbol rename)

Ryzen AI 9 HX 370, Linux/WSL2 x86-64, pinned logical CPU. Nine alternating before/after pairs against f88f133. The VM benchmark uses the existing release-0.24 secp256k1 benchmark ELF and arguments, V2 ASM + MOP, estimated cycles, fresh VM + load + run (30 warmups, 300 measured runs/process). The guest exits 0 at 1,148,317 cycles in both builds.

Workload Before → after (median of process medians) Median paired time reduction
secp256k1 VM 1.937 → 1.890 ms 2.22% (9/9)
Same, matched ASM ADD handler address 1.943 → 1.922 ms 1.06% (9/9)

The matched-address control uses alignment in separate benchmark builds only, not in the patch. These are workload-specific results; layout and run-to-run variance matter. No block replay.

Validation

  • New fall-through regression: pre-fill the destination slot with a different PC, fall through a not-taken branch and verify the exit and cycle limit across V0/V1/V2, including fixed and memoized dynamic decoders.
  • make test: 82 passed; make test-asm: 172 passed; make fmt, make clippy, and regenerated assembly constants.
  • QEMU user-mode: executed the complete tests/test_asm.rs target on AArch64 and riscv64 (30 passed on each), including the fall-through regression. These are correctness tests, not native performance measurements.
  • AArch64 assembly cross-assembled for macOS. x86-64 assembly cross-assembled for macOS and Windows; not execution-tested there. Full chaos suite not rerun on this revision; the earlier revision and baseline exhibited the same pre-existing test_artifact failure.

@quake quake changed the title perf(asm): cache x86-64 trace successor offsets perf(asm): cache the x86-64 fall-through trace offset Nov 29, 2026
@quake quake changed the title perf(asm): cache the x86-64 fall-through trace offset perf(asm): cache the fall-through trace offset Nov 29, 2026
@mohanson

Copy link
Copy Markdown
Collaborator

Moving the address of the next trace to a point determined during trace construction is a potentially useful optimization. However, the performance gain is too small to justify the added complexity.

Regarding pull requests (PRs) for CKB-VM performance optimization, we adhere to a consistent rule: in the interest of project stability and security, PRs offering only marginal performance gains should not be merged, unless there is compelling evidence to justify them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants