args_info: call get_args_info once per enum variant - #237
Open
haydonryan wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, eachvariant previously called
<Ty>::get_args_info()twice — once for.nameand once for
.command— constructing a fullCommandInfoWithArgsvalue twiceand discarding one. This PR calls it once and reuses the value for both fields.
get_args_info()returns a 9-fieldCommandInfoWithArgs<'static>that includesa heap
Vec<SubCommandInfo>(built and dropped). In the test binary this functionis 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
Vecallocation pervariant) holds regardless of inlining.