Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(serde_v8): remove Mutex from ZeroCopyBuf #15888

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions serde_v8/magic/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::fmt::Debug;
use std::ops::Deref;
use std::ops::DerefMut;
use std::sync::Mutex;

use super::transl8::FromV8;
use super::transl8::ToV8;
Expand All @@ -14,7 +13,7 @@ use crate::magic::transl8::impl_magic;
// allowing us to use a single type for familiarity
pub enum ZeroCopyBuf {
FromV8(V8Slice),
ToV8(Mutex<Option<Box<[u8]>>>),
ToV8(Option<Box<[u8]>>),
// Variant of the ZeroCopyBuf than is never exposed to the JS.
// Generally used to pass Vec<u8> backed buffers to resource methods.
Temp(Vec<u8>),
Expand All @@ -30,7 +29,7 @@ impl Debug for ZeroCopyBuf {

impl ZeroCopyBuf {
pub fn empty() -> Self {
ZeroCopyBuf::ToV8(Mutex::new(Some(vec![0_u8; 0].into_boxed_slice())))
ZeroCopyBuf::ToV8(Some(vec![0_u8; 0].into_boxed_slice()))
}

pub fn new_temp(vec: Vec<u8>) -> Self {
Expand Down Expand Up @@ -91,7 +90,7 @@ impl DerefMut for ZeroCopyBuf {

impl From<Box<[u8]>> for ZeroCopyBuf {
fn from(buf: Box<[u8]>) -> Self {
ZeroCopyBuf::ToV8(Mutex::new(Some(buf)))
ZeroCopyBuf::ToV8(Some(buf))
}
}

Expand All @@ -112,9 +111,7 @@ impl ToV8 for ZeroCopyBuf {
value.into()
}
Self::Temp(_) => unreachable!(),
Self::ToV8(x) => {
x.get_mut().unwrap().take().expect("ZeroCopyBuf was empty")
}
Self::ToV8(ref mut x) => x.take().expect("ZeroCopyBuf was empty"),
};

if buf.is_empty() {
Expand Down Expand Up @@ -151,13 +148,9 @@ impl From<ZeroCopyBuf> for bytes::Bytes {
fn from(zbuf: ZeroCopyBuf) -> bytes::Bytes {
match zbuf {
ZeroCopyBuf::FromV8(v) => v.into(),
// WARNING(AaronO): potential footgun, but will disappear in future ZeroCopyBuf refactor
ZeroCopyBuf::ToV8(v) => v
.lock()
.unwrap()
.take()
.expect("ZeroCopyBuf was empty")
.into(),
ZeroCopyBuf::ToV8(mut v) => {
v.take().expect("ZeroCopyBuf was empty").into()
}
ZeroCopyBuf::Temp(v) => v.into(),
}
}
Expand Down