← Back to Pull requests

Allocate Rust JSG resources in their Wrappable's allocation #8689

Closed
unknown closed this pull request without merging on Nov 19, 2026 22:14 UTC
View on GitHub
unknown unknown commented on Nov 19, 2026 19:59 UTC
Author

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 that Rc. Every jsg::Rc also held its own std::rc::Rc, so clone and drop kept a Rust refcount next to the kj::Rc and strong refcounts.

This PR puts the resource in the Wrappable's allocation and drops the std::rc::Rc entirely:

  • Wrappable::allocate() over-allocates with a class-specific operator new. Rust moves the resource into the trailing storage, and ~Wrappable drops it in place. jsg::Rc holds a pointer to the resource. Function::new data and startup snapshot copies (SnapshotCopy) use the same path.

  • jsg::Weak holds a kj::WeakRc<Wrappable>, mirrored as a CXX shared struct the same way kj-rs mirrors kj::Rc. It still expires before the resource's Drop runs, and it still refuses a condemned Wrappable. A Weak that outlives its resource keeps only the small weak cell.

  • Resources and function data are limited to 16-byte alignment, the default operator new alignment. A const assertion enforces it, and a new test covers a 16-byte-aligned resource.

  • The second commit has 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-bench Metadata/native-memory-rust): 2 malloc blocks / 256 bytes → 1 block / 240 bytes.

    jsg-bench, main vs. 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 jsg is clean. ASan has not been run locally.

    Diff: 311 lines (+233 / -78) across 3 files Reviewer: nathanwhit Review duration: 135m
    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
    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.344
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8689"
    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.3% (MEDIUM) · Higher = larger blast radius
    40.3%
    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.9% · Safe to merge
    15.9%