tlopex commented on code in PR #685:
URL: https://github.com/apache/tvm-ffi/pull/685#discussion_r3653519673


##########
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(
+    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 probe = Ident::new("__tvm_ffi_match_any_probe", span);
+    let pattern_list_id = 
Ident::new("__tvm_ffi_match_any_leaf_pattern_list_id", span);
+    let type_indices = Ident::new("__tvm_ffi_match_any_type_indices", span);
+    let static_table = Ident::new("__TVM_FFI_MATCH_ANY_LEAF_TABLE", span);
+    let table = Ident::new("__tvm_ffi_match_any_leaf_table", span);
+    let arm_id = Ident::new("__tvm_ffi_match_any_arm_id", span);
+    let selected = Ident::new("__tvm_ffi_match_any_selected", span);
+    let selected_value = Ident::new("__tvm_ffi_match_any_selected_value", 
span);
+    let selected_enum = Ident::new("__TvmFfiMatchAnyArm", span);
+    let fallback_variant = Ident::new("Fallback", span);
+    let arm_count = arms.len();
+    let matchers = arms.iter().map(|arm| &arm.matcher).collect::<Vec<_>>();
+    let arm_types = (0..arm_count)
+        .map(|arm_id| Ident::new(&format!("__TvmFfiMatchAnyType{arm_id}"), 
span))
+        .collect::<Vec<_>>();
+    let arm_variants = (0..arm_count)
+        .map(|arm_id| Ident::new(&format!("Arm{arm_id}"), span))
+        .collect::<Vec<_>>();
+    let arm_constants = (0..arm_count)
+        .map(|arm_id| Ident::new(&format!("__TVM_FFI_MATCH_ANY_ARM_{arm_id}"), 
span))
+        .collect::<Vec<_>>();
+    let arm_constant_definitions =
+        arm_constants
+            .iter()
+            .enumerate()
+            .map(|(arm_id, arm_constant)| {
+                quote! {
+                    const #arm_constant: #tvm_ffi::match_any::ArmId =
+                        #arm_id as #tvm_ffi::match_any::ArmId;
+                }
+            });
+    let lookup_entries = arm_constants
+        .iter()
+        .enumerate()
+        .map(|(arm_id, arm_constant)| quote!((#type_indices[#arm_id], 
#arm_constant)));
+    let pattern_list = matchers
+        .iter()
+        .rev()
+        .fold(quote!(()), |tail, matcher| quote!((#matcher, #tail)));
+
+    let lookup_arm_id = quote! {
+        {
+            use #tvm_ffi::match_any::LeafPatternMetadata as _;
+
+            let #probe =
+                #tvm_ffi::match_any::LeafPatternProbe::<#pattern_list>::new();
+            match (&#probe).leaf_pattern_list_id() {
+                ::core::option::Option::Some(#pattern_list_id) => {
+                    static #static_table: ::std::sync::OnceLock<

Review Comment:
   the case is not frequent so we just keep the current implenment



-- 
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]

Reply via email to