Skip to content

Commit

Permalink
Improve performance of List (#418)
Browse files Browse the repository at this point in the history
This updates the List method to use the ShardRepoMaxMatchCount option
when it runs a search so that it doesn't need to search each repository
individually and sequentially. The goal here is to improve performance
for Sourcegraph queries like `repo:has.file(test.go)`.
  • Loading branch information
camdencheek authored Sep 2, 2022
1 parent e97813e commit d1964a3
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,30 +568,32 @@ func (d *indexData) gatherBranches(docID uint32, mt matchTree, known map[matchTr
}

func (d *indexData) List(ctx context.Context, q query.Q, opts *ListOptions) (rl *RepoList, err error) {
var include func(rle *RepoListEntry) (bool, error)
var include func(rle *RepoListEntry) bool

q = d.simplify(q)
if c, ok := q.(*query.Const); ok {
if !c.Value {
return &RepoList{}, nil
}
include = func(rle *RepoListEntry) (bool, error) {
return true, nil
include = func(rle *RepoListEntry) bool {
return true
}
} else {
// We need to run a search per repo to decide if it is included.
include = func(rle *RepoListEntry) (bool, error) {
qOneRepo := query.NewAnd(
query.NewRepoSet(rle.Repository.Name),
q)
sr, err := d.Search(ctx, qOneRepo, &SearchOptions{
ShardMaxMatchCount: 1,
TotalMaxMatchCount: 1,
})
if err != nil {
return false, err
}
return len(sr.Files) > 0, nil
sr, err := d.Search(ctx, q, &SearchOptions{
ShardRepoMaxMatchCount: 1,
})
if err != nil {
return nil, err
}

foundRepos := make(map[string]struct{}, len(sr.Files))
for _, file := range sr.Files {
foundRepos[file.Repository] = struct{}{}
}

include = func(rle *RepoListEntry) bool {
_, ok := foundRepos[rle.Repository.Name]
return ok
}
}

Expand All @@ -609,12 +611,7 @@ func (d *indexData) List(ctx context.Context, q query.Q, opts *ListOptions) (rl
continue
}
rle := &d.repoListEntry[i]
ok, err := include(rle)
if err != nil {
return nil, err
}

if !ok {
if !include(rle) {
continue
}

Expand Down

0 comments on commit d1964a3

Please sign in to comment.