From 6703ccb0fc3aca7ba4bdb9083f199fe0c1b66bd9 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 7 May 2026 20:53:04 +0800 Subject: Add dispatch tree feature for fast argument matching --- mingling_core/src/program/exec.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'mingling_core/src/program') diff --git a/mingling_core/src/program/exec.rs b/mingling_core/src/program/exec.rs index 469f6d2..9cb2892 100644 --- a/mingling_core/src/program/exec.rs +++ b/mingling_core/src/program/exec.rs @@ -9,11 +9,18 @@ use crate::{ pub mod error; #[cfg(feature = "async")] -pub async fn exec(program: &Program) -> Result +pub async fn exec( + program: &'static Program, +) -> Result where C: ProgramCollect, { - let mut current = dispatch_args_dynamic(program, program.args.clone())?; + #[cfg(not(feature = "dispatch_tree"))] + let mut current = dispatch_args_dynamic(program, &program.args)?; + + #[cfg(feature = "dispatch_tree")] + let mut current = C::dispatch_args_trie(&program.args)?; + let mut stop_next = false; // If the program has Help enabled, skip actual logic and jump to Help @@ -54,11 +61,16 @@ where } #[cfg(not(feature = "async"))] -pub fn exec(program: &Program) -> Result +pub fn exec(program: &'static Program) -> Result where C: ProgramCollect, { - let mut current = dispatch_args_dynamic(program, program.args.clone())?; + #[cfg(not(feature = "dispatch_tree"))] + let mut current = dispatch_args_dynamic(program, &program.args)?; + + #[cfg(feature = "dispatch_tree")] + let mut current = C::dispatch_args_trie(&program.args)?; + let mut stop_next = false; // If the program has Help enabled, skip actual logic and jump to Help @@ -100,8 +112,8 @@ where /// Dynamically dispatch input arguments to registered entry types pub(crate) fn dispatch_args_dynamic( - program: &Program, - args: Vec, + program: &'static Program, + args: &Vec, ) -> Result, ProgramInternalExecuteError> where C: ProgramCollect, @@ -126,9 +138,9 @@ where /// Match user input against registered dispatchers and return the matched dispatcher and remaining arguments. #[allow(clippy::type_complexity)] pub(crate) fn match_user_input( - program: &Program, - args: Vec, -) -> Result<(&(dyn Dispatcher + Send + Sync), Vec), ProgramInternalExecuteError> + program: &'static Program, + args: &Vec, +) -> Result<(&'static (dyn Dispatcher + Send + Sync), Vec), ProgramInternalExecuteError> where C: ProgramCollect, { -- cgit