Skip to content

grep: short-circuit max-count zero#46

Open
wondr-wclabs wants to merge 1 commit into
uutils:mainfrom
wondr-wclabs:codex/max-count-zero-short-circuit
Open

grep: short-circuit max-count zero#46
wondr-wclabs wants to merge 1 commit into
uutils:mainfrom
wondr-wclabs:codex/max-count-zero-short-circuit

Conversation

@wondr-wclabs
Copy link
Copy Markdown
Contributor

Closes #4.

-m 0 means grep should stop before reading any input lines. In this case there can never be selected output, so after options and patterns are parsed and the matcher is compiled, the command can return the normal no-match exit before constructing the searcher or opening stdin/files.

That placement is intentional: invalid patterns still report a regex error, while missing searched files are not touched for -m 0. The -m 1 path still opens inputs and reports missing-file errors as before.

I added regression coverage for the missing-file short-circuit, the -m 1 control case, and invalid-pattern ordering.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jun 5, 2026

Merging this PR will not alter performance

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing wondr-wclabs:codex/max-count-zero-short-circuit (bbf205f) with main (d28bf76)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@wondr-wclabs wondr-wclabs force-pushed the codex/max-count-zero-short-circuit branch from 8fc34c5 to bbf205f Compare June 5, 2026 08:04
Copy link
Copy Markdown
Collaborator

@lhecker lhecker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this conflicts with your other PR. I suppose we should let the other one merge first.

@wondr-wclabs
Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. The overlap is the early-return slot after Matcher::compile(&config)?: #43 adds the inverted-empty-pattern no-match short-circuit there, and this PR adds the -m 0 no-match short-circuit in the same area, plus the same ExitCode import.

I agree #43 is the better one to land first if maintainers want that behavior in place before this one. I’ll keep this PR as-is since it is independently green and approved, then rebase it after #43 lands so the final code keeps both short-circuits together and preserves the current ordering: compile/validate the pattern first, then return code 1 without opening input files when the match semantics guarantee no selected lines.

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.

grep - with -m 0 still opens files instead of short-circuiting like GNU (errors on a missing file GNU never touches)

2 participants