From 0628d041748d69a865c38460bfaefbaa85ce1494 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 20 May 2022 12:02:29 -0500 Subject: [PATCH] perf(derive): Reuse allocations for external subcommands In case the `ArgMatches` is cloned, this forces a constraint that the underlying type impls `Clone` but that should be safe as we only support `String` and `OsString`. --- clap_derive/src/derives/subcommand.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clap_derive/src/derives/subcommand.rs b/clap_derive/src/derives/subcommand.rs index f91a821d..7edc2eb0 100644 --- a/clap_derive/src/derives/subcommand.rs +++ b/clap_derive/src/derives/subcommand.rs @@ -527,9 +527,10 @@ fn gen_from_arg_matches( ::std::iter::once(#str_ty::from(#subcommand_name_var)) .chain( #sub_arg_matches_var - .get_many::<#str_ty>("") + .remove_many::<#str_ty>("") .expect("unexpected type") .into_iter().flatten() // `""` isn't present, bug in `unstable-v4` + .map(|f| ::std::sync::Arc::try_unwrap(f).unwrap_or_else(|arc| (*arc).clone())) .map(#str_ty::from) ) .collect::<::std::vec::Vec<_>>()