Skip to content

perf(interpreter): add unlikely() hint to gas-block charge in run() hot loop#810

Open
zhoutianxia1 wants to merge 3 commits into
jarchain:masterfrom
zhoutianxia1:perf/interpreter-gas-branch-analysis
Open

perf(interpreter): add unlikely() hint to gas-block charge in run() hot loop#810
zhoutianxia1 wants to merge 3 commits into
jarchain:masterfrom
zhoutianxia1:perf/interpreter-gas-branch-analysis

Conversation

@zhoutianxia1
Copy link
Copy Markdown

Summary

Gas-block boundaries are sparse (~5-15% of instructions for typical workloads like blake2b, keccak, ed25519). The bb_gas_cost > 0 guard in the run() hot loop is almost always false, but without an explicit hint LLVM cannot prove this and may not lay out the fall-through path as the primary code stream.

This PR adds core::intrinsics::unlikely() around the guard so LLVM moves the gas-charge block to a cold section, keeping the sequential hot path as fall-through code - reducing I-cache footprint per iteration.

Changes

  • grey/crates/javm/src/lib.rs: add #![feature(core_intrinsics)] + #![allow(internal_features)]. The project already requires nightly (rust-toolchain.toml pins nightly edition 2024).
  • grey/crates/javm/src/interpreter/mod.rs: import core::intrinsics::unlikely; wrap bb_gas_cost > 0 check.
  • docs/interpreter-gas-branch-misprediction-analysis.md: full analysis including frequency study, cache impact, micro-architectural cost estimate, recommendations.

Analysis

Key findings:

  • Gas-block starts are ~5-15% of instructions (workload-dependent)
  • The outer branch is strongly biased false, already well-predicted by hardware
  • The main win is I-cache layout: unlikely moves the cold charge block out-of-line, shrinking the hot path cache footprint
  • Expected: 1-3% throughput improvement on instruction-dense workloads

Correctness

Zero change in semantics or gas values. unlikely is a pure code-generation hint.

cargo check -p javm passes with no errors and no warnings.

Refs: #400

1825833563 and others added 3 commits April 25, 2026 16:50
Two compile errors prevented grey from building on Windows:

1. build.rs: PathBuf::display() emits Windows backslash separators which
   Rust's lexer then interprets as escape sequences inside include_bytes!
   string literals (e.g. \j, \g, \o). Fix by replacing '\\' with '/'
   before embedding the path into the generated service_blobs.rs.

2. node.rs: tokio::signal::unix is not available on Windows. Wrap the
   SIGTERM, SIGUSR1, and SIGHUP signal registrations in #[cfg(unix)]
   and provide std::future::pending::<()>() placeholders on non-Unix so
   the select! arms compile and are simply never selected.
…ot loop

Gas-block boundaries are sparse (~5-15% of instructions). The �b_gas_cost > 0
guard is almost always false, but without an explicit hint LLVM may not lay out
the fall-through path optimally.

Changes:
- Add #![feature(core_intrinsics)] + #![allow(internal_features)] to
  crates/javm/src/lib.rs (project is already nightly-only per rust-toolchain.toml)
- Import core::intrinsics::unlikely in interpreter/mod.rs
- Wrap �b_gas_cost > 0 check with unlikely() so LLVM moves the charge
  block to a cold section, keeping the sequential hot path as fall-through

Expected effect: 1-3% I-cache improvement on instruction-dense workloads
(blake2b, keccak, ed25519). No change in semantics or gas values.

Also add docs/interpreter-gas-branch-misprediction-analysis.md with full
frequency analysis, cache impact study, and micro-architectural cost estimate.

Refs: jarchain#400 (Optimize javm interpreter performance)
@github-actions
Copy link
Copy Markdown
Contributor

Genesis Review

Comparison targets:

How to review

Post a comment with the following format (rank from best to worst):

/review
difficulty: <commit1>, <commit2>, ..., <commitN>, currentPR
novelty: <commit1>, <commit2>, ..., <commitN>, currentPR
design: <commit1>, <commit2>, ..., <commitN>, currentPR
verdict: merge

Use the short commit hashes above and currentPR for this PR.
Each line ranks all comparison targets + this PR from best to worst.

To meta-review another reviewer's comment, react with 👍 or 👎.

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.

1 participant