From 34416a2637176496d7810a6fd59dfbf7fe612796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 5 Oct 2023 23:48:03 +0300 Subject: [PATCH] Preallocate some slices and maps with known size --- args.go | 2 +- command.go | 2 +- flag_groups.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/args.go b/args.go index b7674c88..ed1e70ce 100644 --- a/args.go +++ b/args.go @@ -52,7 +52,7 @@ func OnlyValidArgs(cmd *Command, args []string) error { if len(cmd.ValidArgs) > 0 { // Remove any description that may be included in ValidArgs. // A description is following a tab character. - var validArgs []string + validArgs := make([]string, 0, len(cmd.ValidArgs)) for _, v := range cmd.ValidArgs { validArgs = append(validArgs, strings.SplitN(v, "\t", 2)[0]) } diff --git a/command.go b/command.go index 6866f7d0..1764a217 100644 --- a/command.go +++ b/command.go @@ -701,7 +701,7 @@ Loop: // This is not a flag or a flag value. Check to see if it matches what we're looking for, and if so, // return the args, excluding the one at this position. if s == x { - ret := []string{} + ret := make([]string, 0, len(args)-1) ret = append(ret, args[:pos]...) ret = append(ret, args[pos+1:]...) return ret diff --git a/flag_groups.go b/flag_groups.go index 0671ec5f..6e938325 100644 --- a/flag_groups.go +++ b/flag_groups.go @@ -130,7 +130,7 @@ func processFlagForGroupAnnotation(flags *flag.FlagSet, pflag *flag.Flag, annota continue } - groupStatus[group] = map[string]bool{} + groupStatus[group] = make(map[string]bool, len(flagnames)) for _, name := range flagnames { groupStatus[group][name] = false }