From e70a1c23128fb6550742fdfd22af5905ca7890a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 6 Jun 2023 16:02:16 +0200 Subject: [PATCH] refactor(serde_v8): don't access backing store twice (#19382) I did that change recently, did spot that we're calling `get_backing_store()` in succession and that API call is not cheap. --- serde_v8/magic/v8slice.rs | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/serde_v8/magic/v8slice.rs b/serde_v8/magic/v8slice.rs index b1dd897703..2b103f1c96 100644 --- a/serde_v8/magic/v8slice.rs +++ b/serde_v8/magic/v8slice.rs @@ -31,20 +31,6 @@ pub struct V8Slice { unsafe impl Send for V8Slice {} impl V8Slice { - pub fn from_buffer( - buffer: v8::Local, - range: Range, - ) -> Result { - let store = buffer.get_backing_store(); - if store.is_shared() { - return Err(v8::DataError::BadType { - actual: "shared ArrayBufferView", - expected: "non-shared ArrayBufferView", - }); - } - Ok(Self { store, range }) - } - fn as_slice(&self) -> &[u8] { // SAFETY: v8::SharedRef is similar to Arc<[u8]>, // it points to a fixed continuous slice of bytes on the heap. @@ -92,12 +78,15 @@ impl FromV8 for V8Slice { value: v8::Local, ) -> Result { match to_ranged_buffer(scope, value) { - Ok((b, r)) => { - if b.get_backing_store().is_resizable_by_user_javascript() { - return Err(crate::Error::ResizableBackingStoreNotSupported); + Ok((b, range)) => { + let store = b.get_backing_store(); + if store.is_resizable_by_user_javascript() { + Err(crate::Error::ResizableBackingStoreNotSupported) + } else if store.is_shared() { + Err(crate::Error::ExpectedBuffer(value_to_type_str(value))) + } else { + Ok(V8Slice { store, range }) } - Self::from_buffer(b, r) - .map_err(|_| crate::Error::ExpectedBuffer(value_to_type_str(value))) } Err(_) => Err(crate::Error::ExpectedBuffer(value_to_type_str(value))), }