From 38f0b41e7d16db24c1ba7c7cc7b536f4d7e169e9 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Nov 2022 06:37:18 -0800 Subject: perf(web): optimize single pass utf8 decoding (#16593) - [x] Avoid copying buffers. https://encoding.spec.whatwg.org/#dom-textdecoder-decode > Implementations are strongly encouraged to use an implementation strategy that avoids this copy. When doing so they will have to make sure that changes to input do not affect future calls to [decode()](https://encoding.spec.whatwg.org/#dom-textdecoder-decode). - [x] Special op to avoid string label deserialization and parsing. (Ideally we should map labels to integers in JS) - [x] Avoid webidl `Object.assign` when options is undefined. --- ops/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'ops') diff --git a/ops/lib.rs b/ops/lib.rs index 298327af2..d295ec9bd 100644 --- a/ops/lib.rs +++ b/ops/lib.rs @@ -449,13 +449,16 @@ fn codegen_u8_slice(core: &TokenStream2, idx: usize) -> TokenStream2 { let value = args.get(#idx as i32); match #core::v8::Local::<#core::v8::ArrayBuffer>::try_from(value) { Ok(b) => { + // Handles detached buffers. + let byte_length = b.byte_length(); let store = b.data() as *mut u8; // SAFETY: rust guarantees that lifetime of slice is no longer than the call. - unsafe { ::std::slice::from_raw_parts_mut(store, b.byte_length()) } + unsafe { ::std::slice::from_raw_parts_mut(store, byte_length) } }, Err(_) => { if let Ok(view) = #core::v8::Local::<#core::v8::ArrayBufferView>::try_from(value) { - let (offset, len) = (view.byte_offset(), view.byte_length()); + let len = view.byte_length(); + let offset = view.byte_offset(); let buffer = match view.buffer(scope) { Some(v) => v, None => { -- cgit v1.2.3