Skip to content

bash: build option/subcommand strings directly - #238

Open
haydonryan wants to merge 1 commit into
google:masterfrom
haydonryan:pr-4ab-bash-direct-build
Open

bash: build option/subcommand strings directly#238
haydonryan wants to merge 1 commit into
google:masterfrom
haydonryan:pr-4ab-bash-direct-build

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.

LLM Generated below here:

In argh_complete::bash::generate_bash_dispatch, three lists were built by
collecting per-item Strings into a Vec<String> and then join-ing them:
the opts list, the cmds list, and the prev_matches list. This PR builds
all three directly by accumulating into a single String (push/push_str
with separators).

Why it saves binary space

  • Fewer heap allocations: no per-item String (via .to_string()/format!)
    and no join result String — one buffer filled in place.
  • Less monomorphized generic code: removes Vec<String> instantiation and the
    SliceConcatExt::join monomorphization for Vec<String>, plus the
    format!/Display machinery at each call site.

Measurement

Release-mode text-size deltas on completion_example (default profile),

Measured with and without LTO (default release profile
has no LTO; lto=true is the full/fat LTO the testbed uses):

Build Baseline PR Delta
No LTO (default release) 418,760 416,480 −2,280
Fat LTO (lto=true) 382,647 381,375 −1,272

Under fat LTO the win roughly halves (−1,272) because LTO already folds much
of the redundant Vec/join/format! code the change targets. The headline
number is the no-LTO case: −2,280 bytes.

Behavior unchanged — verified by the argh_complete exact-match tests passing
and the full workspace suite (13 suites, 0 failures).

In generate_bash_dispatch, build the opts, cmds, and prev_matches strings by
accumulating directly into a String (push/push_str with separators) instead of
collecting per-item Strings into a Vec and joining them.

Same output; fewer heap allocations and less monomorphized generic code
(Vec<String> + SliceConcatExt::join + format! removed from this function).

Measured: -2280 bytes text on completion_example (release, default profile).
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