codefloe/forgejo-codefloe
Watch
4
12
Fork
You've already forked forgejo-codefloe
0

fix(indexer): bound zoekt code search #82

Merged
pat-s merged 2 commits from fix/bound-zoekt-code-search into v16.0/forgejo-codefloe 2026-08-10 15:23:12 +00:00
Owner

The code indexer passes zoekt a bare SearchOptions, so every limit is zero:

result, err := b.inner.Searcher.Search(ctx, q, &zoekt.SearchOptions{
    Whole: true,
})

zoekt does ship caps for exactly this, in SearchOptions.SetDefaults:

if o.ShardMaxMatchCount == 0 {
    // We cap the total number of matches, so overly broad
    // searches don't crash the machine.
    o.ShardMaxMatchCount = 100000
}

but SetDefaults is only ever called from zoekt's own cmd/ binaries and from the index builder, never on the search path. A caller that builds SearchOptions by hand opts out of the protection without meaning to.

Combined with Whole: true, which attaches the full body of every matching file, one query materialises the whole corpus into memory before paginateResults cuts it down to a single page.

Measured

Against a live instance with 2726 shards, using zoekt directly against the same index directory so the numbers isolate the search itself:

query files materialised elapsed
import 178,379 23.351s
error 85,049 6.473s
package main 1,243 43ms

Every one of those renders 20 results.

With the caps in this PR, same corpus, same queries:

query files materialised elapsed
import 1,000 172ms 136x
error 1,000 202ms 32x
package main 1,000 24ms 1.8x

Values

  • ShardMaxMatchCount and TotalMaxMatchCount are zoekt's own defaults. They do not bind at this corpus size and are there as insurance for a larger one.
  • MaxWallTime is 2s. zoekt's own web server uses 10s; a page render wants to be well inside that. It did not trigger in any measurement above, so it is a ceiling for pathological queries rather than a limit on normal ones.
  • MaxDocDisplayCount is 1000, matching the result cap GitHub applies to its code search. This is the one doing the work: it truncates after collating, so the file bodies beyond the first thousand are never materialised.

Tradeoff

The result total and the page count become capped rather than exact: a query matching 178k files now reports 1000. Deep pagination past the cap is no longer possible. That is the same behaviour GitHub has, and the alternative is a request that can occupy a core for 23 seconds.

The code indexer passes zoekt a bare `SearchOptions`, so every limit is zero: ```go result, err := b.inner.Searcher.Search(ctx, q, &zoekt.SearchOptions{ Whole: true, }) ``` zoekt does ship caps for exactly this, in `SearchOptions.SetDefaults`: ```go if o.ShardMaxMatchCount == 0 { // We cap the total number of matches, so overly broad // searches don't crash the machine. o.ShardMaxMatchCount = 100000 } ``` but `SetDefaults` is only ever called from zoekt's own `cmd/` binaries and from the index builder, never on the search path. A caller that builds `SearchOptions` by hand opts out of the protection without meaning to. Combined with `Whole: true`, which attaches the full body of every matching file, one query materialises the whole corpus into memory before `paginateResults` cuts it down to a single page. ## Measured Against a live instance with 2726 shards, using zoekt directly against the same index directory so the numbers isolate the search itself: | query | files materialised | elapsed | | --- | --- | --- | | `import` | 178,379 | 23.351s | | `error` | 85,049 | 6.473s | | `package main` | 1,243 | 43ms | Every one of those renders 20 results. With the caps in this PR, same corpus, same queries: | query | files materialised | elapsed | | | --- | --- | --- | --- | | `import` | 1,000 | 172ms | 136x | | `error` | 1,000 | 202ms | 32x | | `package main` | 1,000 | 24ms | 1.8x | ## Values - `ShardMaxMatchCount` and `TotalMaxMatchCount` are zoekt's own defaults. They do not bind at this corpus size and are there as insurance for a larger one. - `MaxWallTime` is 2s. zoekt's own web server uses 10s; a page render wants to be well inside that. It did not trigger in any measurement above, so it is a ceiling for pathological queries rather than a limit on normal ones. - `MaxDocDisplayCount` is 1000, matching the result cap GitHub applies to its code search. This is the one doing the work: it truncates after collating, so the file bodies beyond the first thousand are never materialised. ## Tradeoff The result total and the page count become capped rather than exact: a query matching 178k files now reports 1000. Deep pagination past the cap is no longer possible. That is the same behaviour GitHub has, and the alternative is a request that can occupy a core for 23 seconds.
fix(indexer): bound zoekt code search
Some checks failed
testing / test-unit (pull_request) Successful in 5m54s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m40s
testing / backend-checks (pull_request) Successful in 5m51s
testing / frontend-checks (pull_request) Successful in 1m30s
testing / test-remote-cacher (garnet) (pull_request) Successful in 6m33s
testing / test-remote-cacher (valkey) (pull_request) Successful in 1m25s
testing / test-e2e (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
e208230ba4
The code indexer passed zoekt a bare SearchOptions, so every limit was
zero. zoekt does ship caps for this, in SearchOptions.SetDefaults, under
the comment "We cap the total number of matches, so overly broad searches
don't crash the machine", but SetDefaults is only ever called from zoekt's
own commands and the index builder, never on the search path. A caller
that builds SearchOptions by hand therefore opts out of the protection
without meaning to.

Combined with Whole, which attaches the full body of every matching file,
one query materialises the whole corpus before the results are paginated
down to a single page. Measured against a 2726 shard instance:

  query "import": 178379 files, 23.351s
  query "error":   85049 files,  6.473s

Both render 20 results.

Set the two counts to zoekt's own defaults, add the wall clock ceiling
that zoekt's web server uses, and truncate the returned documents at 1000,
the same result cap GitHub applies to its code search. Same corpus, same
queries:

  query "import": 1000 files, 172ms
  query "error":  1000 files, 202ms

The counts do not bind at this corpus size and are there as insurance;
the work saved is the file bodies that no longer have to be materialised.
The visible tradeoff is that the result total and the page count are now
capped rather than exact.
pat-s scheduled this pull request to auto merge when all checks succeed 2026-08-10 14:15:30 +00:00
docs: record the bounded code search in the custom changes list
All checks were successful
testing / frontend-checks (pull_request) Successful in 1m5s
testing / backend-checks (pull_request) Successful in 5m2s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m42s
testing / test-remote-cacher (garnet) (pull_request) Successful in 6m32s
testing / test-unit (pull_request) Successful in 9m1s
testing / test-remote-cacher (valkey) (pull_request) Successful in 1m21s
testing / test-e2e (pull_request) Successful in 26m39s
testing / test-mysql (pull_request) Successful in 30m39s
testing / test-sqlite (pull_request) Successful in 39m1s
testing / test-pgsql (pull_request) Successful in 42m2s
issue-labels / backporting (pull_request_target) Has been skipped
milestone / set (pull_request_target) Has been skipped
a8a2d8f88b
pat-s merged commit a127b3cd1e into v16.0/forgejo-codefloe 2026-08-10 15:23:12 +00:00
pat-s deleted branch fix/bound-zoekt-code-search 2026-08-10 15:23:12 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
codefloe/forgejo-codefloe!82
No description provided.