Visitar URL original
perf(complete): shrink completion splitting and error suggestions by jdx · Pull Request #1554 · jdx/usage · GitHub
Skip to content

perf(complete): shrink completion splitting and error suggestions - #1554

Merged
jdx merged 16 commits into
mainfrom
perf/argv-complete-size
Oct 14, 2026
Merged

jdx merged 16 commits into
mainfrom
perf/argv-complete-size

Conversation

@jdx

@jdx jdx commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

https://entire.io/gh/jdx/usage/trails/95

Third pass on size, this time in completion and parse-error rendering (complete.rs, diagnostic.rs). Stacked on the help-rendering PR above it.

What changed

  • flag_meta and arg_meta walk the CLI's static metadata tree comparing addresses. The optimiser unrolled them into one compare per flag at every call site, so the cost grew with the number of flags. Keeping them out of line saves about 5.4 KB on oxlint and 2 KB on oxfmt, and more on larger CLIs.
  • split keeps its state in a struct with out-of-line transition functions; the jaro suggestion search and nearest avoid indexing and sorting; the usize-to-text conversion and name copying into errors are compiled once.
  • Fifteen more ideas were measured and reverted when they grew the binary.

Completion splitting was compared against the previous implementation on 18 million random cases (all six shells, quotes, escapes, non-ASCII whitespace, arbitrary cursors) with identical results, and 26 error and completion invocations of oxlint and oxfmt are byte-identical. Split::walked and Split::argv no longer panic when cword is out of range; they return the whole word list and an empty slice.

Measured

Section sums against the parent branch: oxlint 14,906,504 → 14,896,736 B (-9,768), oxfmt 9,243,420 → 9,237,348 B (-6,072). Interleaved min-of-300 wall times of startup, completion and error paths are flat (e.g. completion of flags 662 µs vs 653 µs).

This description was generated by Claude Code.

🤖 Generated with Claude Code

AI-assisted — Tool: Claude Code; model: anthropic/claude-sonnet-5-5; version: unavailable.


Note

Medium Risk
Touches shell completion splitting, parse-error suggestions, and core parser slicing on hot paths; behavior is intended to stay identical but the surface area is large enough that subtle completion or parse edge cases could regress.

Overview
Performance and codegen: Completion and diagnostics shrink duplicated machine code by marking tree walks (flag_meta, arg_meta) and small hot helpers #[inline(never)], refactoring shell split behind a Splitting state type, and replacing sort-based Jaro/nearest suggestion logic with in-place matching and insertion ordering. Candidate dedup uses dedup_by; several string paths move to byte-wise checks and shared offer_owned_like / owned / number helpers.

Safety and parser hot path: A new slice::before / slice::after module replaces panicking &s[n..] indexing across the parser, completion, and error rendering; Split::walked and Split::argv degrade gracefully when cword is out of range. Bash colon marking for completions is rewritten to track escapes explicitly.

Derive / static data: Generated entry points call __usage_process_args, __usage_borrow_words, and __usage_own_words so simple CLIs borrow argv[1..] instead of copying; flattened command groups reference __usage_subslice views into the parent’s joined flag/arg tables instead of embedding a second copy of flattened metadata.

Reviewed by Cursor Bugbot for commit 5c81cd9. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of incomplete or unusual command-line input, reducing unexpected failures during parsing, completion, and error reporting.
    • Preserved escaped colons and cursor positions more accurately in shell completion, including PowerShell doubled quotes.
    • Improved command suggestions with more consistent matching, ordering, and handling of negated flags.
    • Corrected metadata associations for arguments and flags in flattened commands.

@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Configuration used: Central YAML (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a0b5c4ba-117c-45c1-81e7-3ccdd9f0777f
📥 Commits

Reviewing files that changed from the base of the PR and between afe35b5 and 5c81cd9.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Central YAML (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fac79fee-a3fe-4855-b6a6-299789a92820
📥 Commits

Reviewing files that changed from the base of the PR and between 524cb13 and afe35b5.

📒 Files selected for processing: 5
  • argv/src/complete.rs
  • argv/src/diagnostic.rs
  • argv/src/lib.rs
  • argv/src/slice.rs
  • derive/src/codegen.rs

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

📝 Walkthrough

This change updates runtime argument parsing, completion, and diagnostics. It adds shared argument and slice helpers, changes generated code to borrow process arguments where applicable, and tracks positional argument offsets when composing flattened command tables.

Changes: Argument handling and generated parser updates

Layer / File(s) Summary
Runtime argument helpers and parsing
argv/src/lib.rs, argv/src/slice.rs
Adds hidden process-argument and static-subslice helpers, plus bounds-aware slice helpers used in argument parsing and command traversal. Rewrites is_number to scan the mantissa and optional exponent.
Completion candidates and word splitting
argv/src/complete.rs
Uses checked access and shared state for completion parsing and splitting. Updates shell-specific handling, file-type matching, candidate metadata merging, restart checks, and flag candidate construction. Marks flag_meta and arg_meta as non-inlined.
Diagnostic suggestions and rendering
argv/src/diagnostic.rs
Uses checked slicing and root-command fallbacks. Adds negated flag names to suggestions, changes Jaro matching and candidate ordering, and updates selector parsing and string-formatting helpers.
Generated argument collection and borrowing
derive/src/codegen.rs
Generated code collects process arguments through the new helpers and borrows words where applicable. View and multicall rewriting continues to use owned words. Embedded outcomes and completer setup also use the borrowing helper.
Flattened command metadata offsets
derive/src/codegen.rs
Tracks positional argument offsets when composing flattened tables and builds flattened-group metadata from slices of the parent tables.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Suggested reviewers: lu-zero

Merge Risk: ⚪ Minimal · up to afe35

No identified issue needs correction before merge; proceed with normal checks.

Pre-merge checks: Passed 5
Passed Passed checks: 5 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely identifies the main changes: performance improvements to completion splitting and error suggestions.
Docstring Coverage Passed Docstring coverage is 81.25% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 64 functions across 5 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable defects were found.

Summary

This PR reduces repeated code in completion, error rendering, parsing, and generated entry points.

  • Shell completions split words and keep cursor positions bounded.
  • Completion candidates keep their labels while repeated lookup code stays shared.
  • Parse errors keep close-name suggestions in the same order.
  • The parser uses bounded slices while checking command-line words.
  • Generated CLIs borrow ordinary command-line words directly.
  • Flattened command metadata points into the parent’s joined tables.

No new actionable issues were found. The previous unnumbered finding about large suggestion lists was explicitly dismissed by jdx: realistic lists are small, and restoring a shared sort would undo the size savings. That thread is resolved.

The previous review commit failed the ancestry and merge-base checks, so this review covered the full PR diff. No tests were run.

Reviews (3) · Last reviewed commit: "perf(derive): stop emitting a second cop..." · Reviewed by Greptile

Comment thread argv/src/diagnostic.rs Resolved
@jdx
jdx added this pull request to stack #1556 October 14, 2026 18:39
Base automatically changed from perf/argv-help-size to main October 14, 2026 19:31
jdx and others added 16 commits October 14, 2026 19:31
… own

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ed description copy

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The search compares addresses along a tree that is a constant in each CLI, and the optimizer
unrolled it at every call site into a comparison per flag.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… routines

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… a short list

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@jdx
jdx force-pushed the perf/argv-complete-size branch from afe35b5 to 5c81cd9 Compare October 14, 2026 19:31
@jdx
jdx merged commit 492beb3 into main Oct 14, 2026
14 checks passed
@jdx
jdx deleted the perf/argv-complete-size branch October 14, 2026 19:42
jdx pushed a commit that referenced this pull request Oct 14, 2026
<!-- entire-trail-link-start -->
https://entire.io/gh/jdx/usage/trails/93
<!-- entire-trail-link-end -->

### 📚 Documentation

- replace V7.md with V8.md for pending breaking changes by
[@jdx](https://github.com/jdx) in
[#1550](#1550)

### ⚡ Performance

- **(complete)** shrink completion splitting and error suggestions by
[@jdx](https://github.com/jdx) in
[#1554](#1554)
- **(lib)** shrink the help, completion and error rendering code in
usage-argv by [@jdx](https://github.com/jdx) in
[#1551](#1551)
- **(lib)** shrink the help renderer further by outlining wrapping,
slicing and searching by [@jdx](https://github.com/jdx) in
[#1553](#1553)

### 📦️ Dependency Updates

- bump jactionlint to 2.2.0 and mbx to 1.24.0 by
[@jdx](https://github.com/jdx) in
[#1549](#1549)
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