tlopex commented on code in PR #685:
URL: https://github.com/apache/tvm-ffi/pull/685#discussion_r3653427907
##########
rust/tvm-ffi-macros/src/match_any.rs:
##########
@@ -105,41 +110,225 @@ pub fn expand(input: proc_macro::TokenStream) ->
proc_macro::TokenStream {
fn expand_match_any(input: MatchAnyInput) -> TokenStream {
let tvm_ffi = get_tvm_ffi_crate();
+ let scrutinee = input.scrutinee;
+ let fallback = input.fallback;
+ let arms = input.arms;
+ let can_attempt_leaf_lookup = arms.len() >= MIN_LOOKUP_TABLE_ARMS
+ && arms
+ .iter()
+ .all(|arm| arm.guard.is_none() && is_simple_binding(&arm.binding));
+
+ if can_attempt_leaf_lookup {
+ expand_leaf_lookup_match(&tvm_ffi, &scrutinee, &arms, &fallback)
+ } else {
+ expand_ordered_match(&tvm_ffi, &scrutinee, &arms, &fallback)
+ }
+}
+
+fn expand_ordered_match(
+ tvm_ffi: &TokenStream,
+ scrutinee: &Expr,
+ arms: &[TypedArm],
+ fallback: &Expr,
+) -> TokenStream {
let span = Span::mixed_site();
let source = Ident::new("__tvm_ffi_match_any_source", span);
let converted = Ident::new("__tvm_ffi_match_any_converted", span);
let view = Ident::new("__tvm_ffi_match_any_view", span);
let rejected = Ident::new("__tvm_ffi_match_any_rejected", span);
- let scrutinee = input.scrutinee;
- let fallback = input.fallback;
- let dispatch_fallback = fallback.clone();
- let arms = input.arms;
- let dispatch = arms
- .into_iter()
- .rev()
- .fold(quote!({ #dispatch_fallback }), |next, arm| {
- let matcher = arm.matcher;
- let binding = arm.binding;
- let body = arm.body;
- let matched = if let Some(guard) = arm.guard {
- quote!(::core::result::Result::Ok(#binding) if #guard)
- } else {
- quote!(::core::result::Result::Ok(#binding))
+ let dispatch = expand_ordered_dispatch(arms, fallback, &view, &rejected);
+
+ quote! {
+ {
+ let #source = &(#scrutinee);
+ let #converted: ::core::result::Result<
+ #tvm_ffi::AnyView<'_>,
+ ::core::convert::Infallible,
+ > =
::core::convert::TryInto::<#tvm_ffi::AnyView<'_>>::try_into(#source);
+ let #view = match #converted {
+ ::core::result::Result::Ok(view) => view,
+ ::core::result::Result::Err(error) => match error {},
};
+ if #view.type_index()
+ >= #tvm_ffi::TypeIndex::kTVMFFIStaticObjectBegin as i32
+ {
+ #dispatch
+ } else {
+ #fallback
+ }
+ }
+ }
+}
+
+fn expand_ordered_dispatch(
+ arms: &[TypedArm],
+ fallback: &Expr,
+ view: &Ident,
+ rejected: &Ident,
+) -> TokenStream {
+ arms.iter().rev().fold(quote!({ #fallback }), |next, arm| {
+ let matcher = &arm.matcher;
+ let binding = &arm.binding;
+ let body = &arm.body;
+ let matched = if let Some(guard) = &arm.guard {
+ quote!(::core::result::Result::Ok(#binding) if #guard)
+ } else {
+ quote!(::core::result::Result::Ok(#binding))
+ };
+
+ quote! {
+ match ::core::convert::TryInto::<#matcher>::try_into(#view) {
+ #matched => { #body },
+ #rejected => {
+ ::core::mem::drop(#rejected);
+ #next
+ }
+ }
+ }
+ })
+}
+
+fn expand_leaf_lookup_match(
Review Comment:
updated
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]