Repository navigation
Add Function::from_callback() and call functions without copying - #8726
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #8726 +/- ##
=======================================
Coverage 67.68% 67.68%
=======================================
Files 839 839
Lines 214024 214034 +10
Branches 32530 32531 +1
=======================================
+ Hits 144864 144871 +7
- Misses 49910 49911 +1
- Partials 19250 19252 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Superseded by the review of
7f1ec287.
This review, of 561d39a2
💬 Approved with comments — No correctness issues in the FFI or the new API; the only note is an INFO that the Rust JSG README doesn't document Function::from_callback() yet.
Findings: 1 info (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📝 Documentation: 💬 1 info
The Rust JSG README does not document the new Function::from_callback() API.
Recommendation: 💬 Optional Nits
- [INFO] Rust JSG README doesn't document
Function::from_callback()or its trade-offs. (src/rust/jsg/v8.rs:2461)
View full review
src/rust/jsg/README.mdexplains how to create functions from Rust closures withFunction::new, but doesn't mention the new publicFunction::from_callback()or its trade-offs.
✅ All other checks passed
📋 Code Quality: ✅ LGTM
local_function_call() passes the argument slice to V8 without copying it. This relies on ffi::Local having the same layout as v8::Local, which ffi-inl.h asserts. An empty slice becomes nullptr with a count of 0. from_callback() uses the existing function_new shim with undefined as data and keeps its error handling (Error::pending). The new tests cover both an extern "C" callback and one built with function_callback().
Reviewed commit: 561d39a2
561d39a to
7f1ec28
Compare
There was a problem hiding this comment.
Superseded by the review of
37f5688c.
This review, of 7f1ec287
💬 Approved with comments — No correctness issues in the no-copy call path or in from_callback(). There are two INFO notes: the guard throws from a shim that isn't declared Result, though no real call can reach it, and the README is missing the new API.
Findings: 2 info (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 info
The FFI change and the new API are correct. One INFO: the new KJ_REQUIRE throws from a shim that isn't declared Result.
Recommendation: 💬 Optional Nits
- [INFO] New
KJ_REQUIREinlocal_function_callthrows from a shim that isn't declaredResult, so an oversized slice panics instead of being rejected. (src/rust/jsg/ffi.c++:652)
View full review
src/rust/jsg/ffi.c++:652:KJ_REQUIRE(args.size() <= INT_MAX, ...)throws a KJ exception.local_function_callis declared inv8.rs:604-609as returning a plainMaybeLocal, notResult<MaybeLocal>. The rule inffi.h:243-244says a shim that can throw must be declaredResult. Otherwise an oversized slice turns into a panic rather than the rejection the description promises.- Only a slice of more than 2^31
Locals reaches it, so no real call can.
- Only a slice of more than 2^31
I found nothing wrong in these:
- Passing the slice without a copy. The layout is asserted in
ffi-inl.h, and an empty slice becomesnullptrwith a count of 0. - The order of the
exceptionPendingguard. - How
from_callback()handles an error (Error::pending). - The new tests.
📝 Documentation: 💬 1 info
The Rust JSG README still doesn't document Function::from_callback().
Recommendation: 💬 Optional Nits
- [INFO] Rust JSG README (and
src/rust/AGENTS.md) don't documentFunction::from_callback()or its trade-offs. (src/rust/jsg/v8.rs:2468)
View full review
src/rust/jsg/v8.rs:2468adds the publicFunction::from_callback()with a thorough doc comment.- The README's "Functions from Rust closures" section (
src/rust/jsg/README.md:968-998) covers onlyFunction::new(). src/rust/AGENTS.md:17also lists onlyFunction::new.- A short note in both on when to use
from_callback()would help: it carries no state, has no signature, and gets no Lock or panic catching with a rawextern "C"callback.
- The README's "Functions from Rust closures" section (
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found.
⚡ Performance: ✅ LGTM
The change cuts per-call cost as intended. local_function_call no longer allocates a LocalVector for each call, and from_callback() skips the Wrappable lookup on each call. I found no regressions.
Reviewed commit: 7f1ec287
Two changes to Rust JSG that the timing loop of workerd bench needs to cost as little per call as its C++ version. v8::Function::from_callback() creates a function from a raw RawFunctionCallback with no data, like C++ v8::Function::New() with a plain callback. A function from Function::new() reaches its data through a Wrappable on every call, so a call costs more than the callback itself does. The bench runner measures its own per-call overhead by timing an empty function, and that function must cost no more than C++'s did, or the runner subtracts too much from every case. The callback can be an extern "C" function that V8 calls directly, or one that jsg::function_callback() makes from a closure that captures nothing. The function has no data and no signature, and can be called with new; a raw extern "C" callback also gets no lock and no panic handling, which the doc comment spells out. local_function_call() copied the arguments into a v8::LocalVector, a heap-allocated vector, on every call with arguments. The Rust Local is repr(transparent) over ffi::Local, which ffi-inl.h asserts has the layout of a v8::Local, so the arguments now pass to v8::Function::Call() as they are. Every Local<Function>::call() with arguments benefits. A new test checks that a call passes every argument, in order, and only those.
7f1ec28 to
37f5688
Compare
There was a problem hiding this comment.
Superseded by the review of
1fe4a49c.
This review, of 37f5688c
💬 Approved with comments — I found no correctness, security or performance problems in the no-copy call path or in Function::from_callback(), and the new README section covers the API and its trade-offs. One INFO note remains: the KJ_REQUIRE in a shim that isn't declared Result, which no real call can reach.
Findings: 1 info
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 info
No correctness problems. One INFO note is still open: the KJ_REQUIRE guard sits in a shim that isn't declared Result.
Recommendation: 💬 Optional Nits
- [INFO]
KJ_REQUIREinlocal_function_callthrows from a shim that isn't declaredResult, so an oversized slice panics instead of being rejected. (src/rust/jsg/ffi.c++:652)
View full review
🔄 Since last review: 1 finding resolved, 1 preserved, 0 new
src/rust/jsg/ffi.c++:652:KJ_REQUIRE(args.size() <= INT_MAX, ...)throws a KJ exception, butlocal_function_callis declared insrc/rust/jsg/v8.rs:604as returning a plainMaybeLocal, notResult<MaybeLocal>.- An oversized slice therefore panics instead of being rejected.
- Only a slice of more than 2^31
Locals can reach this guard, so no real call does.
I checked these and found nothing wrong:
- Passing the slice without a copy, including an empty slice, which becomes
nullptrwith a count of 0. from_callback()and its error path.- The new tests.
- The new README section.
✅ All other checks passed
🔒 Security: ✅ LGTM
The no-copy argument pass depends on the layout assertion in ffi-inl.h, and the static_cast<int> is safe once the length check has passed. The doc comment on Function::from_callback() states the caller's responsibilities for a raw extern "C" callback.
📖 AGENTS.md: ✅ LGTM
No issues found.
⚡ Performance: ✅ LGTM
The change removes the per-call LocalVector allocation, and with from_callback() a call no longer goes through a Wrappable to reach its data. I found no regressions.
📝 Documentation: ✅ LGTM
The new README section documents Function::from_callback() and its trade-offs.
Reviewed commit: 37f5688c
There was a problem hiding this comment.
Superseded by the review of
6fcc7181.
This review, of 1fe4a49c
💬 Approved with comments — I found no correctness, security or performance problems in the no-copy call path or in Function::from_callback(), and the new README section covers the API and its trade-offs. One INFO note remains: the KJ_REQUIRE in a shim that isn't declared Result, which no real call can reach.
Findings: 1 info
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 info
No correctness problems. One INFO note is still open: the KJ_REQUIRE guard sits in a shim that isn't declared Result.
Recommendation: 💬 Optional Nits
- [INFO]
KJ_REQUIREinlocal_function_callthrows from a shim that isn't declaredResult, so an oversized slice panics instead of being rejected. (src/rust/jsg/ffi.c++:652)
View full review
🔄 Since last review: 1 finding resolved, 1 preserved, 0 new
src/rust/jsg/ffi.c++:652:KJ_REQUIRE(args.size() <= INT_MAX, ...)throws a KJ exception, butlocal_function_callis declared insrc/rust/jsg/v8.rs:604as returning a plainMaybeLocal, notResult<MaybeLocal>.- An oversized slice therefore panics instead of being rejected.
- Only a slice of more than 2^31
Locals can reach this guard, so no real call does.
I checked these and found nothing wrong:
- Passing the slice without a copy, including an empty slice, which becomes
nullptrwith a count of 0. from_callback()and its error path.- The new tests.
- The new README section.
✅ All other checks passed
🔒 Security: ✅ LGTM
The no-copy argument pass depends on the layout assertion in ffi-inl.h, and the static_cast<int> is safe once the length check has passed. The doc comment on Function::from_callback() states the caller's responsibilities for a raw extern "C" callback.
📖 AGENTS.md: ✅ LGTM
No issues found.
⚡ Performance: ✅ LGTM
The change removes the per-call LocalVector allocation, and with from_callback() a call no longer goes through a Wrappable to reach its data. I found no regressions.
📝 Documentation: ✅ LGTM
The new README section documents Function::from_callback() and its trade-offs.
Reviewed commit: 1fe4a49c — same changes as 37f5688c, so this is that commit's review (no new review was run; line numbers are as of 37f5688c)
There was a problem hiding this comment.
✅ Approved — Nothing in the changed code differs from the last reviewed commit. The one earlier note (the KJ_REQUIRE that no real call can reach) was answered by the author, and the code supports the answer. No new issues.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No issues found. The doc comment on Function::from_callback() states what the caller must take care of when using a raw extern "C" callback.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
The no-copy argument pass in src/rust/jsg/ffi.c++ is correct:
- The layout assertion in
ffi-inl.hguarantees thatLocalandv8::Localmatch. - An empty slice maps to
nullptrwith a count of 0. - The
INT_MAXcheck guards the cast toint.
Function::from_callback() reuses ffi::function_new with undefined as the data. It maps a null result to Error::pending(), the same way Function::new does.
The tests cover:
- both kinds of callback;
- the function's shape;
- a panic becoming an internal error;
- the pending-exception path;
- argument order.
The earlier KJ_REQUIRE note is closed: the author explained that an oversized slice would be a coding error, and only a slice of more than 2^31 elements can reach the check.
⚡ Performance: ✅ LGTM
Calls with arguments no longer allocate a LocalVector each time. A function created by from_callback() no longer goes through a Wrappable to reach its data on each call. No regressions.
📝 Documentation: ✅ LGTM
The README section documents Function::from_callback() and its trade-offs.
Reviewed commit: 6fcc7181
This makes two changes to Rust JSG that cut the per-call cost of
creating and calling JavaScript functions from Rust. They matter most
to code that calls into JavaScript in a hot loop and needs the cost of
each call to match C++, such as a benchmark harness.
workerd bench'stiming loop is one: it subtracts its own measured per-call overhead
from every case, so a call path costlier than the C++ one would bias
every result rather than just slow the tool down.
v8::Function::from_callback()creates a function from a rawRawFunctionCallbackwith no data, like C++v8::Function::New()witha plain callback. A function created by
Function::new()reaches itsdata through a Wrappable on every call, so each call costs more than
V8's call of the callback itself. The callback can be an
extern "C"function that V8 calls directly, or one that
jsg::function_callback()builds from a closure that captures nothing. The trade-off against
Function::new()is that the function carries no state, has nosignature, and can be called with
new. A rawextern "C"callbackalso gets no
Lockand no panic catching, so a panic in it aborts theprocess;
function_callback()provides both, and the doc commentspells all of this out. New jsg-tests cover both kinds of callback,
the function's shape (name,
length,new, anythis), and a panicin a closure callback becoming an internal error; the existing
pending-exception test now also covers
from_callback().local_function_call(), the shim behindLocal<Function>::call(),used to copy the arguments into a
v8::LocalVector, a heap-allocatedvector, on every call with arguments. The Rust
Localisrepr(transparent)overffi::Local, andffi-inl.hasserts thatffi::Localhas the same size and alignment asv8::Local, so thearguments now pass to
v8::Function::Call()as they are. V8 takes themas non-const but, as checked against its source, only reads them. Every
Local<Function>::call()with arguments benefits. A new test checksthat a call passes every argument, in order, and only those, for zero
arguments, four, and a subslice. The shim also now rejects more
arguments than V8's
intcount can hold rather than truncating it.In a
--config=benchmarkbuild on Linux, the bench runner's measuredoverhead for an empty synchronous call went from 140-255 ns down to
76-119 ns; the C++ runner measured 50-95 ns. That case passes no
arguments, so the gain comes from
from_callback(); the argumentchange applies to the overhead case that passes one argument and to
cases with setup or a receiver. The remaining gap is calls across the
Rust/C++ boundary that aren't inlined.