From 4451fa857bf4e9f021c8c63d3944774e8c9b337f Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 6 Mar 2023 08:58:04 +0530 Subject: perf(ext/node): improve createHash performance (#18033) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` > deno run -A ../test.mjs cpu: unknown runtime: deno 1.31.1 (aarch64-apple-darwin) benchmark time (avg) (min … max) p75 p99 p995 ------------------------------------------------- ----------------------------- 2.22 µs/iter (2.2 µs … 2.28 µs) 2.22 µs 2.28 µs 2.28 µs > target/release/deno run -A test.mjs cpu: unknown runtime: deno 1.31.1 (aarch64-apple-darwin) benchmark time (avg) (min … max) p75 p99 p995 ------------------------------------------------- ----------------------------- 864.9 ns/iter (825.05 ns … 1.22 µs) 864.93 ns 1.22 µs 1.22 µs ``` --- ext/node/crypto/mod.rs | 55 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 14 deletions(-) (limited to 'ext/node/crypto/mod.rs') diff --git a/ext/node/crypto/mod.rs b/ext/node/crypto/mod.rs index 597779328..f9a61b456 100644 --- a/ext/node/crypto/mod.rs +++ b/ext/node/crypto/mod.rs @@ -17,23 +17,38 @@ use rsa::RsaPublicKey; mod digest; -#[op] -pub fn op_node_create_hash( - state: &mut OpState, - algorithm: String, -) -> Result { - Ok(state.resource_table.add(digest::Context::new(&algorithm)?)) +#[op(fast)] +pub fn op_node_create_hash(state: &mut OpState, algorithm: &str) -> u32 { + state + .resource_table + .add(match digest::Context::new(algorithm) { + Ok(context) => context, + Err(_) => return 0, + }) } -#[op] -pub fn op_node_hash_update( - state: &mut OpState, - rid: ResourceId, - data: &[u8], -) -> Result<(), AnyError> { - let context = state.resource_table.get::(rid)?; +#[op(fast)] +pub fn op_node_hash_update(state: &mut OpState, rid: u32, data: &[u8]) -> bool { + let context = match state.resource_table.get::(rid) { + Ok(context) => context, + _ => return false, + }; context.update(data); - Ok(()) + true +} + +#[op(fast)] +pub fn op_node_hash_update_str( + state: &mut OpState, + rid: u32, + data: &str, +) -> bool { + let context = match state.resource_table.get::(rid) { + Ok(context) => context, + _ => return false, + }; + context.update(data.as_bytes()); + true } #[op] @@ -47,6 +62,18 @@ pub fn op_node_hash_digest( Ok(context.digest()?.into()) } +#[op] +pub fn op_node_hash_digest_hex( + state: &mut OpState, + rid: ResourceId, +) -> Result { + let context = state.resource_table.take::(rid)?; + let context = Rc::try_unwrap(context) + .map_err(|_| type_error("Hash context is already in use"))?; + let digest = context.digest()?; + Ok(hex::encode(digest)) +} + #[op] pub fn op_node_hash_clone( state: &mut OpState, -- cgit v1.2.3