Skip to content

Commit

Permalink
Reduce memory allocation in cli.New (#96)
Browse files Browse the repository at this point in the history
* Add a benchmark for cli.New

* Reduce memory allocation in cli.New

* Add TODO around the flags maps
  • Loading branch information
FollowTheProcess authored Sep 22, 2024
1 parent 3713af3 commit 9d76e8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
23 changes: 18 additions & 5 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,6 @@ func TestOptionValidation(t *testing.T) {
options: []cli.Option{cli.Example("comment here", "")},
errMsg: "example command cannot be empty",
},
{
name: "example both empty",
options: []cli.Option{cli.Example("", "")},
errMsg: "example comment cannot be empty\nexample command cannot be empty",
},
{
name: "empty short description",
options: []cli.Option{cli.Short("")},
Expand Down Expand Up @@ -832,6 +827,24 @@ func BenchmarkExecuteHelp(b *testing.B) {
}
}

// Benchmarks calling New to build a typical CLI.
func BenchmarkNew(b *testing.B) {
for range b.N {
_, err := cli.New(
"benchy",
cli.Short("A typical CLI to benchmark calling cli.New"),
cli.Version("dev"),
cli.Commit("dfdddaf"),
cli.Example("An example", "bench --help"),
cli.Allow(cli.AnyArgs()),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
)
if err != nil {
b.Fatal(err)
}
}
}

// shuffle returns a randomly ordered copy of items.
func shuffle[T any](items []T) []T {
shuffled := slices.Clone(items)
Expand Down
2 changes: 2 additions & 0 deletions internal/flag/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

// Set is a set of command line flags.
type Set struct {
// TODO(@FollowTheProcess): Figure out a way so that we don't have to store 2 maps
// as it's very memory wasteful
flags map[string]Entry // The actual stored flags, can lookup by name
shorthands map[rune]Entry // The flags by shorthand
args []string // Arguments minus flags or flag values
Expand Down
8 changes: 2 additions & 6 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,11 @@ func Long(long string) Option {
// An arbitrary number of examples can be added to a [Command], and calls to [Example] are additive.
func Example(comment, command string) Option {
f := func(cfg *config) error {
errs := make([]error, 0, 2) //nolint:mnd // 2 here is because we have two arguments
if comment == "" {
errs = append(errs, errors.New("example comment cannot be empty"))
return errors.New("example comment cannot be empty")
}
if command == "" {
errs = append(errs, errors.New("example command cannot be empty"))
}
if len(errs) > 0 {
return errors.Join(errs...)
return errors.New("example command cannot be empty")
}
cfg.examples = append(cfg.examples, example{comment: comment, command: command})
return nil
Expand Down

0 comments on commit 9d76e8c

Please sign in to comment.