Skip to content

Make Rust JSG promise reactions as cheap as C++ JSG's - #8806

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

littledivy merged 3 commits into
mainfrom
littledivy/2026-11-22-rjsg-promise-perf

Conversation

@littledivy

Copy link
Copy Markdown
Member

jsg-bench had no promise cases. With new ones, a Rust #[jsg_method] that returns or takes a jsg::Promise was 2.2-4.6x slower than the C++ JSG method returning jsg::Promise, on a cross-language LTO build. There were two causes. Rust converted a typed promise's value with the general then_or_catch(), which costs a new Wrappable, an opaque wrapper, a second function that only rethrows a rejection, and a TracedGlobal of the creator. C++ JSG's thenWrap()/thenUnwrap() is one function with the creator as its V8 data. Also, every Rust reaction function was created as a constructor, so V8 built a prototype object for each one, where C++ JSG passes ConstructorBehavior::kThrow.

In this patch jsg-bench gets promise cases for both bindings. The conversion reactions become one function added with a fulfilled-only Then(), through a single shim, and function_new() takes the constructor behaviour, so promise reactions are created with kThrow. Function::new and Function::from_callback, whose functions JavaScript can call and construct, are unchanged.

Rust/C++ CPU time per call, measured with --config=cross-lto and jsg-bench linked like workerd (#8790). Before and after binaries alternated on one executor, 4 rounds x 5 repetitions. Two runs agreed within 5%; the table shows the first. Each call also runs the microtasks, so the reactions are timed.

Case C++ Rust before Rust after Speedup Rust/C++ after
promise/resolved-void 404 ns 1863 ns 449 ns 4.1x 1.03x
promise/resolved (Promise<f64>) 763 ns 2165 ns 877 ns 2.5x 1.16x
promise/then-promise 2027 ns 5504 ns 2339 ns 2.4x 1.18x
promise/then-value 1828 ns 4070 ns 2140 ns 1.9x 1.18x

promise/throw-captured (Rust 0.14x of C++) and promise/untyped-void don't change.

Nothing a Worker can observe changes, so there is no compat flag. JavaScript never sees reaction functions. A rejection still passes through in the same number of ticks, a value of the wrong type still rejects with the same TypeError, and a method's resource stays alive until its promise settles. returned_promise_keeps_its_resource_alive covers that last point, and it fails if the creator isn't kept as the reaction's data. //src/rust/..., the memory-cache Rust tests and the jsg-bench smoke test pass, and each commit builds and passes clippy on its own.

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

@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 e4e751f5.

This review, of f7e5ce0b

✅ Approved — I found no correctness, security, performance or documentation issues. The reaction-function change matches C++ JSG's thenWrap()/thenUnwrap() pattern and follows the safety rules for Rust JSG shims.

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. The new C++ shim checks exceptionPending() before it calls V8, and it uses no C++ JSG.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

conversion_reaction and then_conversion follow the existing reaction pattern. Results go through set_return_value and errors through throw_exception. The shim returns an empty result when V8 can't create the function, and Rust turns that into Error::pending(). Only internal reaction functions use ConstructorBehavior::Throw.


⚡ Performance: ✅ LGTM

Each conversion now creates one V8 function with no prototype object, the same per-call cost as C++ JSG's thenWrap()/thenUnwrap().


📝 Documentation: ✅ LGTM

The README additions and doc comments match the code.


Reviewed commit: f7e5ce0b

@codecov-commenter

codecov-commenter commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 24.67532% with 58 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.73%. Comparing base (5ec8aa8) to head (e4e751f).
⚠️ Report is 66 commits behind head on main.

Files with missing lines Patch % Lines
src/rust/jsg/promise.rs 11.42% 31 Missing ⚠️
src/rust/jsg/ffi.c++ 14.28% 12 Missing ⚠️
src/rust/jsg/typed_promise.rs 0.00% 8 Missing ⚠️
src/rust/jsg/v8.rs 65.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8806      +/-   ##
==========================================
- Coverage   67.78%   67.73%   -0.05%     
==========================================
  Files         842      848       +6     
  Lines      214757   215714     +957     
  Branches    32572    32510      -62     
==========================================
+ Hits       145570   146114     +544     
- Misses      49947    50401     +454     
+ Partials    19240    19199      -41     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/rust/jsg-bench/bench.c++ Resolved
Comment thread src/rust/jsg-bench/bench.c++ Resolved
jsg-bench had no case for a method that returns or takes a promise, so
Rust JSG's promise paths were never compared with C++ JSG's.

In this patch both bindings get methods that return a resolved
Promise<double> and Promise<void>, an untyped promise, a .then() over a
Promise<double> argument, and a promise from a callback that throws.
Each call also runs the microtasks, so the reactions it adds are timed,
and both isolates capture throws as rejections, as
capture_async_api_throws makes them do.
When a method returns a jsg::Promise<T>, or takes one from JavaScript,
Rust JSG adds a reaction that converts the value, as C++ JSG's
thenWrap() and thenUnwrap() do. Rust made it with the general
then_or_catch(): a new Wrappable for its data, an opaque wrapper for
that, a second function that only rethrows a rejection, and a
TracedGlobal of the promise's creator. C++ JSG makes one function, with
the creator as its V8 data, and passes a rejection through natively.

In this patch the conversion reactions are plain function callbacks
added with a fulfilled-only Then(), and the creator is the function's
data, as in C++ JSG. A rejection still passes through in the same
number of ticks, a value of the wrong type still rejects with the same
TypeError, and the creator stays alive until the promise settles, which
returned_promise_keeps_its_resource_alive checks.
Rust JSG creates the functions of a promise reaction with
v8::Function::New's default ConstructorBehavior::kAllow, which makes
each a constructor, so V8 builds a prototype object for it as well.
C++ JSG creates its reaction functions with kThrow. JavaScript never
sees these functions, so how they are made can't be observed.

In this patch function_new() takes the constructor behaviour, and
then_or_catch() and the typed promise conversions create their
reactions with kThrow, as C++ JSG does. Function::new and
Function::from_callback, whose functions JavaScript can call and
construct, keep kAllow.
@littledivy
littledivy force-pushed the littledivy/2026-11-22-rjsg-promise-perf branch from f7e5ce0 to e4e751f Compare November 24, 2026 02:45

@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 — I found no correctness, security, performance or documentation issues. The only change since the last review moves the benchmark to the jsg::JsPromise and jsg::JsFunction wrappers, as an approver suggested.

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. The shim checks exceptionPending() before it calls V8, and it uses no C++ JSG.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates are needed.


📋 Code Quality: ✅ LGTM

The ConstructorBehavior values match V8's and are checked by static_assert. Only promise reactions use Throw. then_conversion takes F by value, which meets the precondition of unit_value::<F>(). The creator is kept alive as the reaction's V8 data, and returned_promise_keeps_its_resource_alive covers that. bench.c++ now uses JsPromise and JsFunction.


⚡ Performance: ✅ LGTM

Each conversion now creates one non-constructor V8 function, the same per-call cost as C++ JSG.


📝 Documentation: ✅ LGTM

The README additions and doc comments match the code.


Reviewed commit: e4e751f5

@littledivy
littledivy merged commit eb9c558 into main Nov 24, 2026
23 of 24 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-22-rjsg-promise-perf branch November 24, 2026 03:31
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.

3 participants