Skip to content

Make the Rust MemoryCache's read() as cheap as C++ - #8804

Merged
littledivy merged 3 commits into
mainfrom
littledivy/2026-11-22-perf-memcache-rust
Nov 24, 2026
Merged

littledivy merged 3 commits into
mainfrom
littledivy/2026-11-22-perf-memcache-rust

Conversation

@littledivy

@littledivy littledivy commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

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 to Then() 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, which settlesAfterAsManyMicrotasksAsCpp in 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 that pass_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-lto build. 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.

@littledivy
littledivy requested review from a team as code owners November 22, 2026 05:05

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 empty MaybeLocal.
  • The Rust wrapper. It follows the existing then pattern.
  • The test. It checks that the promise settles one microtask later for both fulfillment and rejection.
  • Removed code. NoData now lives in fallback.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

Comment thread src/rust/jsg/ffi.c++ Resolved
Comment thread src/bench/memory-cache/memory-cache.js Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-22-perf-memcache-rust branch from 738e7e0 to b724f84 Compare November 24, 2026 02:51

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 what passValue and local_promise_pass_through are for.
  • The benchmarks moved to src/bench/memory-cache. In its BUILD.bazel, the Rust config imports the C++ config, which is listed in data, and a comment explains why @all-autogates is turned off for the C++ config.
  • NoData now lives in fallback.rs, its only user. Nothing still refers to the lib.rs helper 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

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 what passValue and local_promise_pass_through are for.
  • The benchmarks moved to src/bench/memory-cache. In its BUILD.bazel, the Rust config imports the C++ config, which is listed in data, and a comment explains why @all-autogates is turned off for the C++ config.
  • NoData now lives in fallback.rs, its only user. Nothing still refers to the lib.rs helper 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)

@littledivy
littledivy enabled auto-merge November 24, 2026 03:08
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.
@littledivy
littledivy force-pushed the littledivy/2026-11-22-perf-memcache-rust branch from 5229fbb to b9dd4d0 Compare November 24, 2026 03:43

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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

@littledivy
littledivy merged commit 3e3dd36 into main Nov 24, 2026
36 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-22-perf-memcache-rust branch November 24, 2026 04:09
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.

2 participants