Skip to content

Return an existing Rust JSG resource wrapper without the template lookup - #8668

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-19-rjsg-perf-rebench
Nov 20, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-19-rjsg-perf-rebench

Conversation

@littledivy

Copy link
Copy Markdown
Member

Returning a jsg::Rc whose resource already has a JS wrapper, as a getter that hands out a child object does, went through all the steps of creating one first. WrappableRc::to_js looked up the realm and the resource's constructor template, cloned the kj::Rc<Wrappable> to pass it by value, and called the wrap_resource shim, which only then found the existing wrapper. C++ JSG checks for the wrapper first.

In this patch a wrappable_get_wrapper shim returns the existing wrapper, following wrap_resource's rules for a wrapper in another isolate and for a pending exception, and to_js calls it before anything else. wrap_resource calls it too, so its contract doesn't change. Nothing visible changes, and the existing tests for wrapper identity, wrappers in other isolates and pending exceptions cover both paths.

bench-jsg resource/cached-wrap before after speedup C++ JSG
--config=benchmark 164 ns 145 ns 1.13x 82 ns
with --config=cross-lto (#8559) and an optimizing LTO link 130 ns 103 ns 1.27x 69 ns

Linux x86-64 (4 vCPU), bench-jsg linked statically, median CPU time over 4 processes of 5 repetitions, before and after on the same machine. The other cases, including resource/new-wrap, don't change beyond noise.

Returning a `jsg::Rc` whose resource already has a JS wrapper, as a getter that hands out a child object does, went through all the steps of creating one first. `WrappableRc::to_js` looked up the realm and the resource's constructor template, cloned the `kj::Rc<Wrappable>` to pass it by value, and called the `wrap_resource` shim, which only then found the existing wrapper. C++ JSG checks for the wrapper first.

In this patch a `wrappable_get_wrapper` shim returns the existing wrapper, following `wrap_resource`'s rules for a wrapper in another isolate and for a pending exception, and `to_js` calls it before anything else. `wrap_resource` calls it too, so its contract doesn't change. Nothing visible changes, and the existing tests for wrapper identity, wrappers in other isolates and pending exceptions cover both paths.

| bench-jsg `resource/cached-wrap` | before | after | speedup | C++ JSG |
|---|---:|---:|---:|---:|
| `--config=benchmark` | 164 ns | 145 ns | 1.13x | 82 ns |
| with `--config=cross-lto` (#8559) and an optimizing LTO link | 130 ns | 103 ns | 1.27x | 69 ns |

Linux x86-64 (4 vCPU), bench-jsg linked statically, median CPU time over 4 processes of 5 repetitions, before and after on the same machine. The other cases, including `resource/new-wrap`, don't change beyond noise.
@littledivy
littledivy requested review from a team as code owners November 19, 2026 12:28

@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 — This is a performance-only change. The new fast path for an existing wrapper handles a wrapper in another isolate and a pending exception the same way as before, and I found no issues.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

wrappable_get_wrapper runs the same checks in the same order that wrap_resource ran inline before, so wrap_resource behaves as it did. The early return in to_js keeps the same handling for a wrapper in another isolate and for a pending exception: a pending exception falls through to wrap_resource, which returns empty, and that becomes Error::pending. The shim checks for a pending exception before calling tryGetHandle, and as_pin_mut is used only inside the call.


📝 Documentation: ✅ LGTM

The doc comments added and updated in ffi.h and v8.rs match the new behaviour.


Reviewed commit: 987b15aa

@littledivy
littledivy merged commit 1218286 into main Nov 20, 2026
23 of 24 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-19-rjsg-perf-rebench branch November 20, 2026 04:21
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.

2 participants