From 8e38caadccb6f5d85693e2cf60aa754e5c11e23c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 22 Jul 2022 20:22:02 -0500 Subject: [PATCH] perf: Remove some monomorphization bloat When checking into binary size, I noticed that the `git` example is a lot larger than v3. `git bisect` narrowed it down to 11076a5c7033b25c34e3e3008f02b9c5d4efa3d3 which doesn't make sense. I did noticed we could remove some bloat from monomorphization. Overall for `cargo-example, we've dropped about 47 KiB. --- src/builder/command.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/builder/command.rs b/src/builder/command.rs index ac5b5469edf..549cd4e4213 100644 --- a/src/builder/command.rs +++ b/src/builder/command.rs @@ -171,8 +171,12 @@ impl<'help> Command<'help> { /// ``` /// [argument]: Arg #[must_use] - pub fn arg>>(mut self, a: A) -> Self { - let mut arg = a.into(); + pub fn arg>>(self, a: A) -> Self { + let arg = a.into(); + self.arg_internal(arg) + } + + fn arg_internal(mut self, mut arg: Arg<'help>) -> Self { if let Some(current_disp_ord) = self.current_disp_ord.as_mut() { if !arg.is_positional() && arg.provider != ArgProvider::Generated { let current = *current_disp_ord; @@ -401,8 +405,12 @@ impl<'help> Command<'help> { /// ``` #[inline] #[must_use] - pub fn subcommand>(mut self, subcmd: S) -> Self { - let mut subcmd = subcmd.into(); + pub fn subcommand>(self, subcmd: S) -> Self { + let subcmd = subcmd.into(); + self.subcommand_internal(subcmd) + } + + fn subcommand_internal(mut self, mut subcmd: Self) -> Self { if let Some(current_disp_ord) = self.current_disp_ord.as_mut() { let current = *current_disp_ord; subcmd.disp_ord.get_or_insert(current);