diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-09-07 16:21:47 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-07 16:21:47 +0530 |
commit | 027d4d433dce32a3b715184b54e7fe6403dedec2 (patch) | |
tree | dfd70be0537dd8defce5ef14eacfa57be222746d /ext | |
parent | d57f9d560d7b675bda3cf3ba0ac69201b73467b3 (diff) |
perf(ops): inline &[u8] arguments and enable fast API (#15731)
Diffstat (limited to 'ext')
-rw-r--r-- | ext/ffi/lib.rs | 3 | ||||
-rw-r--r-- | ext/web/02_timers.js | 10 | ||||
-rw-r--r-- | ext/web/compression.rs | 14 | ||||
-rw-r--r-- | ext/web/lib.rs | 20 | ||||
-rw-r--r-- | ext/web/timers.rs | 32 |
5 files changed, 29 insertions, 50 deletions
diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index b93638c88..bc3a58209 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -18,7 +18,6 @@ use deno_core::Extension; use deno_core::OpState; use deno_core::Resource; use deno_core::ResourceId; -use deno_core::ZeroCopyBuf; use dlopen::raw::Library; use libffi::middle::Arg; use libffi::middle::Cif; @@ -2154,7 +2153,7 @@ where fn op_ffi_buf_copy_into<FP>( state: &mut deno_core::OpState, src: usize, - mut dst: ZeroCopyBuf, + dst: &mut [u8], len: usize, ) -> Result<(), AnyError> where diff --git a/ext/web/02_timers.js b/ext/web/02_timers.js index 6cbc706e6..5d7ee49e0 100644 --- a/ext/web/02_timers.js +++ b/ext/web/02_timers.js @@ -13,6 +13,7 @@ MapPrototypeGet, MapPrototypeHas, MapPrototypeSet, + Uint8Array, Uint32Array, // deno-lint-ignore camelcase NumberPOSITIVE_INFINITY, @@ -27,13 +28,10 @@ const { reportException } = window.__bootstrap.event; const { assert } = window.__bootstrap.infra; - let hr; + const hrU8 = new Uint8Array(8); + const hr = new Uint32Array(hrU8.buffer); function opNow() { - if (!hr) { - hr = new Uint32Array(2); - ops.op_now_set_buf(hr); - } - ops.op_now.fast(); + ops.op_now.fast(hrU8); return (hr[0] * 1000 + hr[1] / 1e6); } diff --git a/ext/web/compression.rs b/ext/web/compression.rs index f3610e2ea..d2647e498 100644 --- a/ext/web/compression.rs +++ b/ext/web/compression.rs @@ -68,38 +68,38 @@ pub fn op_compression_new( pub fn op_compression_write( state: &mut OpState, rid: ResourceId, - input: ZeroCopyBuf, + input: &[u8], ) -> Result<ZeroCopyBuf, AnyError> { let resource = state.resource_table.get::<CompressionResource>(rid)?; let mut inner = resource.0.borrow_mut(); let out: Vec<u8> = match &mut *inner { Inner::DeflateDecoder(d) => { - d.write_all(&input)?; + d.write_all(input)?; d.flush()?; d.get_mut().drain(..) } Inner::DeflateEncoder(d) => { - d.write_all(&input)?; + d.write_all(input)?; d.flush()?; d.get_mut().drain(..) } Inner::DeflateRawDecoder(d) => { - d.write_all(&input)?; + d.write_all(input)?; d.flush()?; d.get_mut().drain(..) } Inner::DeflateRawEncoder(d) => { - d.write_all(&input)?; + d.write_all(input)?; d.flush()?; d.get_mut().drain(..) } Inner::GzDecoder(d) => { - d.write_all(&input)?; + d.write_all(input)?; d.flush()?; d.get_mut().drain(..) } Inner::GzEncoder(d) => { - d.write_all(&input)?; + d.write_all(input)?; d.flush()?; d.get_mut().drain(..) } diff --git a/ext/web/lib.rs b/ext/web/lib.rs index e71ed6d14..9c1e85952 100644 --- a/ext/web/lib.rs +++ b/ext/web/lib.rs @@ -50,7 +50,6 @@ pub use crate::message_port::JsMessageData; pub use crate::message_port::MessagePort; use crate::timers::op_now; -use crate::timers::op_now_set_buf; use crate::timers::op_sleep; use crate::timers::op_timer_handle; use crate::timers::StartTime; @@ -106,7 +105,6 @@ pub fn init<P: TimersPermission + 'static>( compression::op_compression_new::decl(), compression::op_compression_write::decl(), compression::op_compression_finish::decl(), - op_now_set_buf::decl(), op_now::decl::<P>(), op_timer_handle::decl(), op_cancel_handle::decl(), @@ -149,8 +147,8 @@ fn forgiving_base64_decode(input: &mut [u8]) -> Result<usize, AnyError> { } #[op] -fn op_base64_encode(s: ZeroCopyBuf) -> String { - forgiving_base64_encode(s.as_ref()) +fn op_base64_encode(s: &[u8]) -> String { + forgiving_base64_encode(s) } #[op] @@ -179,7 +177,7 @@ fn op_encoding_normalize_label(label: String) -> Result<String, AnyError> { #[op] fn op_encoding_decode_single( - data: ZeroCopyBuf, + data: &[u8], label: String, fatal: bool, ignore_bom: bool, @@ -205,7 +203,7 @@ fn op_encoding_decode_single( if fatal { let (result, _, written) = - decoder.decode_to_utf16_without_replacement(&data, &mut output, true); + decoder.decode_to_utf16_without_replacement(data, &mut output, true); match result { DecoderResult::InputEmpty => { output.truncate(written); @@ -220,7 +218,7 @@ fn op_encoding_decode_single( } } else { let (result, _, written, _) = - decoder.decode_to_utf16(&data, &mut output, true); + decoder.decode_to_utf16(data, &mut output, true); match result { CoderResult::InputEmpty => { output.truncate(written); @@ -262,7 +260,7 @@ fn op_encoding_new_decoder( #[op] fn op_encoding_decode( state: &mut OpState, - data: ZeroCopyBuf, + data: &[u8], rid: ResourceId, stream: bool, ) -> Result<U16String, AnyError> { @@ -279,7 +277,7 @@ fn op_encoding_decode( if fatal { let (result, _, written) = - decoder.decode_to_utf16_without_replacement(&data, &mut output, !stream); + decoder.decode_to_utf16_without_replacement(data, &mut output, !stream); match result { DecoderResult::InputEmpty => { output.truncate(written); @@ -294,7 +292,7 @@ fn op_encoding_decode( } } else { let (result, _, written, _) = - decoder.decode_to_utf16(&data, &mut output, !stream); + decoder.decode_to_utf16(data, &mut output, !stream); match result { CoderResult::InputEmpty => { output.truncate(written); @@ -326,7 +324,7 @@ struct EncodeIntoResult { #[op] fn op_encoding_encode_into( input: String, - mut buffer: ZeroCopyBuf, + buffer: &mut [u8], ) -> EncodeIntoResult { // Since `input` is already UTF-8, we can simply find the last UTF-8 code // point boundary from input that fits in `buffer`, and copy the bytes up to diff --git a/ext/web/timers.rs b/ext/web/timers.rs index f6b2cc9e7..ba5e12d62 100644 --- a/ext/web/timers.rs +++ b/ext/web/timers.rs @@ -4,7 +4,6 @@ use deno_core::error::AnyError; use deno_core::op; -use deno_core::ZeroCopyBuf; use deno_core::CancelFuture; use deno_core::CancelHandle; @@ -24,24 +23,12 @@ pub trait TimersPermission { pub type StartTime = Instant; -static mut NOW_BUF: *mut u32 = std::ptr::null_mut(); - -#[op] -pub fn op_now_set_buf(buf: ZeroCopyBuf) { - assert_eq!(buf.len(), 8); - // SAFETY: This is safe because this is the only place where we initialize - // NOW_BUF. - unsafe { - NOW_BUF = buf.as_ptr() as *mut u32; - } -} - // Returns a milliseconds and nanoseconds subsec // since the start time of the deno runtime. // If the High precision flag is not set, the // nanoseconds are rounded on 2ms. #[op(fast)] -pub fn op_now<TP>(state: &mut OpState) +pub fn op_now<TP>(state: &mut OpState, buf: &mut [u8]) where TP: TimersPermission + 'static, { @@ -57,17 +44,14 @@ where let reduced_time_precision = 2_000_000; // 2ms in nanoseconds subsec_nanos -= subsec_nanos % reduced_time_precision; } - - // SAFETY: This is safe because we initialize NOW_BUF in op_now_set_buf, its a null pointer - // otherwise. - // op_now_set_buf guarantees that the buffer is 8 bytes long. - unsafe { - if !NOW_BUF.is_null() { - let buf = std::slice::from_raw_parts_mut(NOW_BUF, 2); - buf[0] = seconds as u32; - buf[1] = subsec_nanos as u32; - } + if buf.len() < 8 { + return; } + let buf: &mut [u32] = + // SAFETY: buffer is at least 8 bytes long. + unsafe { std::slice::from_raw_parts_mut(buf.as_mut_ptr() as _, 2) }; + buf[0] = seconds as u32; + buf[1] = subsec_nanos as u32; } pub struct TimerHandle(Rc<CancelHandle>); |