0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-12 16:59:32 -05:00

perf(serde_v8): avoid SerializablePkg allocs (#13860)

For common return types such as String/ZeroCopyBuf/ByteString
This commit is contained in:
Aaron O'Mullan 2022-03-07 13:06:50 +01:00 committed by GitHub
parent 0b9da1aa7a
commit 4e1da28b39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,9 @@
use std::any::TypeId; use std::any::TypeId;
use std::mem::transmute_copy; use std::mem::transmute_copy;
use crate::Buffer;
use crate::ByteString;
/// Serializable exists to allow boxing values as "objects" to be serialized later, /// 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 /// 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 /// replacement for erased-serde that makes less allocations, since it's specific to serde_v8
@ -44,7 +47,6 @@ impl SerializablePkg {
/// Primitive serves as a lightweight serializable wrapper around primitives /// Primitive serves as a lightweight serializable wrapper around primitives
/// so that we can use them for async values /// so that we can use them for async values
#[derive(Clone, Copy)]
pub enum Primitive { pub enum Primitive {
Unit, Unit,
Bool(bool), Bool(bool),
@ -58,6 +60,9 @@ pub enum Primitive {
UInt64(u64), UInt64(u64),
Float32(f32), Float32(f32),
Float64(f64), Float64(f64),
String(String),
Buffer(Buffer),
ByteString(ByteString),
} }
impl serde::Serialize for Primitive { impl serde::Serialize for Primitive {
@ -65,7 +70,7 @@ impl serde::Serialize for Primitive {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
match *self { match self {
Self::Unit => ().serialize(s), Self::Unit => ().serialize(s),
Self::Bool(x) => x.serialize(s), Self::Bool(x) => x.serialize(s),
Self::Int8(x) => x.serialize(s), Self::Int8(x) => x.serialize(s),
@ -78,6 +83,9 @@ impl serde::Serialize for Primitive {
Self::UInt64(x) => x.serialize(s), Self::UInt64(x) => x.serialize(s),
Self::Float32(x) => x.serialize(s), Self::Float32(x) => x.serialize(s),
Self::Float64(x) => x.serialize(s), Self::Float64(x) => x.serialize(s),
Self::String(x) => x.serialize(s),
Self::Buffer(x) => x.serialize(s),
Self::ByteString(x) => x.serialize(s),
} }
} }
} }
@ -86,7 +94,9 @@ impl<T: serde::Serialize + 'static> From<T> for SerializablePkg {
fn from(x: T) -> Self { fn from(x: T) -> Self {
#[inline(always)] #[inline(always)]
fn tc<T, U>(src: T) -> U { fn tc<T, U>(src: T) -> U {
unsafe { transmute_copy(&src) } let x = unsafe { transmute_copy(&src) };
std::mem::forget(src);
x
} }
let tid = TypeId::of::<T>(); let tid = TypeId::of::<T>();
@ -114,6 +124,12 @@ impl<T: serde::Serialize + 'static> From<T> for SerializablePkg {
Self::Primitive(Primitive::Float32(tc(x))) Self::Primitive(Primitive::Float32(tc(x)))
} else if tid == TypeId::of::<f64>() { } else if tid == TypeId::of::<f64>() {
Self::Primitive(Primitive::Float64(tc(x))) Self::Primitive(Primitive::Float64(tc(x)))
} else if tid == TypeId::of::<String>() {
Self::Primitive(Primitive::String(tc(x)))
} else if tid == TypeId::of::<Buffer>() {
Self::Primitive(Primitive::Buffer(tc(x)))
} else if tid == TypeId::of::<ByteString>() {
Self::Primitive(Primitive::ByteString(tc(x)))
} else { } else {
Self::Serializable(Box::new(x)) Self::Serializable(Box::new(x))
} }