diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-08-30 14:31:14 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-30 14:31:14 +0530 |
commit | 448654764f9c403b2e0d93f6d8dd4044733977ce (patch) | |
tree | 0fa47f15f6fa974f4f962e05a09801d73230eebf /ops/lib.rs | |
parent | 5e0fa5dd887ab05efef16ecc54491bd657410377 (diff) |
perf(ops): inline String args (#15681)
Diffstat (limited to 'ops/lib.rs')
-rw-r--r-- | ops/lib.rs | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/ops/lib.rs b/ops/lib.rs index 500e1f5f9..0b0689356 100644 --- a/ops/lib.rs +++ b/ops/lib.rs @@ -592,14 +592,34 @@ fn codegen_arg( idx: usize, ) -> TokenStream2 { let ident = quote::format_ident!("{name}"); - let pat = match arg { - syn::FnArg::Typed(pat) => &pat.pat, + let (pat, ty) = match arg { + syn::FnArg::Typed(pat) => (&pat.pat, &pat.ty), _ => unreachable!(), }; // Fast path if arg should be skipped if matches!(**pat, syn::Pat::Wild(_)) { return quote! { let #ident = (); }; } + // Fast path for `String` + if is_string(&**ty) { + return quote! { + let #ident = match #core::v8::Local::<#core::v8::String>::try_from(args.get(#idx as i32)) { + Ok(v8_string) => #core::serde_v8::to_utf8(v8_string, scope), + Err(_) => { + return #core::_ops::throw_type_error(scope, format!("Expected string at position {}", #idx)); + } + }; + }; + } + // Fast path for `Option<String>` + if is_option_string(&**ty) { + return quote! { + let #ident = match #core::v8::Local::<#core::v8::String>::try_from(args.get(#idx as i32)) { + Ok(v8_string) => Some(#core::serde_v8::to_utf8(v8_string, scope)), + Err(_) => None + }; + }; + } // Otherwise deserialize it via serde_v8 quote! { let #ident = args.get(#idx as i32); @@ -680,6 +700,14 @@ fn is_result(ty: impl ToTokens) -> bool { } } +fn is_string(ty: impl ToTokens) -> bool { + tokens(ty) == "String" +} + +fn is_option_string(ty: impl ToTokens) -> bool { + tokens(ty) == "Option < String >" +} + /// 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) |