Repository navigation
Return an existing Rust JSG resource wrapper without the template lookup - #8668
Conversation
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.
There was a problem hiding this comment.
✅ 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
Returning a
jsg::Rcwhose 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_jslooked up the realm and the resource's constructor template, cloned thekj::Rc<Wrappable>to pass it by value, and called thewrap_resourceshim, which only then found the existing wrapper. C++ JSG checks for the wrapper first.In this patch a
wrappable_get_wrappershim returns the existing wrapper, followingwrap_resource's rules for a wrapper in another isolate and for a pending exception, andto_jscalls it before anything else.wrap_resourcecalls 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.resource/cached-wrap--config=benchmark--config=cross-lto(#8559) and an optimizing LTO linkLinux 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.