Add Rust JSG Benchmarks #8013
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
nathanwhit
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/: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.
loop optimized. With
JSG_BENCH_VERIFY=1, the executable also checks that the Fast API casestook the fast path, and stops at the first failure.
smoke-test, which runs every case once in that mode. It's taggedworkerd-benchmark, soit runs only in CI configurations that build benchmarks.
analyze.py(optional, standard library only), which turns several runs' JSON output intoper-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 randomorder. 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,
u32argument | 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
Uint8Arraycopied toVec<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:
API roughly halves its cost but doesn't close the gap.
slower. The typed-array copy goes element by element on trunk; a separate change bulk-copies it.
templates. Rust has no template cache yet.
throws a C++ exception for every error, and on macOS unwinding it takes 73–84% of the time.
Rust returns an
Errand never unwinds. The gap is probably smaller on Linux.These numbers measure single binding operations on one machine, not Worker request latency.
Testing
smoke-testpasses in optimized and default builds. A deliberately wrong expected resultmakes it fail. With
--config=rust-debug, which counts Rust Fast API calls, it confirms allsix Rust Fast API cases take the fast path.
-Werror, and the Rust passes clippy (--config=lint).