← Back to Pull requests

Add Rust JSG Benchmarks #8013

Closed
unknown closed this pull request without merging on Oct 27, 2026 17:57 UTC
View on GitHub
unknown unknown commented on Oct 26, 2026 21:45 UTC
Author

Benchmark Rust JSG against C++ JSG

Why

More APIs are being written in Rust JSG, and we have no data on what the Rust bindings cost
compared to C++. This adds a benchmark that runs the same calls through both, so we can see where
Rust is slower, by how much, and what to fix first.

What this adds

src/rust/jsg-bench/:

  • Matching Rust and C++ test objects with the same methods, properties and structs. Each case

  • runs the same JavaScript loop against one or the other. Cases cover method calls, properties,
    argument conversion, structs, strings, arrays, typed arrays, callbacks, errors, object
    creation, native object lifetime, and GC.
  • bench-jsg, a Google Benchmark executable. Besides the plain comparison, it compares C++

  • and Rust with V8's Fast API enabled, and checks C++'s cached struct templates against Rust.
  • Result checks. Each case checks its result before and after timing, and that V8 kept the

  • loop optimized. With JSG_BENCH_VERIFY=1, the executable also checks that the Fast API cases
    took the fast path, and stops at the first failure.
  • smoke-test, which runs every case once in that mode. It's tagged workerd-benchmark, so

  • it runs only in CI configurations that build benchmarks.
  • analyze.py (optional, standard library only), which turns several runs' JSON output into

  • per-call times and Rust/C++ ratios. The README shows how to run it.

    No production code changes.

    Results

    Apple M5 Pro, macOS, --config=benchmark. 5 separate processes, 5 repetitions each, in random
    order. Ratios are Rust time / C++ time, so above 1× means Rust is slower. Each ratio is the
    median across processes, with the lowest and highest in parentheses. The no-binding control case
    measures 1.00× (0.99–1.02).

    | Case | C++ | Rust | Rust/C++ |
    |---|---:|---:|---:|
    | Method call, no arguments | 5.8 ns | 18.7 ns | 3.23× (3.13–3.30) |
    | Method call, u32 argument | 8.5 ns | 26.2 ns | 3.08× (3.00–3.15) |
    | Method call, Fast API on both sides | 3.7 ns | 9.9 ns | 2.67× (2.60–2.73) |
    | Property get | 9.4 ns | 19.7 ns | 2.10× (2.05–2.18) |
    | Call back into JS | 32.7 ns | 73.2 ns | 2.27× (2.20–2.42) |
    | Unwrap an object argument | 28.7 ns | 47.8 ns | 1.67× (1.66–1.74) |
    | Return a new object | 112 ns | 190 ns | 1.71× (1.60–1.81) |
    | GC with 10,000 live objects | 420 µs | 485 µs | 1.15× (1.13–1.19) |
    | Struct return, 8 fields | 350 ns | 492 ns | 1.41× (1.38–1.44) |
    | Struct return, C++ template cache on | 36 ns | 492 ns | 13.67× (13.54–14.20) |
    | 1,024-element array return | 1.6 µs | 30.1 µs | 19.00× (18.80–19.32) |
    | 64 KiB ASCII string argument | 2.0 µs | 25.3 µs | 12.33× (12.11–12.37) |
    | 64 KiB Uint8Array copied to Vec<u8> | 0.7 µs | 216 µs | 309.33× (300.57–314.13) |
    | Throw a TypeError | 5.6 µs | 0.48 µs | 0.09× (0.08–0.09) |

    Native memory per object (macOS allocator statistics): C++ uses 192 bytes in one allocation, Rust
    232 bytes in two.

    What stands out:

  • Per-call overhead. Rust adds about 5–33 ns to every call, 2.2–3.6× the C++ cost. Rust's Fast

  • API roughly halves its cost but doesn't close the gap.
  • Bulk conversions. Large arrays, ASCII strings and especially typed-array copies are much

  • slower. The typed-array copy goes element by element on trunk; a separate change bulk-copies it.
  • Struct returns. 1.4× slower than plain C++, and 13.7× slower than C++ with its cached

  • templates. Rust has no template cache yet.
  • Errors are faster in Rust. Building the JS error costs about the same in both. C++ JSG

  • throws a C++ exception for every error, and on macOS unwinding it takes 73–84% of the time.
    Rust returns an Err and never unwinds. The gap is probably smaller on Linux.

    These numbers measure single binding operations on one machine, not Worker request latency.

    Testing

  • smoke-test passes in optimized and default builds. A deliberately wrong expected result

  • makes it fail. With --config=rust-debug, which counts Rust Fast API calls, it confirms all
    six Rust Fast API cases take the fast path.
  • The C++ builds with -Werror, and the Rust passes clippy (--config=lint).

  • Tested on macOS only; not yet run on Linux.
  • Diff: 520 lines (+390 / -130) across 6 files Reviewer: nathanwhit Review duration: 1211m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 520 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.374
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8013"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.8% (MEDIUM) · Higher = larger blast radius
    47.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.9% · Safe to merge
    18.9%