perf(serde_v8): remove Mutex from ZeroCopyBuf (#15888)

This commit is contained in:
Divy Srivastava 2022-09-13 20:56:59 +05:30 committed by GitHub
parent b4e618315a
commit 51ba4764d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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