fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

all: run modernize across codebase (#919)

The latest release of gopls has a feature called modernize which will
update your code where it can to use modern go features/pkgs.
https://github.com/golang/tools/releases/tag/gopls%2Fv0.18.0

Generated with:

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

Test Plan: CI

+130 -151
+3 -3
api_proto_test.go
··· 433 433 var buf bytes.Buffer 434 434 enc := gob.NewEncoder(&buf) 435 435 436 - for i := 0; i < count; i++ { 436 + for range count { 437 437 err := enc.Encode(exampleSearchResultGo) 438 438 if err != nil { 439 439 panic(err) ··· 442 442 } 443 443 444 444 dec := gob.NewDecoder(&buf) 445 - for i := 0; i < count; i++ { 445 + for range count { 446 446 var res SearchResult 447 447 err := dec.Decode(&res) 448 448 if err != nil { ··· 459 459 b.Run(fmt.Sprintf("count=%d", count), func(b *testing.B) { 460 460 for i := 0; i < b.N; i++ { 461 461 buffers := make([][]byte, 0, count) 462 - for i := 0; i < count; i++ { 462 + for range count { 463 463 buf, err := proto.Marshal(exampleSearchResultProto) 464 464 if err != nil { 465 465 b.Fatal(err)
+2 -2
api_test.go
··· 63 63 b.Run("map", benchmarkEncoding(mapData)) 64 64 } 65 65 66 - func benchmarkEncoding(data interface{}) func(*testing.B) { 66 + func benchmarkEncoding(data any) func(*testing.B) { 67 67 return func(b *testing.B) { 68 68 b.Helper() 69 69 ··· 169 169 opts := SearchOptions{} 170 170 var fieldNames []string 171 171 rv := reflect.ValueOf(&opts).Elem() 172 - for i := 0; i < rv.NumField(); i++ { 172 + for i := range rv.NumField() { 173 173 f := rv.Field(i) 174 174 name := rv.Type().Field(i).Name 175 175 fieldNames = append(fieldNames, name)
+1 -4
cmd/zoekt-indexserver/main.go
··· 68 68 log.Fatal("cpu_fraction must be between 0.0 and 1.0") 69 69 } 70 70 71 - o.cpuCount = int(math.Trunc(float64(runtime.GOMAXPROCS(0)) * o.cpuFraction)) 72 - if o.cpuCount < 1 { 73 - o.cpuCount = 1 74 - } 71 + o.cpuCount = max(int(math.Trunc(float64(runtime.GOMAXPROCS(0))*o.cpuFraction)), 1) 75 72 if o.indexFlagsStr != "" { 76 73 o.indexFlags = strings.Split(o.indexFlagsStr, " ") 77 74 }
+2 -2
cmd/zoekt-mirror-bitbucket-server/main.go
··· 179 179 180 180 func getAllRepos(client bitbucketv1.APIClient) ([]bitbucketv1.Repository, error) { 181 181 var allRepos []bitbucketv1.Repository 182 - opts := map[string]interface{}{ 182 + opts := map[string]any{ 183 183 "limit": 1000, 184 184 "start": 0, 185 185 } ··· 208 208 209 209 func getProjectRepos(client bitbucketv1.APIClient, projectName string) ([]bitbucketv1.Repository, error) { 210 210 var allRepos []bitbucketv1.Repository 211 - opts := map[string]interface{}{ 211 + opts := map[string]any{ 212 212 "limit": 1000, 213 213 "start": 0, 214 214 }
+5 -5
cmd/zoekt-sourcegraph-indexserver/backoff_test.go
··· 95 95 emptyQueue(queue) 96 96 97 97 // consecutive failures will push backoff until to a further out time 98 - for i := 0; i < 1000; i++ { 98 + for range 1000 { 99 99 queue.SetIndexed(opts, indexStateFail) 100 100 } 101 101 ··· 134 134 emptyQueue(queue) 135 135 136 136 // consecutive failures increase duration up to a maximum 137 - for i := 0; i < 100; i++ { 137 + for range 100 { 138 138 queue.SetIndexed(opts, indexStateFail) 139 139 } 140 140 ··· 309 309 now := time.Now() 310 310 expectedFailuresCount := 0 311 311 312 - for i := 0; i < failedCount; i++ { 312 + for i := range failedCount { 313 313 backoff.Fail(now.Add(time.Duration(i)*backoffDuration), logtest.Scoped(t), opts) 314 314 expectedFailuresCount++ 315 315 assertFailuresCount(t, expectedFailuresCount, backoff) ··· 332 332 expectedFailuresCount := 0 333 333 334 334 // consecutive failures count increments per failure 335 - for i := 0; i < maximumCount; i++ { 335 + for i := range maximumCount { 336 336 backoff.Fail(now.Add(time.Duration(i)*backoffDuration), logtest.Scoped(t), opts) 337 337 expectedFailuresCount++ 338 338 assertFailuresCount(t, expectedFailuresCount, backoff) ··· 356 356 maxBackoff: maxBackoffDuration, 357 357 } 358 358 359 - for i := 0; i < failedCount; i++ { 359 + for i := range failedCount { 360 360 now := time.Now() 361 361 362 362 // fail j consecutive times
+4 -7
cmd/zoekt-sourcegraph-indexserver/index_test.go
··· 8 8 "os" 9 9 "os/exec" 10 10 "path/filepath" 11 - "sort" 12 11 "strings" 13 12 "testing" 14 13 "time" ··· 20 19 "google.golang.org/grpc" 21 20 "google.golang.org/protobuf/testing/protocmp" 22 21 "google.golang.org/protobuf/types/known/timestamppb" 22 + 23 + "slices" 23 24 24 25 "github.com/sourcegraph/zoekt" 25 26 configv1 "github.com/sourcegraph/zoekt/cmd/zoekt-sourcegraph-indexserver/grpc/protos/sourcegraph/zoekt/configuration/v1" ··· 137 138 return 138 139 } 139 140 140 - sort.Slice(iteratedIDs, func(i, j int) bool { 141 - return iteratedIDs[i] < iteratedIDs[j] 142 - }) 141 + slices.Sort(iteratedIDs) 143 142 144 143 expectedIDs := []uint32{1, 2, 3} 145 - sort.Slice(expectedIDs, func(i, j int) bool { 146 - return expectedIDs[i] < expectedIDs[j] 147 - }) 144 + slices.Sort(expectedIDs) 148 145 149 146 if diff := cmp.Diff(expectedIDs, iteratedIDs); diff != "" { 150 147 t.Fatalf("unexpected repo ids (-want +got):\n%s", diff)
+7 -10
cmd/zoekt-sourcegraph-indexserver/main.go
··· 64 64 "github.com/sourcegraph/zoekt/internal/profiler" 65 65 "github.com/sourcegraph/zoekt/internal/tenant" 66 66 67 + "slices" 68 + 67 69 "go.uber.org/automaxprocs/maxprocs" 68 70 "go.uber.org/multierr" 69 71 "golang.org/x/net/trace" ··· 418 420 } 419 421 }() 420 422 421 - for i := 0; i < s.IndexConcurrency; i++ { 423 + for range s.IndexConcurrency { 422 424 go s.processQueue() 423 425 } 424 426 ··· 433 435 } 434 436 435 437 sb := strings.Builder{} 436 - for i := 0; i < m; i++ { 438 + for i := range m { 437 439 fmt.Fprintf(&sb, "%d, ", v[i]) 438 440 } 439 441 ··· 1105 1107 for id := range index { 1106 1108 repoIDs = append(repoIDs, id) 1107 1109 } 1108 - sort.Slice(repoIDs, func(i, j int) bool { 1109 - return repoIDs[i] < repoIDs[j] 1110 - }) 1110 + slices.Sort(repoIDs) 1111 1111 return repoIDs 1112 1112 } 1113 1113 ··· 1535 1535 } 1536 1536 } 1537 1537 1538 - cpuCount := int(math.Round(float64(runtime.GOMAXPROCS(0)) * (conf.cpuFraction))) 1539 - if cpuCount < 1 { 1540 - cpuCount = 1 1541 - } 1538 + cpuCount := max(int(math.Round(float64(runtime.GOMAXPROCS(0))*(conf.cpuFraction))), 1) 1542 1539 1543 1540 if conf.indexConcurrency < 1 { 1544 1541 conf.indexConcurrency = 1 ··· 1592 1589 var defaultGRPCServiceConfigurationJSON string 1593 1590 1594 1591 func internalActorUnaryInterceptor() grpc.UnaryClientInterceptor { 1595 - return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { 1592 + return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { 1596 1593 ctx = metadata.AppendToOutgoingContext(ctx, "X-Sourcegraph-Actor-UID", "internal") 1597 1594 return invoker(ctx, method, req, reply, cc, opts...) 1598 1595 }
+6 -13
cmd/zoekt-sourcegraph-indexserver/main_test.go
··· 9 9 "net/url" 10 10 "os" 11 11 "path/filepath" 12 - "sort" 13 12 "strings" 14 13 "testing" 15 14 ··· 20 19 "google.golang.org/grpc" 21 20 22 21 "github.com/google/go-cmp/cmp" 22 + 23 + "slices" 23 24 24 25 "github.com/sourcegraph/zoekt" 25 26 configv1 "github.com/sourcegraph/zoekt/cmd/zoekt-sourcegraph-indexserver/grpc/protos/sourcegraph/zoekt/configuration/v1" ··· 154 155 listCalled = true 155 156 156 157 gotRepoIDs := in.GetIndexedIds() 157 - sort.Slice(gotRepoIDs, func(i, j int) bool { 158 - return gotRepoIDs[i] < gotRepoIDs[j] 159 - }) 158 + slices.Sort(gotRepoIDs) 160 159 161 160 wantRepoIDs := []int32{1, 3} 162 - sort.Slice(wantRepoIDs, func(i, j int) bool { 163 - return wantRepoIDs[i] < wantRepoIDs[j] 164 - }) 161 + slices.Sort(wantRepoIDs) 165 162 166 163 if diff := cmp.Diff(wantRepoIDs, gotRepoIDs); diff != "" { 167 164 t.Errorf("indexed repoIDs mismatch (-want +got):\n%s", diff) ··· 186 183 } 187 184 188 185 receivedRepoIDs := got.IDs 189 - sort.Slice(receivedRepoIDs, func(i, j int) bool { 190 - return receivedRepoIDs[i] < receivedRepoIDs[j] 191 - }) 186 + slices.Sort(receivedRepoIDs) 192 187 193 188 expectedRepoIDs := []uint32{1, 2, 3} 194 - sort.Slice(expectedRepoIDs, func(i, j int) bool { 195 - return expectedRepoIDs[i] < expectedRepoIDs[j] 196 - }) 189 + slices.Sort(expectedRepoIDs) 197 190 198 191 if diff := cmp.Diff(expectedRepoIDs, receivedRepoIDs); diff != "" { 199 192 t.Errorf("mismatch in list of all repoIDs (-want +got):\n%s", diff)
+2 -2
cmd/zoekt-sourcegraph-indexserver/meta.go
··· 45 45 continue 46 46 } 47 47 48 - var merged interface{} 48 + var merged any 49 49 if md.IndexFormatVersion >= 17 { 50 50 merged = repos 51 51 } else { ··· 82 82 // 83 83 // Note: .tmp is the same suffix used by Builder. indexserver knows to clean 84 84 // them up. 85 - func jsonMarshalTmpFile(v interface{}, p string) (_ string, err error) { 85 + func jsonMarshalTmpFile(v any, p string) (_ string, err error) { 86 86 b, err := json.Marshal(v) 87 87 if err != nil { 88 88 return "", err
+2 -2
cmd/zoekt-sourcegraph-indexserver/owner.go
··· 58 58 return err 59 59 } 60 60 61 - content := []byte(fmt.Sprintf(`DO NOT EDIT! generated by zoekt-sourcegraph-indexserver. 61 + content := fmt.Appendf(nil, `DO NOT EDIT! generated by zoekt-sourcegraph-indexserver. 62 62 This file records the identity of the owner of this zoekt index directory. 63 63 If it changes, zoekt-sourcegraph-indexserver will exit with a non-zero exit code. 64 64 This is to prevent multiple owners/writers. 65 65 66 66 hostname=%s 67 - `, o.Hostname)) 67 + `, o.Hostname) 68 68 69 69 // Always write out since we may update the comment 70 70 if err := os.WriteFile(o.Path, content, 0o600); err != nil {
+2 -2
cmd/zoekt-sourcegraph-indexserver/queue.go
··· 389 389 pq[j].heapIdx = j 390 390 } 391 391 392 - func (pq *pqueue) Push(x interface{}) { 392 + func (pq *pqueue) Push(x any) { 393 393 n := len(*pq) 394 394 item := x.(*queueItem) 395 395 item.heapIdx = n 396 396 *pq = append(*pq, item) 397 397 } 398 398 399 - func (pq *pqueue) Pop() interface{} { 399 + func (pq *pqueue) Pop() any { 400 400 old := *pq 401 401 n := len(old) 402 402 item := old[n-1]
+2 -2
cmd/zoekt-sourcegraph-indexserver/queue_test.go
··· 19 19 backoffDuration := 1 * time.Millisecond 20 20 queue := NewQueue(backoffDuration, backoffDuration, logtest.Scoped(t)) 21 21 22 - for i := 0; i < 100; i++ { 22 + for i := range 100 { 23 23 queue.AddOrUpdate(mkHEADIndexOptions(i, strconv.Itoa(i))) 24 24 } 25 25 ··· 64 64 backoffDuration := 1 * time.Millisecond 65 65 queue := NewQueue(backoffDuration, backoffDuration, logtest.Scoped(t)) 66 66 67 - for i := 0; i < 100; i++ { 67 + for i := range 100 { 68 68 queue.AddOrUpdate(mkHEADIndexOptions(i, strconv.Itoa(i))) 69 69 } 70 70
+2 -2
cmd/zoekt-webserver/grpc/server/sampling_test.go
··· 16 16 } 17 17 fileEvents := func(n int) []*zoekt.SearchResult { 18 18 res := make([]*zoekt.SearchResult, n) 19 - for i := 0; i < n; i++ { 19 + for i := range n { 20 20 res[i] = filesEvent 21 21 } 22 22 return res ··· 26 26 } 27 27 statsEvents := func(n int) []*zoekt.SearchResult { 28 28 res := make([]*zoekt.SearchResult, n) 29 - for i := 0; i < n; i++ { 29 + for i := range n { 30 30 res[i] = statsEvent 31 31 } 32 32 return res
+3 -3
grpc/messagesize/prometheus_test.go
··· 317 317 }{ 318 318 { 319 319 name: "invoker successful - observe request size", 320 - invoker: func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 320 + invoker: func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 321 321 return nil 322 322 }, 323 323 ··· 328 328 329 329 { 330 330 name: "invoker error - observe a zero-sized response", 331 - invoker: func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 331 + invoker: func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 332 332 return sentinelError 333 333 }, 334 334 ··· 356 356 var actualRequest any 357 357 358 358 invokerCalled := false 359 - invoker := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 359 + invoker := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 360 360 invokerCalled = true 361 361 362 362 actualRequest = req
+3 -3
grpc/propagator/propagator.go
··· 27 27 // should be configured with an interceptor that uses the same propagator. 28 28 func StreamServerPropagator(prop Propagator) grpc.StreamServerInterceptor { 29 29 return func( 30 - srv interface{}, 30 + srv any, 31 31 ss grpc.ServerStream, 32 32 info *grpc.StreamServerInfo, 33 33 handler grpc.StreamHandler, ··· 52 52 func UnaryServerPropagator(prop Propagator) grpc.UnaryServerInterceptor { 53 53 return func( 54 54 ctx context.Context, 55 - req interface{}, 55 + req any, 56 56 info *grpc.UnaryServerInfo, 57 57 handler grpc.UnaryHandler, 58 - ) (resp interface{}, err error) { 58 + ) (resp any, err error) { 59 59 md, ok := metadata.FromIncomingContext(ctx) 60 60 if ok { 61 61 ctx, err = prop.InjectContext(ctx, md)
+1 -1
index/bits.go
··· 29 29 variants := make([]ngram, 0, 8) 30 30 cur := asRunes 31 31 for { 32 - for i := 0; i < 3; i++ { 32 + for i := range 3 { 33 33 next := unicode.SimpleFold(cur[i]) 34 34 cur[i] = next 35 35 if next != asRunes[i] {
+4 -3
index/bits_test.go
··· 19 19 "log" 20 20 "math/rand" 21 21 "reflect" 22 - "sort" 23 22 "strconv" 24 23 "testing" 25 24 "testing/quick" 25 + 26 + "slices" 26 27 27 28 "github.com/google/go-cmp/cmp" 28 29 ) ··· 197 198 if len(nums) == 0 { 198 199 return nums 199 200 } 200 - sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] }) 201 + slices.Sort(nums) 201 202 filtered := nums[:1] 202 203 for _, n := range nums[1:] { 203 204 if filtered[len(filtered)-1] != n { ··· 295 296 for size := 10; size <= 10000; size *= 10 { 296 297 ds := make([]DocumentSection, 0, size) 297 298 var last uint32 298 - for i := 0; i < size; i++ { 299 + for range size { 299 300 var d DocumentSection 300 301 last += 1 + uint32(rng.Int31n(200)) 301 302 d.Start = last
+1 -1
index/btree.go
··· 427 427 bucket, _ := b.file.Read(off, sz) 428 428 429 429 // decode all ngrams in the bucket and fill map 430 - for i := 0; i < len(bucket)/ngramEncoding; i++ { 430 + for i := range len(bucket) / ngramEncoding { 431 431 gram := ngram(binary.BigEndian.Uint64(bucket[i*8:])) 432 432 m[gram] = b.getPostingList(int(n.postingIndexOffset) + i) 433 433 }
+1 -1
index/btree_test.go
··· 136 136 bucketSize: bucketSize, 137 137 v: 2, 138 138 }) 139 - for i := 0; i < tt.nNgrams; i++ { 139 + for i := range tt.nNgrams { 140 140 bt.insert(ngram(i + 1)) 141 141 } 142 142 bt.freeze()
+7 -7
index/builder.go
··· 42 42 "github.com/rs/xid" 43 43 "golang.org/x/sys/unix" 44 44 45 + "maps" 46 + 45 47 "github.com/sourcegraph/zoekt" 46 48 "github.com/sourcegraph/zoekt/internal/ctags" 47 49 ) ··· 147 149 hasher := sha1.New() 148 150 149 151 hasher.Write([]byte(h.ctagsPath)) 150 - hasher.Write([]byte(fmt.Sprintf("%t", h.cTagsMustSucceed))) 151 - hasher.Write([]byte(fmt.Sprintf("%d", h.sizeMax))) 152 - hasher.Write([]byte(fmt.Sprintf("%q", h.largeFiles))) 153 - hasher.Write([]byte(fmt.Sprintf("%t", h.disableCTags))) 152 + hasher.Write(fmt.Appendf(nil, "%t", h.cTagsMustSucceed)) 153 + hasher.Write(fmt.Appendf(nil, "%d", h.sizeMax)) 154 + hasher.Write(fmt.Appendf(nil, "%q", h.largeFiles)) 155 + hasher.Write(fmt.Appendf(nil, "%t", h.disableCTags)) 154 156 155 157 return fmt.Sprintf("%x", hasher.Sum(nil)) 156 158 } ··· 658 660 659 661 // map of temporary -> final names for all updated shards + shard metadata files 660 662 artifactPaths := make(map[string]string) 661 - for tmp, final := range b.finishedShards { 662 - artifactPaths[tmp] = final 663 - } 663 + maps.Copy(artifactPaths, b.finishedShards) 664 664 665 665 oldShards := b.opts.FindAllShards() 666 666
+3 -3
index/builder_test.go
··· 274 274 t.Fatalf("NewBuilder: %v", err) 275 275 } 276 276 277 - for i := 0; i < 4; i++ { 277 + for i := range 4 { 278 278 nm := fmt.Sprintf("F%d", i) 279 279 _ = b.AddFile(nm, []byte(strings.Repeat("01234567\n", 128))) 280 280 } ··· 339 339 // Generate a large compound shard 340 340 const numRepos = 5000 341 341 repositories := make([]zoekt.Repository, numRepos) 342 - for i := 0; i < numRepos; i++ { 342 + for i := range numRepos { 343 343 repositories[i] = zoekt.Repository{ 344 344 Name: fmt.Sprintf("repo%d", i+1), 345 345 ID: uint32(i + 1), ··· 901 901 numShards = 1 902 902 } 903 903 904 - for i := 0; i < numShards; i++ { 904 + for i := range numShards { 905 905 // Create entries (file + contents) that are ~100 bytes each. 906 906 // This (along with our shardMax setting of 75 bytes) means that each shard 907 907 // will contain at most one of these.
+1 -4
index/contentprovider.go
··· 355 355 }) 356 356 } 357 357 358 - firstLineNumber := int(chunk.firstLine) - numContextLines 359 - if firstLineNumber < 1 { 360 - firstLineNumber = 1 361 - } 358 + firstLineNumber := max(int(chunk.firstLine)-numContextLines, 1) 362 359 firstLineStart := newlines.lineStart(firstLineNumber) 363 360 364 361 chunkScore, symbolInfo := p.scoreChunk(chunk.candidates, language, opts)
+4 -3
index/hititer_test.go
··· 18 18 "fmt" 19 19 "math/rand" 20 20 "reflect" 21 - "sort" 22 21 "testing" 23 22 "testing/quick" 23 + 24 + "slices" 24 25 25 26 "github.com/google/go-cmp/cmp" 26 27 "github.com/sourcegraph/zoekt" ··· 33 34 } 34 35 35 36 nums = sortedUnique(nums) 36 - sort.Slice(limits, func(i, j int) bool { return limits[i] < limits[j] }) 37 + slices.Sort(limits) 37 38 38 39 want := doHitIterator(&inMemoryIterator{postings: nums}, limits) 39 40 ··· 82 83 limits := genUints32(limitsSize) 83 84 84 85 nums = sortedUnique(nums) 85 - sort.Slice(limits, func(i, j int) bool { return limits[i] < limits[j] }) 86 + slices.Sort(limits) 86 87 87 88 ng := stringToNGram("abc") 88 89 deltas := toDeltas(nums)
+3 -3
index/index_test.go
··· 1164 1164 1165 1165 func TestDocumentOrder(t *testing.T) { 1166 1166 var docs []Document 1167 - for i := 0; i < 3; i++ { 1167 + for i := range 3 { 1168 1168 docs = append(docs, Document{Name: fmt.Sprintf("f%d", i), Content: []byte("needle")}) 1169 1169 } 1170 1170 ··· 1260 1260 func TestBranchLimit(t *testing.T) { 1261 1261 for limit := 64; limit <= 65; limit++ { 1262 1262 r := &zoekt.Repository{} 1263 - for i := 0; i < limit; i++ { 1263 + for i := range limit { 1264 1264 s := fmt.Sprintf("b%d", i) 1265 1265 r.Branches = append(r.Branches, zoekt.RepositoryBranch{ 1266 1266 s, "v-" + s, ··· 3953 3953 func BenchmarkScoreChunkMatches(b *testing.B) { 3954 3954 ctx := context.Background() 3955 3955 var builder strings.Builder 3956 - for i := 0; i < 1000; i++ { 3956 + for i := range 1000 { 3957 3957 builder.WriteString(fmt.Sprintf("line-%d one one one two two two three three three four four four five five\n", i)) 3958 3958 } 3959 3959
+1 -1
index/limit_test.go
··· 81 81 Start: zoekt.Location{LineNumber: uint32(lineNumber + (2 * i) + 1)}, 82 82 End: zoekt.Location{LineNumber: uint32(lineNumber + (2 * i) + 2)}, 83 83 }) 84 - cm.Content = append(cm.Content, []byte(fmt.Sprintf("range%dStart\nrange%dEnd\n", i, i))...) 84 + cm.Content = append(cm.Content, fmt.Appendf(nil, "range%dStart\nrange%dEnd\n", i, i)...) 85 85 } 86 86 // 1 line of context. Content in zoekt notably just does not 87 87 // contain a trailing newline.
+1 -1
index/matchtree.go
··· 1057 1057 case *query.Branch: 1058 1058 masks := make([]uint64, 0, len(d.repoMetaData)) 1059 1059 if s.Pattern == "HEAD" { 1060 - for i := 0; i < len(d.repoMetaData); i++ { 1060 + for range d.repoMetaData { 1061 1061 masks = append(masks, 1) 1062 1062 } 1063 1063 } else {
+4 -4
index/matchtree_test.go
··· 297 297 t.Fatal(err) 298 298 } 299 299 want := []uint32{2, 4, 5} 300 - for i := 0; i < len(want); i++ { 300 + for i := range want { 301 301 nextDoc := mt.nextDoc() 302 302 if nextDoc != want[i] { 303 303 t.Fatalf("want %d, got %d", want[i], nextDoc) ··· 320 320 t.Fatal(err) 321 321 } 322 322 want := []uint32{2, 4} 323 - for i := 0; i < len(want); i++ { 323 + for i := range want { 324 324 nextDoc := mt.nextDoc() 325 325 if nextDoc != want[i] { 326 326 t.Fatalf("want %d, got %d", want[i], nextDoc) ··· 352 352 } 353 353 354 354 want := []uint32{3, 5} 355 - for i := 0; i < len(want); i++ { 355 + for i := range want { 356 356 nextDoc := mt.nextDoc() 357 357 if nextDoc != want[i] { 358 358 t.Fatalf("want %d, got %d", want[i], nextDoc) ··· 376 376 t.Fatal(err) 377 377 } 378 378 want := []uint32{2, 4, 5} 379 - for i := 0; i < len(want); i++ { 379 + for i := range want { 380 380 nextDoc := mt.nextDoc() 381 381 if nextDoc != want[i] { 382 382 t.Fatalf("want %d, got %d", want[i], nextDoc)
+2 -2
index/read.go
··· 236 236 return arr, nil 237 237 } 238 238 239 - func (r *reader) readJSON(data interface{}, sec simpleSection) error { 239 + func (r *reader) readJSON(data any, sec simpleSection) error { 240 240 blob, err := r.r.Read(sec.off, sec.sz) 241 241 if err != nil { 242 242 return err ··· 383 383 d.fileNameRuneOffsets = makeRuneOffsetMap(fileNameRuneOffsets) 384 384 385 385 d.subRepoPaths = make([][]string, 0, len(d.repoMetaData)) 386 - for i := 0; i < len(d.repoMetaData); i++ { 386 + for i := range d.repoMetaData { 387 387 keys := make([]string, 0, len(d.repoMetaData[i].SubRepoMap)+1) 388 388 keys = append(keys, "") 389 389 for k := range d.repoMetaData[i].SubRepoMap {
+3 -1
index/shard_builder.go
··· 29 29 "time" 30 30 "unicode/utf8" 31 31 32 + "slices" 33 + 32 34 "github.com/sourcegraph/zoekt" 33 35 "github.com/sourcegraph/zoekt/internal/languages" 34 36 ) ··· 234 236 // 235 237 // [1]: https://sourcegraph.com/github.com/golang/go@go1.23.2/-/blob/src/html/template/html.go?L71-80 236 238 // [2]: https://github.com/apple/swift-system/blob/main/Sources/System/Util+StringArray.swift 237 - elem = append([]string{}, elem...) // copy to mutate 239 + elem = slices.Clone(elem) // copy to mutate 238 240 for i := range elem { 239 241 elem[i] = strings.ReplaceAll(elem[i], "+", "%2B") 240 242 }
+1 -1
index/tombstones.go
··· 60 60 // The caller is responsible for renaming the temporary file to the final file path, or removing 61 61 // the temporary file if it is no longer needed. 62 62 // TODO: Should we stick this in a util package? 63 - func JsonMarshalRepoMetaTemp(shardPath string, repositoryMetadata interface{}) (tempPath, finalPath string, err error) { 63 + func JsonMarshalRepoMetaTemp(shardPath string, repositoryMetadata any) (tempPath, finalPath string, err error) { 64 64 finalPath = shardPath + ".meta" 65 65 66 66 b, err := json.Marshal(repositoryMetadata)
+1 -1
index/write.go
··· 229 229 return w.err 230 230 } 231 231 232 - func (b *ShardBuilder) writeJSON(data interface{}, sec *simpleSection, w *writer) error { 232 + func (b *ShardBuilder) writeJSON(data any, sec *simpleSection, w *writer) error { 233 233 blob, err := json.Marshal(data) 234 234 if err != nil { 235 235 return err
+2 -2
internal/archive/e2e_test.go
··· 117 117 fileSize := 1000 118 118 119 119 files := map[string]string{} 120 - for i := 0; i < 4; i++ { 120 + for i := range 4 { 121 121 s := fmt.Sprintf("%d", i) 122 122 files["F"+s] = strings.Repeat("a", fileSize) 123 123 files["!F"+s] = strings.Repeat("a", fileSize) ··· 219 219 220 220 fileSize := 10 221 221 files := map[string]string{} 222 - for i := 0; i < 4; i++ { 222 + for i := range 4 { 223 223 s := fmt.Sprintf("%d", i) 224 224 files["F"+s] = strings.Repeat("a", fileSize) 225 225 files["!F"+s] = strings.Repeat("a", fileSize)
+9 -9
internal/e2e/e2e_index_test.go
··· 59 59 t.Fatalf("NewBuilder: %v", err) 60 60 } 61 61 62 - for i := 0; i < 4; i++ { 62 + for i := range 4 { 63 63 s := fmt.Sprintf("%d", i) 64 64 if err := b.AddFile("F"+s, []byte(strings.Repeat(s, 1000))); err != nil { 65 65 t.Fatal(err) ··· 121 121 t.Run("meta file", func(t *testing.T) { 122 122 // use retryTest to allow for the directory watcher to notice the meta 123 123 // file 124 - retryTest(t, func(fatalf func(format string, args ...interface{})) { 124 + retryTest(t, func(fatalf func(format string, args ...any)) { 125 125 // Add a .meta file for each shard with repo.Name set to 126 126 // "repo-mutated". We do this inside retry helper since we have noticed 127 127 // some flakiness on github CI. ··· 182 182 t.Fatalf("NewBuilder: %v", err) 183 183 } 184 184 185 - for i := 0; i < 4; i++ { 185 + for i := range 4 { 186 186 s := fmt.Sprintf("%d", i) 187 187 if err := b.AddFile("F"+s, []byte(strings.Repeat(s, 1000))); err != nil { 188 188 t.Fatal(err) ··· 291 291 292 292 // retryTest will retry f until min(t.Deadline(), time.Minute). It returns 293 293 // once f doesn't call fatalf. 294 - func retryTest(t *testing.T, f func(fatalf func(format string, args ...interface{}))) { 294 + func retryTest(t *testing.T, f func(fatalf func(format string, args ...any))) { 295 295 t.Helper() 296 296 297 297 sleep := 10 * time.Millisecond ··· 306 306 go func() { 307 307 defer close(done) 308 308 309 - f(func(format string, args ...interface{}) { 309 + f(func(format string, args ...any) { 310 310 runtime.Goexit() 311 311 }) 312 312 ··· 348 348 t.Fatalf("NewBuilder: %v", err) 349 349 } 350 350 351 - for i := 0; i < 4; i++ { 351 + for i := range 4 { 352 352 s := fmt.Sprintf("%d", i) 353 353 if err := b.AddFile("F"+s, []byte(strings.Repeat("a", sizeMax+1))); err != nil { 354 354 t.Fatal(err) ··· 493 493 if err != nil { 494 494 t.Fatalf("NewBuilder: %v", err) 495 495 } 496 - for i := 0; i < 4; i++ { 496 + for i := range 4 { 497 497 s := fmt.Sprintf("%d\n", i) 498 498 if err := b.AddFile("F"+s, []byte(strings.Repeat(s, 1024/2))); err != nil { 499 499 t.Errorf("AddFile: %v", err) ··· 524 524 if err != nil { 525 525 t.Fatalf("NewBuilder: %v", err) 526 526 } 527 - for i := 0; i < 4; i++ { 527 + for i := range 4 { 528 528 s := fmt.Sprintf("%d\n", i) 529 529 if err := b.AddFile("F"+s, []byte(strings.Repeat(s, 1024/2))); err != nil { 530 530 t.Fatal(err) ··· 749 749 } 750 750 751 751 // Call b.Finish() multiple times to ensure that it is idempotent 752 - for i := 0; i < 3; i++ { 752 + for i := range 3 { 753 753 754 754 err = b.Finish() 755 755 if err != nil {
+4 -6
internal/shards/aggregate.go
··· 5 5 "sync" 6 6 "time" 7 7 8 + "maps" 9 + 8 10 "github.com/prometheus/client_golang/prometheus" 9 11 "github.com/prometheus/client_golang/prometheus/promauto" 10 12 "github.com/sourcegraph/zoekt" ··· 48 50 49 51 c.aggregate.Files = index.SortAndTruncateFiles(c.aggregate.Files, c.opts) 50 52 51 - for k, v := range r.RepoURLs { 52 - c.aggregate.RepoURLs[k] = v 53 - } 54 - for k, v := range r.LineFragments { 55 - c.aggregate.LineFragments[k] = v 56 - } 53 + maps.Copy(c.aggregate.RepoURLs, r.RepoURLs) 54 + maps.Copy(c.aggregate.LineFragments, r.LineFragments) 57 55 } 58 56 59 57 // The priority of our aggregate is the largest priority we collect.
+1 -1
internal/shards/sched_test.go
··· 138 138 }() 139 139 140 140 // Fill up interactive queue 141 - for i := 0; i < capacity; i++ { 141 + for range capacity { 142 142 addProc() 143 143 } 144 144
+3 -6
internal/shards/shards.go
··· 690 690 691 691 // We set the number of workers to GOMAXPROCS, or the number of shards, 692 692 // whichever is smaller. 693 - workers := runtime.GOMAXPROCS(0) 694 - if workers > len(shards) { 695 - workers = len(shards) 696 - } 693 + workers := min(runtime.GOMAXPROCS(0), len(shards)) 697 694 698 695 type result struct { 699 696 priority float64 ··· 716 713 // Note: Making "search" a buffered channel has the effect of limiting the number of parallel shard searches. 717 714 // Since searching is mostly CPU bound, limiting parallel shard searches also reduces the peak working set. 718 715 wg.Add(workers) 719 - for i := 0; i < workers; i++ { 716 + for range workers { 720 717 go func() { 721 718 defer wg.Done() 722 719 for s := range search { ··· 996 993 } 997 994 close(feeder) 998 995 999 - for i := 0; i < runtime.GOMAXPROCS(0); i++ { 996 + for range runtime.GOMAXPROCS(0) { 1000 997 go func() { 1001 998 for s := range feeder { 1002 999 listOneShard(ctx, s, q, opts, all)
+5 -5
internal/shards/shards_test.go
··· 159 159 ss := newShardedSearcher(1) 160 160 161 161 n := 10 * runtime.GOMAXPROCS(0) 162 - for i := 0; i < n; i++ { 162 + for i := range n { 163 163 ss.replace(map[string]zoekt.Searcher{ 164 164 fmt.Sprintf("shard%d", i): &rankSearcher{rank: uint16(i)}, 165 165 }) ··· 287 287 288 288 files := results[1].Files 289 289 got := make([]string, len(files)) 290 - for i := 0; i < len(files); i++ { 290 + for i := range files { 291 291 got[i] = files[i].FileName 292 292 } 293 293 ··· 306 306 repoSetNames := []string{} 307 307 repoIDs := []uint32{} 308 308 n := 10 * runtime.GOMAXPROCS(0) 309 - for i := 0; i < n; i++ { 309 + for i := range n { 310 310 shardName := fmt.Sprintf("shard%d", i) 311 311 repoName := fmt.Sprintf("%s-repository%.3d", namePrefix[i%3], i) 312 312 repoID := hash(repoName) ··· 639 639 } 640 640 641 641 func reposForTest(n int) (result []*zoekt.Repository) { 642 - for i := 0; i < n; i++ { 642 + for i := range n { 643 643 result = append(result, &zoekt.Repository{ 644 644 ID: uint32(i + 1), 645 645 Name: fmt.Sprintf("test-repository-%d", i), ··· 970 970 return &zoekt.SearchResult{} 971 971 } 972 972 fm := make([]zoekt.FileMatch, 0, n) 973 - for i := 0; i < n; i++ { 973 + for range n { 974 974 fm = append(fm, zoekt.FileMatch{Repository: fmt.Sprintf("repo%d", repoID), RepositoryID: repoID}) 975 975 } 976 976
+3 -3
internal/trace/trace.go
··· 57 57 // LazyPrintf evaluates its arguments with fmt.Sprintf each time the 58 58 // /debug/requests page is rendered. Any memory referenced by a will be 59 59 // pinned until the trace is finished and later discarded. 60 - func (t *Trace) LazyPrintf(format string, a ...interface{}) { 60 + func (t *Trace) LazyPrintf(format string, a ...any) { 61 61 t.span.LogFields(Printf("log", format, a...)) 62 62 t.trace.LazyPrintf(format, a...) 63 63 } ··· 94 94 // Printf is an opentracing log.Field which is a LazyLogger. So the format 95 95 // string will only be evaluated if the trace is collected. In the case of 96 96 // net/trace, it will only be evaluated on page load. 97 - func Printf(key, f string, args ...interface{}) log.Field { 97 + func Printf(key, f string, args ...any) log.Field { 98 98 return log.Lazy(func(fv log.Encoder) { 99 99 fv.EmitString(key, fmt.Sprintf(f, args...)) 100 100 }) ··· 184 184 e.EmitString(key, strconv.FormatFloat(value, 'E', -1, 64)) 185 185 } 186 186 187 - func (e *encoder) EmitObject(key string, value interface{}) { 187 + func (e *encoder) EmitObject(key string, value any) { 188 188 e.EmitString(key, fmt.Sprintf("%+v", value)) 189 189 } 190 190
+1 -1
internal/tracer/jaeger.go
··· 46 46 } 47 47 48 48 // Infof logs a message at info priority 49 - func (l *jaegerLogger) Infof(msg string, args ...interface{}) { 49 + func (l *jaegerLogger) Infof(msg string, args ...any) { 50 50 log.Printf(msg, args...) 51 51 }
+4 -3
marshal.go
··· 4 4 "bytes" 5 5 "encoding/binary" 6 6 "fmt" 7 + "slices" 7 8 "unsafe" 8 9 ) 9 10 ··· 106 107 // don't own b, so we create a copy of it. 107 108 r := binaryReader{ 108 109 typ: "ReposMap", 109 - b: append([]byte{}, b...), 110 + b: slices.Clone(b), 110 111 } 111 112 112 113 // Version ··· 129 130 allBranchesLen := r.uvarint() 130 131 allBranches := make([]RepositoryBranch, 0, allBranchesLen) 131 132 132 - for i := 0; i < l; i++ { 133 + for range l { 133 134 repoID := r.uvarint() 134 135 hasSymbols := r.byt() == 1 135 136 var indexTimeUnix int64 ··· 137 138 indexTimeUnix = int64(r.uvarint()) 138 139 } 139 140 lb := r.uvarint() 140 - for i := 0; i < lb; i++ { 141 + for range lb { 141 142 allBranches = append(allBranches, RepositoryBranch{ 142 143 Name: r.str(), 143 144 Version: r.str(),
+1 -1
marshal_test.go
··· 74 74 func genRepoList(size int) *RepoList { 75 75 m := make(ReposMap, size) 76 76 indexTime := time.Now().Unix() 77 - for i := 0; i < size; i++ { 77 + for i := range size { 78 78 m[uint32(i)] = MinimalRepoListEntry{ 79 79 HasSymbols: true, 80 80 IndexTimeUnix: indexTime,
+5 -3
query/marshal.go
··· 8 8 "io" 9 9 "unsafe" 10 10 11 + "slices" 12 + 11 13 "github.com/RoaringBitmap/roaring" 12 14 ) 13 15 ··· 75 77 l := r.uvarint() // Length 76 78 brs := make([]BranchRepos, l) 77 79 78 - for i := 0; i < l; i++ { 80 + for i := range l { 79 81 brs[i].Branch = r.str() 80 82 brs[i].Repos = r.bitmap() 81 83 } ··· 157 159 func stringSetDecode(b []byte) (map[string]struct{}, error) { 158 160 // binaryReader returns strings pointing into b to avoid allocations. We 159 161 // don't own b, so we create a copy of it. 160 - r := binaryReader{b: append([]byte{}, b...)} 162 + r := binaryReader{b: slices.Clone(b)} 161 163 162 164 // Version 163 165 if v := r.byt(); v != 1 { ··· 168 170 l := r.uvarint() 169 171 set := make(map[string]struct{}, l) 170 172 171 - for i := 0; i < l; i++ { 173 + for range l { 172 174 set[r.str()] = struct{}{} 173 175 } 174 176
+3 -3
query/marshal_test.go
··· 163 163 164 164 func genFileNameSet(size int) *FileNameSet { 165 165 set := make(map[string]struct{}, size) 166 - for i := 0; i < size; i++ { 166 + for i := range size { 167 167 set[genName(i)] = struct{}{} 168 168 } 169 169 return &FileNameSet{Set: set} 170 170 } 171 171 172 172 // Generating 5.5M repos slows down the benchmark setup time, so we cache things. 173 - var genCache = map[string]interface{}{} 173 + var genCache = map[string]any{} 174 174 175 175 func genRepoBranches(n int) map[string][]string { 176 176 repoBranches := map[string][]string{} 177 177 orgIndex := 0 178 178 repoIndex := 0 179 179 180 - for i := 0; i < n; i++ { 180 + for i := range n { 181 181 org := genName(orgIndex) 182 182 name := "github.com/" + org + "/" + genName(orgIndex*2+repoIndex) 183 183 repoBranches[name] = []string{"HEAD"}
+3 -7
query/regexp.go
··· 18 18 "log" 19 19 "regexp/syntax" 20 20 21 + "slices" 22 + 21 23 "github.com/sourcegraph/zoekt/internal/syntaxutil" 22 24 ) 23 25 ··· 81 83 return true 82 84 } 83 85 84 - for _, s := range r.Sub { 85 - if hasCapture(s) { 86 - return true 87 - } 88 - } 89 - 90 - return false 86 + return slices.ContainsFunc(r.Sub, hasCapture) 91 87 } 92 88 93 89 func uncapture(r *syntax.Regexp) *syntax.Regexp {
+2 -2
web/e2e_test.go
··· 806 806 t.Fatalf("NewShardBuilder: %v", err) 807 807 } 808 808 809 - for i := 0; i < 2; i++ { 809 + for i := range 2 { 810 810 if err := b.Add(index.Document{ 811 811 Name: fmt.Sprintf("file%d", i), 812 812 Content: []byte("bla"), ··· 912 912 t.Fatalf("NewShardBuilder: %v", err) 913 913 } 914 914 915 - for i := 0; i < 2; i++ { 915 + for i := range 2 { 916 916 if err := b.Add(index.Document{ 917 917 Name: fmt.Sprintf("file%d", i), 918 918 Content: []byte("bla"),