Make the Rust MemoryCache's read() as cheap as C++ #8804
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.37) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 225 lines diff across 2 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.324
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #8804"
general
weight: 0.35
jasnell
The Rust MemoryCache follows the promise read() returns, as C++ JSG follows a promise that a method returns, but it does that with
then()and an identity callback. Each call allocates a Wrappable for the reaction's data, creates two functions and registers a rejection reaction. C++ makes one function with no data and passes it toThen()alone. That made read() 1.4-2.0x slower than the C++ MemoryCache.In this patch Rust JSG gets
Local<Promise>::pass_through(), which follows a promise the way C++ does, and read() uses it. It settles after the same number of microtasks, whichsettlesAfterAsManyMicrotasksAsCppin memory-cache-test.js checks for both caches, so JavaScript sees no change. The Rust MemoryCache is behind the RUST_MEMORY_CACHE autogate, so there is no compat flag. A new jsg-test checks thatpass_through()settles one microtask after the promise, with the same value or reason.The first commit adds the benchmarks in src/bench/memory-cache (a C++ and a Rust config), which the numbers below come from. The C++ and Rust configs were run alternately in one action on the same bazelflare executor, in a
--config=benchmark --config=cross-ltobuild. Each number is the median of 8 rounds.| case | C++ | Rust before | Rust after | Rust before → after |
|---|---|---|---|---|
| read hit, number | 1567 ns | 2603 ns | 1296 ns | 2.0x |
| read hit with fallback, number | 1623 ns | 2470 ns | 1232 ns | 2.0x |
| read hit, object | 2395 ns | 3424 ns | 2138 ns | 1.6x |
| read hit with fallback, object | 2577 ns | 3559 ns | 2177 ns | 1.6x |
| read miss | 1202 ns | 2279 ns | 1023 ns | 2.2x |
| read miss, fallback caches a number | 10416 ns | 12575 ns | 10940 ns | 1.13x |
| read miss, fallback caches an object | 11272 ns | 14111 ns | 12718 ns | 1.13x |
delete() doesn't change.