0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

core: avoid async op future reboxing to bundle PromiseId (#10123)

This commit is contained in:
Aaron O'Mullan 2021-04-11 07:05:43 +02:00 committed by GitHub
parent 8aa0d5f96e
commit 29eca72fea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 16 deletions

View file

@ -9,7 +9,6 @@ use crate::OpResponse;
use crate::OpTable; use crate::OpTable;
use crate::PromiseId; use crate::PromiseId;
use crate::ZeroCopyBuf; use crate::ZeroCopyBuf;
use futures::future::FutureExt;
use rusty_v8 as v8; use rusty_v8 as v8;
use serde::Serialize; use serde::Serialize;
use serde_v8::to_v8; use serde_v8::to_v8;
@ -433,7 +432,7 @@ fn send<'s>(
} }
}; };
let payload = OpPayload::new(scope, v); let payload = OpPayload::new(scope, v, promise_id);
let op = OpTable::route_op(op_id, state.op_state.clone(), payload, buf); let op = OpTable::route_op(op_id, state.op_state.clone(), payload, buf);
match op { match op {
Op::Sync(resp) => match resp { Op::Sync(resp) => match resp {
@ -445,13 +444,11 @@ fn send<'s>(
} }
}, },
Op::Async(fut) => { Op::Async(fut) => {
let fut2 = fut.map(move |resp| (promise_id, resp)); state.pending_ops.push(fut);
state.pending_ops.push(fut2.boxed_local());
state.have_unpolled_ops = true; state.have_unpolled_ops = true;
} }
Op::AsyncUnref(fut) => { Op::AsyncUnref(fut) => {
let fut2 = fut.map(move |resp| (promise_id, resp)); state.pending_unref_ops.push(fut);
state.pending_unref_ops.push(fut2.boxed_local());
state.have_unpolled_ops = true; state.have_unpolled_ops = true;
} }
Op::NotFound => { Op::NotFound => {

View file

@ -21,7 +21,7 @@ use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
pub type PromiseId = u64; pub type PromiseId = u64;
pub type OpAsyncFuture = Pin<Box<dyn Future<Output = OpResponse>>>; pub type OpAsyncFuture = Pin<Box<dyn Future<Output = (PromiseId, OpResponse)>>>;
pub type OpFn = pub type OpFn =
dyn Fn(Rc<RefCell<OpState>>, OpPayload, Option<ZeroCopyBuf>) -> Op + 'static; dyn Fn(Rc<RefCell<OpState>>, OpPayload, Option<ZeroCopyBuf>) -> Op + 'static;
pub type OpId = usize; pub type OpId = usize;
@ -29,16 +29,19 @@ pub type OpId = usize;
pub struct OpPayload<'a, 'b, 'c> { pub struct OpPayload<'a, 'b, 'c> {
pub(crate) scope: Option<&'a mut v8::HandleScope<'b>>, pub(crate) scope: Option<&'a mut v8::HandleScope<'b>>,
pub(crate) value: Option<v8::Local<'c, v8::Value>>, pub(crate) value: Option<v8::Local<'c, v8::Value>>,
pub(crate) promise_id: PromiseId,
} }
impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> { impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> {
pub fn new( pub fn new(
scope: &'a mut v8::HandleScope<'b>, scope: &'a mut v8::HandleScope<'b>,
value: v8::Local<'c, v8::Value>, value: v8::Local<'c, v8::Value>,
promise_id: PromiseId,
) -> Self { ) -> Self {
Self { Self {
scope: Some(scope), scope: Some(scope),
value: Some(value), value: Some(value),
promise_id,
} }
} }
@ -46,6 +49,7 @@ impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> {
Self { Self {
scope: None, scope: None,
value: None, value: None,
promise_id: 0,
} }
} }

View file

@ -132,11 +132,11 @@ where
p: OpPayload, p: OpPayload,
b: Option<ZeroCopyBuf>| b: Option<ZeroCopyBuf>|
-> Op { -> Op {
let pid = p.promise_id;
let min_arg: u32 = p.deserialize().unwrap(); let min_arg: u32 = p.deserialize().unwrap();
let fut = op_fn(state.clone(), min_arg, b) let fut = op_fn(state.clone(), min_arg, b)
.map(move |result| serialize_bin_result(result, state)); .map(move |result| (pid, serialize_bin_result(result, state)));
let temp = Box::pin(fut); Op::Async(Box::pin(fut))
Op::Async(temp)
}, },
) )
} }

View file

@ -85,12 +85,13 @@ where
p: OpPayload, p: OpPayload,
buf: Option<ZeroCopyBuf>| buf: Option<ZeroCopyBuf>|
-> Result<Op, AnyError> { -> Result<Op, AnyError> {
let pid = p.promise_id;
// Parse args // Parse args
let args = p.deserialize()?; let args = p.deserialize()?;
use crate::futures::FutureExt; use crate::futures::FutureExt;
let fut = op_fn(state.clone(), args, buf) let fut = op_fn(state.clone(), args, buf)
.map(move |result| serialize_op_result(result, state)); .map(move |result| (pid, serialize_op_result(result, state)));
Ok(Op::Async(Box::pin(fut))) Ok(Op::Async(Box::pin(fut)))
}; };

View file

@ -1506,7 +1506,7 @@ pub mod tests {
Mode::Async => { Mode::Async => {
let control: u8 = payload.deserialize().unwrap(); let control: u8 = payload.deserialize().unwrap();
assert_eq!(control, 42); assert_eq!(control, 42);
let resp = OpResponse::Value(Box::new(43)); let resp = (0, OpResponse::Value(Box::new(43)));
Op::Async(Box::pin(futures::future::ready(resp))) Op::Async(Box::pin(futures::future::ready(resp)))
} }
Mode::AsyncUnref => { Mode::AsyncUnref => {
@ -1515,7 +1515,7 @@ pub mod tests {
let fut = async { let fut = async {
// This future never finish. // This future never finish.
futures::future::pending::<()>().await; futures::future::pending::<()>().await;
OpResponse::Value(Box::new(43)) (0, OpResponse::Value(Box::new(43)))
}; };
Op::AsyncUnref(Box::pin(fut)) Op::AsyncUnref(Box::pin(fut))
} }
@ -1526,7 +1526,7 @@ pub mod tests {
} }
let resp = OpResponse::Value(Box::new(43)); let resp = OpResponse::Value(Box::new(43));
Op::Async(Box::pin(futures::future::ready(resp))) Op::Async(Box::pin(futures::future::ready((0, resp))))
} }
} }
} }
@ -1970,7 +1970,7 @@ pub mod tests {
dispatch_count_.fetch_add(1, Ordering::Relaxed); dispatch_count_.fetch_add(1, Ordering::Relaxed);
let control: u8 = payload.deserialize().unwrap(); let control: u8 = payload.deserialize().unwrap();
assert_eq!(control, 42); assert_eq!(control, 42);
let resp = OpResponse::Value(Box::new(43)); let resp = (0, OpResponse::Value(Box::new(43)));
Op::Async(Box::pin(futures::future::ready(resp))) Op::Async(Box::pin(futures::future::ready(resp)))
}; };

View file

@ -48,7 +48,7 @@ fn op_test_async(
assert!(rx.await.is_ok()); assert!(rx.await.is_ok());
let result = b"test"; let result = b"test";
let result_box: Box<[u8]> = Box::new(*result); let result_box: Box<[u8]> = Box::new(*result);
OpResponse::Buffer(result_box) (0, OpResponse::Buffer(result_box))
}; };
Op::Async(fut.boxed()) Op::Async(fut.boxed())