mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
perf(ops): fast path for SMI return values (#15033)
This commit is contained in:
parent
95d2f206fc
commit
4e7abf4986
1 changed files with 30 additions and 0 deletions
30
ops/lib.rs
30
ops/lib.rs
|
@ -359,9 +359,19 @@ fn codegen_sync_ret(
|
||||||
return quote! {};
|
return quote! {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if is_u32_rv(output) {
|
||||||
|
return quote! {
|
||||||
|
rv.set_uint32(result as u32);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Optimize Result<(), Err> to skip serde_v8 when Ok(...)
|
// Optimize Result<(), Err> to skip serde_v8 when Ok(...)
|
||||||
let ok_block = if is_unit_result(output) {
|
let ok_block = if is_unit_result(output) {
|
||||||
quote! {}
|
quote! {}
|
||||||
|
} else if is_u32_rv_result(output) {
|
||||||
|
quote! {
|
||||||
|
rv.set_uint32(result as u32);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
match #core::serde_v8::to_v8(scope, result) {
|
match #core::serde_v8::to_v8(scope, result) {
|
||||||
|
@ -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>
|
/// Detects if a type is of the form Result<(), Err>
|
||||||
fn is_unit_result(ty: impl ToTokens) -> bool {
|
fn is_unit_result(ty: impl ToTokens) -> bool {
|
||||||
is_result(&ty) && tokens(&ty).contains("Result < ()")
|
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 {
|
fn is_mut_ref_opstate(arg: &syn::FnArg) -> bool {
|
||||||
static RE: Lazy<Regex> =
|
static RE: Lazy<Regex> =
|
||||||
Lazy::new(|| Regex::new(r#": & mut (?:deno_core :: )?OpState$"#).unwrap());
|
Lazy::new(|| Regex::new(r#": & mut (?:deno_core :: )?OpState$"#).unwrap());
|
||||||
|
|
Loading…
Reference in a new issue