Skip to content

Commit

Permalink
perf: dynamically dispatch context function in WebIDLConverter (#1035)
Browse files Browse the repository at this point in the history
Avoids re-instantiating the converters with different generic arguments,
saving on binary size.
  • Loading branch information
nathanwhit authored Jan 8, 2025
1 parent 6e4d533 commit 2741f28
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 211 deletions.
349 changes: 180 additions & 169 deletions core/webidl.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ops/op2/dispatch_slow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ pub fn from_arg(
&mut #scope,
#arg_ident,
#prefix.into(),
|| std::borrow::Cow::Borrowed(#context),
deno_core::webidl::ContextFn::new_borrowed(&|| std::borrow::Cow::Borrowed(#context)),
&#options,
) {
Ok(t) => t,
Expand Down
8 changes: 6 additions & 2 deletions ops/op2/test_cases/sync/webidl.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ops/webidl/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn get_body(
quote! {
return Err(::deno_core::webidl::WebIdlError::new(
__prefix,
&__context,
__context.borrowed(),
::deno_core::webidl::WebIdlErrorKind::DictionaryCannotConvertKey {
converter: #ident_string,
key: #string_name,
Expand All @@ -139,7 +139,7 @@ pub fn get_body(
} else {
let __key = #v8_static_name
.v8_string(__scope)
.map_err(|e| ::deno_core::webidl::WebIdlError::other(__prefix.clone(), &__context, e))?;
.map_err(|e| ::deno_core::webidl::WebIdlError::other(__prefix.clone(), __context.borrowed(), e))?;
__eternal.set(__scope, __key);
Ok(__key)
}
Expand All @@ -151,7 +151,7 @@ pub fn get_body(
__scope,
__value,
__prefix.clone(),
|| format!("{} ({})", #new_context, __context()).into(),
::deno_core::webidl::ContextFn::new_borrowed(&|| format!("{} ({})", #new_context, __context.call()).into()),
&#options,
)?
} else {
Expand All @@ -178,7 +178,7 @@ pub fn get_body(
} else {
return Err(::deno_core::webidl::WebIdlError::new(
__prefix,
&__context,
__context.borrowed(),
::deno_core::webidl::WebIdlErrorKind::ConvertToConverterType("dictionary")
));
}
Expand Down
4 changes: 2 additions & 2 deletions ops/webidl/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ pub fn get_body(
let Ok(str) = __value.try_cast::<::deno_core::v8::String>() else {
return Err(::deno_core::webidl::WebIdlError::new(
__prefix,
&__context,
__context,
::deno_core::webidl::WebIdlErrorKind::ConvertToConverterType("enum"),
));
};

match str.to_rust_string_lossy(__scope).as_str() {
#(#variants),*,
s => Err(::deno_core::webidl::WebIdlError::new(__prefix, &__context, ::deno_core::webidl::WebIdlErrorKind::InvalidEnumVariant { converter: #ident_string, variant: s.to_string() }))
s => Err(::deno_core::webidl::WebIdlError::new(__prefix, __context, ::deno_core::webidl::WebIdlErrorKind::InvalidEnumVariant { converter: #ident_string, variant: s.to_string() }))
}
})
}
Expand Down
6 changes: 2 additions & 4 deletions ops/webidl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,13 @@ fn create_impl(ident: Ident, body: TokenStream) -> TokenStream {
impl<'a> ::deno_core::webidl::WebIdlConverter<'a> for #ident {
type Options = ();

fn convert<C>(
fn convert<'b>(
__scope: &mut ::deno_core::v8::HandleScope<'a>,
__value: ::deno_core::v8::Local<'a, ::deno_core::v8::Value>,
__prefix: std::borrow::Cow<'static, str>,
__context: C,
__context: ::deno_core::webidl::ContextFn<'b>,
__options: &Self::Options,
) -> Result<Self, ::deno_core::webidl::WebIdlError>
where
C: Fn() -> std::borrow::Cow<'static, str>,
{
#body
}
Expand Down
49 changes: 28 additions & 21 deletions ops/webidl/test_cases/dict.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 5 additions & 8 deletions ops/webidl/test_cases/enum.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2741f28

Please sign in to comment.