You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add --max-results to end a search after the file with the N-th matching line - #226
A pattern that matches most of a large tree can only be cut short by killing the search or piping it into head, and a server searches every candidate either way.
--max-results <N> stops after the file in which the N-th matching line is found. Files are reported whole, so a few more lines than N can be printed. The lines counted are those of the source, so a multiline match counts every line it covers, however it is printed.
A scan and the on-disk index stop at that file.
A plain server gets it as the max_results search option, stops after the chunk of candidates that reaches it, and marks the final reply "truncated": true.
A shared daemon and a search with --sort search every candidate, and the limit is applied to the result.
It cannot be combined with -l, --files-without-match, -c, --count-matches, -q or --files.
On a 154k-file private tree (Windows), a query with 37k candidate files that takes 6.5 s in full ends after 0.6 s with --max-results 10000.
Tested by tests/max_results.rs (scan, on-disk index, server, several search paths, multiline matches, and the rejected flag combinations), a shared daemon test, and a unit test for counting the rows of an older server. README and AGENTS.md describe the flag.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The result limit can be exceeded because file identities collide across search roots and multiline or replacement output is counted instead of original matched source lines.
…ng line
A pattern that matches most of a large tree is only cut short by killing the
search or piping it into head, and a server searches every candidate either
way.
--max-results <N> stops after the file in which the N-th matching line is
found, so the output is whole files. A scan and the on-disk index stop there.
The plain server gets it as the max_results search option, stops after the
chunk of candidates that reaches it, and marks the final reply "truncated".
A shared daemon and a search with --sort search every candidate, and the
limit is applied to the result. It cannot be combined with -l,
--files-without-match, -c, --count-matches, -q or --files.
On a 154k-file private tree (Windows), with the candidate reads batched (a
separate change), a query with 37k candidate files that takes 6.5 s in full
ends after 0.6 s with --max-results 10000.
The limit was counted from the rows written, keyed by the file's path under
its search root and the row's line number. Files of one name under separate
search paths were taken for one file, and a match of several lines printed
as one row (-U with --vimgrep or --replace) counted as one line.
Each reported file now adds the matching lines of its source, as its match
totals give them. A scan and the on-disk index take them from the file's
matches, and the server carries them with the file's result. For the count
it keeps across search paths, the client asks every limited search for the
per-file totals; rows are only counted for a server that predates those.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Server-side limiting counts binary matches that the client later hides, allowing them to exhaust --max-results and suppress valid text matches, potentially producing empty output and exit 1.
A file can be indexed as text and still be found binary when it is searched:
indexing inspects only its first 8 KiB, and a file can change before the
index catches up. The server counted the matching lines of such a file
towards max_results, while the client leaves a binary file out of a directory
search. A limit reached on that file ended the search with nothing printed,
though text files matched.
The server no longer counts a binary file. The client counts one that it
reports, as a scan does.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Multi-path searches fail to pass the remaining result budget to the server, causing unnecessary work, while sorted, limited searches overcount match statistics through servers or shared daemons.
For later command-line search paths, this sends the original invocation limit rather than the remaining budget. If the first path reports 9,999 matching lines with --max-results 10000, a server answering the second path still searches until it finds another 10,000 lines (subject to chunk boundaries), although the client only needs the next matching file. This loses the early-stop benefit on broad searches across multiple paths. Subtract writer.reported_lines() before sending the server limit.
Exclude truncated files from reported match totals
tgrep-cli/src/search.rs:1823
With five one-line matching files, --sort path --max-results 1 --stats prints one match but reports five matches and five matched lines through a server or shared daemon. Track the files retained by this loop and restrict the match totals below to those files, including the older-server fallback; search-work statistics can still include all searched candidates.
A search of several paths sent each path's server the whole limit, so a
later path was searched for more lines than the output still needed. It now
gets what is left.
With --stats, a limit applied to a server's or shared daemon's reply (a
sorted search, a shared daemon) printed the totals of every file in the
reply, where a scan reports the files it printed. The totals now cover the
files ahead of the one at which the limit ended the output.
Both points from the latest review are addressed in 3ad98c7.
Remaining budget for later paths: the client sends a server what is left of the limit (limit - reported lines), so a later search path ends as early as the first. limited_request covers the calculation.
Totals of a limited search: with --stats, the totals cover the files ahead of the one at which the limit ended the output, including the older-server fallback, so a server or shared daemon reports what a scan does. stats_cover_reported_files checks a server with and without --sort.
I think these two fails are also spurious, not caused by this change:
Clippy fails on two needless_bool lints in serve.rs (lines 3572 and 7669) that also fail on main's scheduled run
the ubuntu failure is managed::faults::operations_cancel_stops_an_in_flight_collection_without_later_delete_credit, which passes on the other platforms and on main, looks flaky?
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
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.
A pattern that matches most of a large tree can only be cut short by killing the search or piping it into
head, and a server searches every candidate either way.--max-results <N>stops after the file in which the N-th matching line is found. Files are reported whole, so a few more lines than N can be printed. The lines counted are those of the source, so a multiline match counts every line it covers, however it is printed.max_resultssearch option, stops after the chunk of candidates that reaches it, and marks the final reply"truncated": true.--sortsearch every candidate, and the limit is applied to the result.-l,--files-without-match,-c,--count-matches,-qor--files.On a 154k-file private tree (Windows), a query with 37k candidate files that takes 6.5 s in full ends after 0.6 s with
--max-results 10000.Tested by
tests/max_results.rs(scan, on-disk index, server, several search paths, multiline matches, and the rejected flag combinations), a shared daemon test, and a unit test for counting the rows of an older server. README and AGENTS.md describe the flag.