Repository navigation
Make the Rust MemoryCache's read() as cheap as C++ - #8804
Conversation
There was a problem hiding this comment.
Superseded by the review of
b724f849.
This review, of 738e7e02
✅ Approved — No issues found. The new pass_through() follows a promise the way C++ JSG does, it checks for a pending exception and registers its callback for snapshots, and a test checks that it settles one microtask later with the same value or reason.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security concerns.
📖 AGENTS.md: ✅ LGTM
Follows the repository conventions, including the rule that Rust JSG shims check exceptionPending() before calling V8.
📋 Code Quality: ✅ LGTM
No issues found.
- The C++ shim. It checks
exceptionPending()first, registers the callback as an external reference, and reports failure as an emptyMaybeLocal. - The Rust wrapper. It follows the existing
thenpattern. - The test. It checks that the promise settles one microtask later for both fulfillment and rejection.
- Removed code.
NoDatanow lives infallback.rs, its only user, and nothing still refers to the removed helper. - The benchmark build targets are configured correctly.
⚡ Performance: ✅ LGTM
The change removes the per-call data allocation, the second function and the rejection reaction from read(). No performance concerns.
📝 Documentation: ✅ LGTM
The doc comments match what the code does.
Reviewed commit: 738e7e02
738e7e0 to
b724f84
Compare
There was a problem hiding this comment.
Superseded by the review of
5229fbbf.
This review, of b724f849
✅ Approved — No issues found. pass_through() matches how C++ JSG follows a promise and follows the Rust JSG shim safety rules. The approver's comments (a comment at ffi.c++ and moving the benchmarks to src/bench) are addressed.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The shim checks exceptionPending() before it makes any V8 call that takes a context, and it returns an empty result when V8 fails. The Rust wrapper turns that empty result into Error::pending.
📖 AGENTS.md: ✅ LGTM
The change follows the repository's conventions, including the rule that a Rust JSG shim checks exceptionPending() before calling V8.
📋 Code Quality: ✅ LGTM
- The new comment in
ffi.c++explains whatpassValueandlocal_promise_pass_throughare for. - The benchmarks moved to
src/bench/memory-cache. In itsBUILD.bazel, the Rust config imports the C++ config, which is listed indata, and a comment explains why@all-autogatesis turned off for the C++ config. NoDatanow lives infallback.rs, its only user. Nothing still refers to thelib.rshelper this change removes.
⚡ Performance: ✅ LGTM
read() no longer allocates data for each call, creates a second function or registers a rejection reaction. It now creates one function and calls Then() once. The benchmark files add nothing to production code paths.
📝 Documentation: ✅ LGTM
The new comments match what the code does.
Reviewed commit: b724f849
There was a problem hiding this comment.
Superseded by the review of
b9dd4d08.
This review, of 5229fbbf
✅ Approved — No issues found. pass_through() matches how C++ JSG follows a promise and follows the Rust JSG shim safety rules. The approver's comments (a comment at ffi.c++ and moving the benchmarks to src/bench) are addressed.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The shim checks exceptionPending() before it makes any V8 call that takes a context, and it returns an empty result when V8 fails. The Rust wrapper turns that empty result into Error::pending.
📖 AGENTS.md: ✅ LGTM
The change follows the repository's conventions, including the rule that a Rust JSG shim checks exceptionPending() before calling V8.
📋 Code Quality: ✅ LGTM
- The new comment in
ffi.c++explains whatpassValueandlocal_promise_pass_throughare for. - The benchmarks moved to
src/bench/memory-cache. In itsBUILD.bazel, the Rust config imports the C++ config, which is listed indata, and a comment explains why@all-autogatesis turned off for the C++ config. NoDatanow lives infallback.rs, its only user. Nothing still refers to thelib.rshelper this change removes.
⚡ Performance: ✅ LGTM
read() no longer allocates data for each call, creates a second function or registers a rejection reaction. It now creates one function and calls Then() once. The benchmark files add nothing to production code paths.
📝 Documentation: ✅ LGTM
The new comments match what the code does.
Reviewed commit: 5229fbbf — same changes as b724f849, so this is that commit's review (no new review was run; line numbers are as of b724f849)
Nothing measured the MemoryCache binding, so the Rust MemoryCache behind RUST_MEMORY_CACHE had no numbers to compare with the C++ one. In this patch src/bench/memory-cache times read() hits and misses, with and without a fallback, a fallback that caches a new value, and delete(), with a number and an object value. memory-cache-rust.wd-bench runs the same cases with the RUST_MEMORY_CACHE autogate on. Misses and deletes use caches of their own, so they never evict the values that hit.
C++ JSG follows a promise that a method returns with one reaction: a function with no data that returns its argument, passed to Then() alone. Rust JSG code that needs the same follow has only then(), which allocates a Wrappable for the reaction's data, creates two functions and registers a rejection reaction. In this patch pass_through() does what C++ does, through a new local_promise_pass_through shim. It settles one microtask after the promise, with the same value or reason, as then() with an identity callback does. The new jsg-test checks that for a fulfillment and a rejection.
The Rust MemoryCache follows the promise read() returns, as C++ JSG does, with then() and an identity callback. That costs about 1 us a call, which made read() 1.4-2.0x slower than the C++ MemoryCache. In this patch read() uses pass_through(). It settles after the same number of microtasks, which settlesAfterAsManyMicrotasksAsCpp in memory-cache-test.js checks for both caches. JavaScript sees no change, and the Rust MemoryCache is behind the RUST_MEMORY_CACHE autogate, so there is no compat flag.
5229fbb to
b9dd4d0
Compare
There was a problem hiding this comment.
✅ Approved — No issues found. pass_through() reuses the existing then_conversion helper, so it follows a promise the way C++ JSG does, keeps the shim safety rules and gives the speedup the pull request claims.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The helper that pass_through() calls checks exceptionPending() before it uses a context, and turns an empty result from V8 into Error::pending. Rejections pass through unchanged. The change adds no new input or trust boundary.
📖 AGENTS.md: ✅ LGTM
The change follows the repository's conventions, including the Rust JSG shim safety rules.
📋 Code Quality: ✅ LGTM
pass_through() calls the existing then_conversion helper with undefined as its data and a non-capturing closure, and unit_value checks at compile time that the closure captures nothing. NoData now lives in fallback.rs, its only user, and nothing still refers to the helper removed from lib.rs.
⚡ Performance: ✅ LGTM
Each read() now creates one function and makes one Then() call, with no Wrappable and no rejection reaction. The benchmark files add nothing to production code paths.
📝 Documentation: ✅ LGTM
The doc comment on pass_through() matches what the code does.
Reviewed commit: b9dd4d08
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.delete() doesn't change.