Skip to content

args_info: call get_args_info once per enum variant - #237

Open
haydonryan wants to merge 1 commit into
google:masterfrom
haydonryan:pr-3a-args-info-single-call
Open

args_info: call get_args_info once per enum variant#237
haydonryan wants to merge 1 commit into
google:masterfrom
haydonryan:pr-3a-args-info-single-call

Conversation

@haydonryan

@haydonryan haydonryan commented Aug 16, 2026

Copy link
Copy Markdown

Hi there,
I'm experimenting with prompts for LLMs to finding code optimizations (size and speed), and ran it over this crate. It popped up with a few small improvements. This is one.

If inline saves 1,176 bytes

LLM generated below here:

In the derive-generated ArgsInfo::get_args_info() for subcommand enums, each
variant previously called <Ty>::get_args_info() twice — once for .name
and once for .command — constructing a full CommandInfoWithArgs value twice
and discarding one. This PR calls it once and reuses the value for both fields.

get_args_info() returns a 9-field CommandInfoWithArgs<'static> that includes
a heap Vec<SubCommandInfo> (built and dropped). In the test binary this function
is inlined, so the full construction + drop body is emitted at each call site.
Before the change, each enum variant therefore emitted two inlined copies of the
construction; after, one. The redundant second copy is removed per variant.

The runtime benefit (half the construction work, one fewer Vec allocation per
variant) holds regardless of inlining.

For each variant of a subcommand enum, the generated ArgsInfo::get_args_info
previously called <Ty>::get_args_info() twice (once for .name, once for
.command), constructing a full CommandInfoWithArgs twice and discarding one.
Call it once and reuse for both fields.

This halves the per-variant construction work and, since get_args_info is
inlined, removes a full duplicated CommandInfoWithArgs construction + drop
from the emitted machine code. Behavior is identical.
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