diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-04-13 14:32:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-13 14:32:47 +0200 |
commit | 702284dc2268eae565778b8b522ba534d7d48580 (patch) | |
tree | 9fbd5ee7f0b31643363399ce25df830fb62e1be8 /core/runtime.rs | |
parent | e39bc959c160e2af947034bd183b010a747e6b88 (diff) |
perf(ops): directly respond for eager ops (#18683)
This commit changes "eager ops" to directly return a response value
instead of calling "opresponse" callback in JavaScript. This saves
one boundary crossing and has a fantastic impact on the "async_ops.js"
benchmark:
```
v1.32.4
$ deno run cli/bench/async_ops.js
time 329 ms rate 3039513
time 322 ms rate 3105590
time 307 ms rate 3257328
time 301 ms rate 3322259
time 303 ms rate 3300330
time 306 ms rate 3267973
time 300 ms rate 3333333
time 301 ms rate 3322259
time 301 ms rate 3322259
time 301 ms rate 3322259
time 302 ms rate 3311258
time 301 ms rate 3322259
time 302 ms rate 3311258
time 302 ms rate 3311258
time 303 ms rate 3300330
```
```
this branch
$ ./target/release/deno run -A cli/bench/async_ops.js
time 257 ms rate 3891050
time 248 ms rate 4032258
time 251 ms rate 3984063
time 246 ms rate 4065040
time 238 ms rate 4201680
time 227 ms rate 4405286
time 228 ms rate 4385964
time 229 ms rate 4366812
time 228 ms rate 4385964
time 226 ms rate 4424778
time 226 ms rate 4424778
time 227 ms rate 4405286
time 228 ms rate 4385964
time 227 ms rate 4405286
time 228 ms rate 4385964
time 227 ms rate 4405286
time 229 ms rate 4366812
time 228 ms rate 4385964
```
Prerequisite for https://github.com/denoland/deno/pull/18652
Diffstat (limited to 'core/runtime.rs')
-rw-r--r-- | core/runtime.rs | 38 |
1 files changed, 11 insertions, 27 deletions
diff --git a/core/runtime.rs b/core/runtime.rs index ef65d2192..b03f3f7d0 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -2437,12 +2437,12 @@ pub fn queue_fast_async_op( } #[inline] -pub fn queue_async_op( +pub fn queue_async_op<'s>( ctx: &OpCtx, - scope: &mut v8::HandleScope, + scope: &'s mut v8::HandleScope, deferred: bool, op: impl Future<Output = (RealmIdx, PromiseId, OpId, OpResult)> + 'static, -) { +) -> Option<v8::Local<'s, v8::Value>> { let runtime_state = match ctx.runtime_state.upgrade() { Some(rc_state) => rc_state, // atleast 1 Rc is held by the JsRuntime. @@ -2459,31 +2459,13 @@ pub fn queue_async_op( ); match OpCall::eager(op) { - // This calls promise.resolve() before the control goes back to userland JS. It works something - // along the lines of: - // - // function opresolve(promiseId, ...) { - // getPromise(promiseId).resolve(...); - // } - // const p = setPromise(); - // op.op_async(promiseId, ...); // Calls `opresolve` - // return p; - EagerPollResult::Ready((_, promise_id, op_id, mut resp)) if !deferred => { - let context_state_rc = JsRealm::state_from_scope(scope); - let context_state = context_state_rc.borrow(); - - let args = &[ - v8::Integer::new(scope, promise_id).into(), - resp.to_v8(scope).unwrap(), - ]; - + // If the result is ready we'll just return it straight to the caller, so + // we don't have to invoke a JS callback to respond. // This works under the + // assumption that `()` return value is serialized as `null`. + EagerPollResult::Ready((_, _, op_id, mut resp)) if !deferred => { + let resp = resp.to_v8(scope).unwrap(); ctx.state.borrow_mut().tracker.track_async_completed(op_id); - - let tc_scope = &mut v8::TryCatch::new(scope); - let js_recv_cb = - context_state.js_recv_cb.as_ref().unwrap().open(tc_scope); - let this = v8::undefined(tc_scope).into(); - js_recv_cb.call(tc_scope, this, args); + return Some(resp); } EagerPollResult::Ready(op) => { let ready = OpCall::ready(op); @@ -2497,6 +2479,8 @@ pub fn queue_async_op( state.have_unpolled_ops = true; } } + + None } #[cfg(test)] |