1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-09 07:39:15 -05:00

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::fmt::Debug;
use std::ops::Deref; use std::ops::Deref;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::sync::Mutex;
use super::transl8::FromV8; use super::transl8::FromV8;
use super::transl8::ToV8; use super::transl8::ToV8;
@ -14,7 +13,7 @@ use crate::magic::transl8::impl_magic;
// allowing us to use a single type for familiarity // allowing us to use a single type for familiarity
pub enum ZeroCopyBuf { pub enum ZeroCopyBuf {
FromV8(V8Slice), FromV8(V8Slice),
ToV8(Mutex<Option<Box<[u8]>>>), ToV8(Option<Box<[u8]>>),
// Variant of the ZeroCopyBuf than is never exposed to the JS. // Variant of the ZeroCopyBuf than is never exposed to the JS.
// Generally used to pass Vec<u8> backed buffers to resource methods. // Generally used to pass Vec<u8> backed buffers to resource methods.
Temp(Vec<u8>), Temp(Vec<u8>),
@ -30,7 +29,7 @@ impl Debug for ZeroCopyBuf {
impl ZeroCopyBuf { impl ZeroCopyBuf {
pub fn empty() -> Self { 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 { pub fn new_temp(vec: Vec<u8>) -> Self {
@ -91,7 +90,7 @@ impl DerefMut for ZeroCopyBuf {
impl From<Box<[u8]>> for ZeroCopyBuf { impl From<Box<[u8]>> for ZeroCopyBuf {
fn from(buf: Box<[u8]>) -> Self { 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() value.into()
} }
Self::Temp(_) => unreachable!(), Self::Temp(_) => unreachable!(),
Self::ToV8(x) => { Self::ToV8(ref mut x) => x.take().expect("ZeroCopyBuf was empty"),
x.get_mut().unwrap().take().expect("ZeroCopyBuf was empty")
}
}; };
if buf.is_empty() { if buf.is_empty() {
@ -151,13 +148,9 @@ impl From<ZeroCopyBuf> for bytes::Bytes {
fn from(zbuf: ZeroCopyBuf) -> bytes::Bytes { fn from(zbuf: ZeroCopyBuf) -> bytes::Bytes {
match zbuf { match zbuf {
ZeroCopyBuf::FromV8(v) => v.into(), ZeroCopyBuf::FromV8(v) => v.into(),
// WARNING(AaronO): potential footgun, but will disappear in future ZeroCopyBuf refactor ZeroCopyBuf::ToV8(mut v) => {
ZeroCopyBuf::ToV8(v) => v v.take().expect("ZeroCopyBuf was empty").into()
.lock() }
.unwrap()
.take()
.expect("ZeroCopyBuf was empty")
.into(),
ZeroCopyBuf::Temp(v) => v.into(), ZeroCopyBuf::Temp(v) => v.into(),
} }
} }