Skip to content

Commit

Permalink
perf(serde_v8): remove Mutex from ZeroCopyBuf (denoland#15888)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Sep 13, 2022
1 parent b4e6183 commit 51ba476
Showing 1 changed file with 7 additions and 14 deletions.
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

0 comments on commit 51ba476

Please sign in to comment.