← Back to Pull requests

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

Closed
unknown closed this pull request without merging on Nov 24, 2026 03:31 UTC
View on GitHub
unknown unknown commented on Nov 22, 2026 06:13 UTC
Author

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.

Diff: 303 lines (+227 / -76) across 3 files Reviewer: jasnell Review duration: 2718m
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 303 lines diff across 3 files
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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.342
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #8806"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 40.1% (MEDIUM) · Higher = larger blast radius
40.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 15.8% · Safe to merge
15.8%