From 19d0e6b6710a61e6fdcf9f08e6057e49b349fe18 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Sun, 4 Apr 2021 01:17:02 +0200 Subject: [PATCH] perf(serde_v8): introduce Serializable boxable object (#9983) --- Cargo.lock | 10 ---------- core/Cargo.toml | 1 - core/bindings.rs | 2 +- core/lib.rs | 1 - core/ops.rs | 3 +-- core/runtime.rs | 2 +- serde_v8/src/lib.rs | 2 ++ serde_v8/src/serializable.rs | 22 ++++++++++++++++++++++ 8 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 serde_v8/src/serializable.rs diff --git a/Cargo.lock b/Cargo.lock index fc1d4ca844..1af9d8e755 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -565,7 +565,6 @@ version = "0.83.0" dependencies = [ "anyhow", "bencher", - "erased-serde", "futures", "indexmap", "lazy_static", @@ -897,15 +896,6 @@ dependencies = [ "termcolor", ] -[[package]] -name = "erased-serde" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0465971a8cc1fa2455c8465aaa377131e1f1cf4983280f474a13e68793aa770c" -dependencies = [ - "serde", -] - [[package]] name = "errno" version = "0.1.8" diff --git a/core/Cargo.toml b/core/Cargo.toml index 1e23f788ed..a06e1e43fc 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -14,7 +14,6 @@ path = "lib.rs" [dependencies] anyhow = "1.0.38" -erased-serde = "0.3.13" futures = "0.3.12" indexmap = "1.6.1" lazy_static = "1.4.0" diff --git a/core/bindings.rs b/core/bindings.rs index 3484d3cbdc..f086e1f9c6 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -438,7 +438,7 @@ fn send<'s>( match op { Op::Sync(resp) => match resp { OpResponse::Value(v) => { - rv.set(to_v8(scope, v).unwrap()); + rv.set(v.to_v8(scope).unwrap()); } OpResponse::Buffer(buf) => { rv.set(boxed_slice_to_uint8array(scope, buf).into()); diff --git a/core/lib.rs b/core/lib.rs index 3ee08689eb..f514110834 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -65,7 +65,6 @@ pub use crate::ops::OpResponse; pub use crate::ops::OpState; pub use crate::ops::OpTable; 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_sync; pub use crate::ops_bin::ValueOrVector; diff --git a/core/ops.rs b/core/ops.rs index 5bd0928b6e..1a2a2ebc77 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -22,7 +22,6 @@ use std::ops::DerefMut; use std::pin::Pin; use std::rc::Rc; -pub use erased_serde::Serialize as Serializable; pub type PromiseId = u64; pub type OpAsyncFuture = Pin>>; pub type OpFn = @@ -60,7 +59,7 @@ impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> { } pub enum OpResponse { - Value(Box), + Value(Box), Buffer(Box<[u8]>), } diff --git a/core/runtime.rs b/core/runtime.rs index a64a71449a..f358cf05eb 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1419,7 +1419,7 @@ impl JsRuntime { let (promise_id, resp) = overflown_response; args.push(v8::Integer::new(scope, promise_id as i32).into()); 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) => { bindings::boxed_slice_to_uint8array(scope, buf).into() } diff --git a/serde_v8/src/lib.rs b/serde_v8/src/lib.rs index 9413791bbd..bd4b00b320 100644 --- a/serde_v8/src/lib.rs +++ b/serde_v8/src/lib.rs @@ -5,6 +5,7 @@ mod keys; mod magic; mod payload; mod ser; +mod serializable; pub mod utils; pub use de::{from_v8, from_v8_cached, Deserializer}; @@ -12,3 +13,4 @@ pub use error::{Error, Result}; pub use keys::KeyCache; pub use magic::Value; pub use ser::{to_v8, Serializer}; +pub use serializable::Serializable; diff --git a/serde_v8/src/serializable.rs b/serde_v8/src/serializable.rs new file mode 100644 index 0000000000..2e781340b2 --- /dev/null +++ b/serde_v8/src/serializable.rs @@ -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, crate::Error>; +} + +/// Allows all implementors of `serde::Serialize` to implement Serializable +impl Serializable for T { + fn to_v8<'a>( + &self, + scope: &mut v8::HandleScope<'a>, + ) -> Result, crate::Error> { + crate::to_v8(scope, self) + } +}