Skip to content

Commit 1f59e31

Browse files
committed
perf(utils): pre-size FilterArgs result slice (was unpresized regrow per CLI run)
FilterArgs ran on every Cli.Run with an unpresized 'var clean []string' + append — geometric regrow. Pre-sized to len(args) (almost all args survive the filter); returns nil when empty to stay byte-identical. 1 alloc, no regrow. Co-Authored-By: Virgil <virgil@lethean.io>
1 parent 8459346 commit 1f59e31

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

utils.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,22 @@ func ArgBool(index int, args ...any) bool {
170170
//
171171
// clean := core.FilterArgs(os.Args[1:])
172172
func FilterArgs(args []string) []string {
173-
var clean []string
173+
if len(args) == 0 {
174+
return nil
175+
}
176+
// Pre-size to len(args): almost every arg survives the filter, so the
177+
// append never grows the backing array (was an unpresized geometric
178+
// regrow on every CLI invocation via Cli.Run).
179+
clean := make([]string, 0, len(args))
174180
for _, a := range args {
175181
if a == "" || HasPrefix(a, "-test.") {
176182
continue
177183
}
178184
clean = append(clean, a)
179185
}
186+
if len(clean) == 0 {
187+
return nil // byte-identical to the old var-nil behaviour
188+
}
180189
return clean
181190
}
182191

0 commit comments

Comments
 (0)