|
8 | 8 | "sort"
|
9 | 9 | "strings"
|
10 | 10 | "sync"
|
| 11 | + "sync/atomic" |
11 | 12 | "time"
|
12 | 13 |
|
13 | 14 | "github.com/golangci/golangci-lint/pkg/logutils"
|
@@ -305,33 +306,51 @@ func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages.
|
305 | 306 | perPkgIssues[i.Pkg] = append(perPkgIssues[i.Pkg], *i)
|
306 | 307 | }
|
307 | 308 |
|
308 |
| - savedIssuesCount := 0 |
| 309 | + savedIssuesCount := int32(0) |
309 | 310 | lintResKey := getIssuesCacheKey(analyzers)
|
| 311 | + |
| 312 | + workerCount := runtime.GOMAXPROCS(-1) |
| 313 | + var wg sync.WaitGroup |
| 314 | + wg.Add(workerCount) |
| 315 | + |
| 316 | + pkgCh := make(chan *packages.Package, len(allPkgs)) |
| 317 | + for i := 0; i < workerCount; i++ { |
| 318 | + go func() { |
| 319 | + defer wg.Done() |
| 320 | + for pkg := range pkgCh { |
| 321 | + pkgIssues := perPkgIssues[pkg] |
| 322 | + encodedIssues := make([]EncodingIssue, 0, len(pkgIssues)) |
| 323 | + for ind := range pkgIssues { |
| 324 | + i := &pkgIssues[ind] |
| 325 | + encodedIssues = append(encodedIssues, EncodingIssue{ |
| 326 | + FromLinter: i.FromLinter, |
| 327 | + Text: i.Text, |
| 328 | + Pos: i.Pos, |
| 329 | + LineRange: i.LineRange, |
| 330 | + Replacement: i.Replacement, |
| 331 | + }) |
| 332 | + } |
| 333 | + |
| 334 | + atomic.AddInt32(&savedIssuesCount, int32(len(encodedIssues))) |
| 335 | + if err := lintCtx.PkgCache.Put(pkg, lintResKey, encodedIssues); err != nil { |
| 336 | + lintCtx.Log.Infof("Failed to save package %s issues (%d) to cache: %s", pkg, len(pkgIssues), err) |
| 337 | + } else { |
| 338 | + issuesCacheDebugf("Saved package %s issues (%d) to cache", pkg, len(pkgIssues)) |
| 339 | + } |
| 340 | + } |
| 341 | + }() |
| 342 | + } |
| 343 | + |
310 | 344 | for _, pkg := range allPkgs {
|
311 | 345 | if pkgsFromCache[pkg] {
|
312 | 346 | continue
|
313 | 347 | }
|
314 | 348 |
|
315 |
| - pkgIssues := perPkgIssues[pkg] |
316 |
| - encodedIssues := make([]EncodingIssue, 0, len(pkgIssues)) |
317 |
| - for ind := range pkgIssues { |
318 |
| - i := &pkgIssues[ind] |
319 |
| - encodedIssues = append(encodedIssues, EncodingIssue{ |
320 |
| - FromLinter: i.FromLinter, |
321 |
| - Text: i.Text, |
322 |
| - Pos: i.Pos, |
323 |
| - LineRange: i.LineRange, |
324 |
| - Replacement: i.Replacement, |
325 |
| - }) |
326 |
| - } |
327 |
| - |
328 |
| - savedIssuesCount += len(encodedIssues) |
329 |
| - if err := lintCtx.PkgCache.Put(pkg, lintResKey, encodedIssues); err != nil { |
330 |
| - lintCtx.Log.Infof("Failed to save package %s issues (%d) to cache: %s", pkg, len(pkgIssues), err) |
331 |
| - } else { |
332 |
| - issuesCacheDebugf("Saved package %s issues (%d) to cache", pkg, len(pkgIssues)) |
333 |
| - } |
| 349 | + pkgCh <- pkg |
334 | 350 | }
|
| 351 | + close(pkgCh) |
| 352 | + wg.Wait() |
| 353 | + |
335 | 354 | issuesCacheDebugf("Saved %d issues from %d packages to cache in %s", savedIssuesCount, len(allPkgs), time.Since(startedAt))
|
336 | 355 | }
|
337 | 356 |
|
|
0 commit comments