Skip to content

perf: build flag names with one allocation - #2553

Open
g4lb wants to merge 1 commit into
urfave:mainfrom
g4lb:perf/flagnames-one-alloc
Open

g4lb wants to merge 1 commit into
urfave:mainfrom
g4lb:perf/flagnames-one-alloc

Conversation

@g4lb

@g4lb g4lb commented Nov 20, 2026

Copy link
Copy Markdown

What type of PR is this?

  • cleanup

What this PR does / why we need it:

FlagNames built a temporary slice of the name and the aliases, and then appended to a slice with no capacity. Now it allocates the result once. Setup calls FlagNames many times through Names(), so each Run makes fewer allocations. The output does not change.

Benchmark Before After Allocs
FlagNames, 1 alias 171 ns 85 ns 3 → 1
FlagNames, 10 aliases 575 ns 208 ns 6 → 1
Run, 30 flags and 10 subcommands 131 µs 107 µs 1807 → 1425
Run a subcommand with flags 184 µs 146 µs 2783 → 2067

Which issue(s) this PR fixes:

None.

Testing

  • make vet, make lint, make test (also with urfave_cli_no_template), make v3diff and make check-binary-size pass.
  • Old and new Run output, errors and flag values matched on 1M random apps and arguments.
  • I measured no slowdown on AMD EPYC 7763 (Go 1.27.4, 24 interleaved rounds, 3 code layouts).

Release Notes

NONE

@g4lb
g4lb requested a review from a team as a code owner November 20, 2026 23:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant