Repository navigation
Make Rust JSG promise reactions as cheap as C++ JSG's - #8806
Conversation
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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.
f7e5ce0 to
e4e751f
Compare
There was a problem hiding this comment.
✅ 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
jsg-bench had no promise cases. With new ones, a Rust
#[jsg_method]that returns or takes ajsg::Promisewas 2.2-4.6x slower than the C++ JSG method returningjsg::Promise, on a cross-language LTO build. There were two causes. Rust converted a typed promise's value with the generalthen_or_catch(), which costs a new Wrappable, an opaque wrapper, a second function that only rethrows a rejection, and aTracedGlobalof the creator. C++ JSG'sthenWrap()/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 passesConstructorBehavior::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, andfunction_new()takes the constructor behaviour, so promise reactions are created withkThrow.Function::newandFunction::from_callback, whose functions JavaScript can call and construct, are unchanged.Rust/C++ CPU time per call, measured with
--config=cross-ltoand 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.promise/resolved-voidpromise/resolved(Promise<f64>)promise/then-promisepromise/then-valuepromise/throw-captured(Rust 0.14x of C++) andpromise/untyped-voiddon'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_alivecovers 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.