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 = [
"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"

View file

@ -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"

View file

@ -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());

View file

@ -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;

View file

@ -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<Box<dyn Future<Output = OpResponse>>>;
pub type OpFn =
@ -60,7 +59,7 @@ impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> {
}
pub enum OpResponse {
Value(Box<dyn Serializable>),
Value(Box<dyn serde_v8::Serializable>),
Buffer(Box<[u8]>),
}

View file

@ -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()
}

View file

@ -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;

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)
}
}