Allocate Rust JSG resources in their Wrappable's allocation #8689
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 · 311 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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.344
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #8689"
general
weight: 0.35
nathanwhit
A Rust JSG resource used to take two heap allocations: an
std::rc::Rc<R>holding the resource, and the C++Wrappable, which owned a leaked clone of thatRc. Everyjsg::Rcalso held its ownstd::rc::Rc, so clone and drop kept a Rust refcount next to thekj::Rcand strong refcounts.This PR puts the resource in the
Wrappable's allocation and drops thestd::rc::Rcentirely:Wrappable::allocate()over-allocates with a class-specificoperator new. Rust moves the resource into the trailing storage, and~Wrappabledrops it in place.jsg::Rcholds a pointer to the resource.Function::newdata and startup snapshot copies (SnapshotCopy) use the same path.jsg::Weakholds akj::WeakRc<Wrappable>, mirrored as a CXX shared struct the same way kj-rs mirrorskj::Rc. It still expires before the resource'sDropruns, and it still refuses a condemnedWrappable. AWeakthat outlives its resource keeps only the small weak cell.operator newalignment. A const assertion enforces it, and a new test covers a 16-byte-aligned resource.attachWrapper()write the Rust marker itself, so the wrapper's internal field is set once instead of twice.The main benefits are memory and simpler ownership. Timing is mostly neutral to slightly better.
Native memory per resource (
jsg-benchMetadata/native-memory-rust): 2 malloc blocks / 256 bytes → 1 block / 240 bytes.jsg-bench,mainvs. this PR: macOS arm64,--config=benchmark. Each figure is the median CPU time per operation over 6 processes of 5 interleaved repetitions. Other processes put intermittent load on the machine, so allow about ±10% noise.| Case | main | PR |
| --- | --- | --- |
|
Rust/native/lifetime/0| 38.2 ns | 31.2 ns (0.82x) ||
Rust/native/lifetime/1| 48.6 ns | 43.5 ns (0.90x) ||
Rust/native/lifetime/16| 191.7 ns | 153.7 ns (0.80x) ||
Rust/resource/new-wrap| 152.5 ns | 142.6 ns (0.94x) ||
Rust/resource/constructor| 144.5 ns | 134.7 ns (0.93x) ||
Rust/gc/allocate-collect/10000| 1.62 ms | 1.46 ms (0.90x) |Each commit message has its own numbers against its parent.
Testing:
//src/rust/...,//src/workerd/jsg/...and//src/workerd/api/tests:all(which includes the startup snapshot tests) pass, both with and without--config=rust-debug, using--test_size_filters=.just clippy jsgis clean. ASan has not been run locally.