Skip to content

Commit

Permalink
perf(ops): fast path for SMI return values (denoland#15033)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Jul 1, 2022
1 parent 95d2f20 commit 4e7abf4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ops/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,19 @@ fn codegen_sync_ret(
return quote! {};
}

if is_u32_rv(output) {
return quote! {
rv.set_uint32(result as u32);
};
}

// Optimize Result<(), Err> to skip serde_v8 when Ok(...)
let ok_block = if is_unit_result(output) {
quote! {}
} else if is_u32_rv_result(output) {
quote! {
rv.set_uint32(result as u32);
}
} else {
quote! {
match #core::serde_v8::to_v8(scope, result) {
Expand Down Expand Up @@ -408,11 +418,31 @@ fn is_result(ty: impl ToTokens) -> bool {
}
}

/// Detects if the type can be set using `rv.set_uint32` fast path
fn is_u32_rv(ty: impl ToTokens) -> bool {
["u32", "u8", "u16"].iter().any(|&s| tokens(&ty) == s) || is_resource_id(&ty)
}

/// Detects if the type is of the format Result<u32/u8/u16, Err>
fn is_u32_rv_result(ty: impl ToTokens) -> bool {
is_result(&ty)
&& (tokens(&ty).contains("Result < u32")
|| tokens(&ty).contains("Result < u8")
|| tokens(&ty).contains("Result < u16")
|| is_resource_id(&ty))
}

/// Detects if a type is of the form Result<(), Err>
fn is_unit_result(ty: impl ToTokens) -> bool {
is_result(&ty) && tokens(&ty).contains("Result < ()")
}

fn is_resource_id(arg: impl ToTokens) -> bool {
static RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r#": (?:deno_core :: )?ResourceId$"#).unwrap());
RE.is_match(&tokens(arg))
}

fn is_mut_ref_opstate(arg: &syn::FnArg) -> bool {
static RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r#": & mut (?:deno_core :: )?OpState$"#).unwrap());
Expand Down

0 comments on commit 4e7abf4

Please sign in to comment.