1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00

perf(serde_v8): introduce Serializable boxable object (#9983)

This commit is contained in:
Aaron O'Mullan 2021-04-04 01:17:02 +02:00 committed by GitHub
parent 878599ca7c
commit 19d0e6b671
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 16 deletions

10
Cargo.lock generated
View file

@ -565,7 +565,6 @@ version = "0.83.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bencher", "bencher",
"erased-serde",
"futures", "futures",
"indexmap", "indexmap",
"lazy_static", "lazy_static",
@ -897,15 +896,6 @@ dependencies = [
"termcolor", "termcolor",
] ]
[[package]]
name = "erased-serde"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0465971a8cc1fa2455c8465aaa377131e1f1cf4983280f474a13e68793aa770c"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.1.8" version = "0.1.8"

View file

@ -14,7 +14,6 @@ path = "lib.rs"
[dependencies] [dependencies]
anyhow = "1.0.38" anyhow = "1.0.38"
erased-serde = "0.3.13"
futures = "0.3.12" futures = "0.3.12"
indexmap = "1.6.1" indexmap = "1.6.1"
lazy_static = "1.4.0" lazy_static = "1.4.0"

View file

@ -438,7 +438,7 @@ fn send<'s>(
match op { match op {
Op::Sync(resp) => match resp { Op::Sync(resp) => match resp {
OpResponse::Value(v) => { OpResponse::Value(v) => {
rv.set(to_v8(scope, v).unwrap()); rv.set(v.to_v8(scope).unwrap());
} }
OpResponse::Buffer(buf) => { OpResponse::Buffer(buf) => {
rv.set(boxed_slice_to_uint8array(scope, buf).into()); rv.set(boxed_slice_to_uint8array(scope, buf).into());

View file

@ -65,7 +65,6 @@ pub use crate::ops::OpResponse;
pub use crate::ops::OpState; pub use crate::ops::OpState;
pub use crate::ops::OpTable; pub use crate::ops::OpTable;
pub use crate::ops::PromiseId; pub use crate::ops::PromiseId;
pub use crate::ops::Serializable;
pub use crate::ops_bin::bin_op_async; pub use crate::ops_bin::bin_op_async;
pub use crate::ops_bin::bin_op_sync; pub use crate::ops_bin::bin_op_sync;
pub use crate::ops_bin::ValueOrVector; pub use crate::ops_bin::ValueOrVector;

View file

@ -22,7 +22,6 @@ use std::ops::DerefMut;
use std::pin::Pin; use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
pub use erased_serde::Serialize as Serializable;
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 = OpResponse>>>;
pub type OpFn = pub type OpFn =
@ -60,7 +59,7 @@ impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> {
} }
pub enum OpResponse { pub enum OpResponse {
Value(Box<dyn Serializable>), Value(Box<dyn serde_v8::Serializable>),
Buffer(Box<[u8]>), Buffer(Box<[u8]>),
} }

View file

@ -1419,7 +1419,7 @@ impl JsRuntime {
let (promise_id, resp) = overflown_response; let (promise_id, resp) = overflown_response;
args.push(v8::Integer::new(scope, promise_id as i32).into()); args.push(v8::Integer::new(scope, promise_id as i32).into());
args.push(match resp { args.push(match resp {
OpResponse::Value(value) => serde_v8::to_v8(scope, value).unwrap(), OpResponse::Value(value) => value.to_v8(scope).unwrap(),
OpResponse::Buffer(buf) => { OpResponse::Buffer(buf) => {
bindings::boxed_slice_to_uint8array(scope, buf).into() bindings::boxed_slice_to_uint8array(scope, buf).into()
} }

View file

@ -5,6 +5,7 @@ mod keys;
mod magic; mod magic;
mod payload; mod payload;
mod ser; mod ser;
mod serializable;
pub mod utils; pub mod utils;
pub use de::{from_v8, from_v8_cached, Deserializer}; pub use de::{from_v8, from_v8_cached, Deserializer};
@ -12,3 +13,4 @@ pub use error::{Error, Result};
pub use keys::KeyCache; pub use keys::KeyCache;
pub use magic::Value; pub use magic::Value;
pub use ser::{to_v8, Serializer}; pub use ser::{to_v8, Serializer};
pub use serializable::Serializable;

View file

@ -0,0 +1,22 @@
use rusty_v8 as v8;
/// Serializable exists to allow boxing values as "objects" to be serialized later,
/// this is particularly useful for async op-responses. This trait is a more efficient
/// replacement for erased-serde that makes less allocations, since it's specific to serde_v8
/// (and thus doesn't have to have generic outputs, etc...)
pub trait Serializable {
fn to_v8<'a>(
&self,
scope: &mut v8::HandleScope<'a>,
) -> Result<v8::Local<'a, v8::Value>, crate::Error>;
}
/// Allows all implementors of `serde::Serialize` to implement Serializable
impl<T: serde::Serialize> Serializable for T {
fn to_v8<'a>(
&self,
scope: &mut v8::HandleScope<'a>,
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
crate::to_v8(scope, self)
}
}