Skip to content

Commit 64d6a14

Browse files
committed
Steal files between checkers when checking
Files are assigned to checkers up front, so the checker with the most work left finishes last while the others sit idle. Results no longer depend on which checker checks a file or which files it checked before, so a checker that runs out of files can take one from another without changing any output. Each checker still starts with the files assigned to it, keeping the locality of the assignment, and takes the file another checker would reach last. A file stays associated with the checker that checked it, so later requests for it reuse that checker's caches.
1 parent dca6039 commit 64d6a14

1 file changed

Lines changed: 109 additions & 20 deletions

File tree

‎tsc/internal/compiler/checkerpool.go‎

Lines changed: 109 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"slices"
77
"sort"
88
"sync"
9+
"sync/atomic"
910

1011
"github.com/microsoft/TypeScript/tsc/internal/ast"
1112
"github.com/microsoft/TypeScript/tsc/internal/checker"
@@ -32,8 +33,9 @@ type checkerPool struct {
3233
createCheckersOnce sync.Once
3334
checkers []*checker.Checker
3435
locks []*sync.Mutex
35-
fileAssociations map[*ast.SourceFile]*checker.Checker
36-
associations []int // from ProgramFactories.AssignCheckers, if set
36+
fileIndices map[*ast.SourceFile]int
37+
fileCheckers []atomic.Int32 // Index in checkers of the checker of each file, by file index
38+
associations []int // from ProgramFactories.AssignCheckers, if set
3739
}
3840

3941
var _ CheckerPool = (*checkerPool)(nil)
@@ -361,13 +363,19 @@ func (p *checkerPool) GetChecker(ctx context.Context, file *ast.SourceFile) (*ch
361363
// e.g. for read-only operations like obtaining an emit resolver.
362364
func (p *checkerPool) getCheckerForFileNonExclusive(file *ast.SourceFile) (*checker.Checker, func()) {
363365
p.createCheckers()
364-
return p.fileAssociations[file], noop
366+
return p.checkers[p.checkerIndexOf(file)], noop
367+
}
368+
369+
// checkerIndexOf returns the index in checkers of the checker the file is
370+
// associated with: the one that checked it, or that will.
371+
func (p *checkerPool) checkerIndexOf(file *ast.SourceFile) int {
372+
return int(p.fileCheckers[p.fileIndices[file]].Load())
365373
}
366374

367375
func (p *checkerPool) getCheckerForFileExclusive(ctx context.Context, file *ast.SourceFile) (*checker.Checker, func()) {
368376
p.createCheckers()
369-
c := p.fileAssociations[file]
370-
idx := slices.Index(p.checkers, c)
377+
idx := p.checkerIndexOf(file)
378+
c := p.checkers[idx]
371379
p.locks[idx].Lock()
372380
return c, sync.OnceFunc(func() {
373381
p.locks[idx].Unlock()
@@ -431,9 +439,11 @@ func (p *checkerPool) createCheckers() {
431439
fileOrder := getCheckerAssociationOrder(fileWeights, isDeclarationFile, policy.prioritizeSourceFiles)
432440
associations = getCheckerAssociationsInOrder(fileWeights, adjacentFiles, fileOrder, checkerCount, policy.balancePenaltyMultiplier)
433441
}
434-
p.fileAssociations = make(map[*ast.SourceFile]*checker.Checker, len(p.program.files))
442+
p.fileIndices = make(map[*ast.SourceFile]int, len(p.program.files))
443+
p.fileCheckers = make([]atomic.Int32, len(p.program.files))
435444
for i, file := range p.program.files {
436-
p.fileAssociations[file] = p.checkers[associations[i]]
445+
p.fileIndices[file] = i
446+
p.fileCheckers[i].Store(int32(associations[i]))
437447
}
438448
})
439449
}
@@ -489,36 +499,115 @@ func (p *checkerPool) GetGlobalDiagnostics() []*ast.Diagnostic {
489499
return SortAndDeduplicateDiagnostics(slices.Concat(globalDiagnostics...))
490500
}
491501

492-
// forEachCheckerGroupDo runs one task per checker in parallel. Each task iterates
493-
// the provided files, processing only those assigned to its checker. Within each
494-
// checker's set, files are visited in their original order.
502+
// forEachCheckerGroupDo runs one task per checker in parallel, each checking
503+
// first the files associated with it, in their original order. A task that
504+
// runs out of files takes the last file not yet started from the checker with
505+
// the most left, and the file is associated with its checker from then on.
506+
// Which checker checks a file and what it checked before do not change the
507+
// results, only how much each checker can reuse.
495508
func (p *checkerPool) forEachCheckerGroupDo(ctx context.Context, files []*ast.SourceFile, singleThreaded bool, cb func(c *checker.Checker, fileIndex int, file *ast.SourceFile)) {
496-
p.createCheckers()
497-
498-
checkerCount := len(p.checkers)
499-
wg := core.NewWorkGroup(core.WorkGroupConcurrency(singleThreaded))
500-
for checkerIdx := range checkerCount {
509+
groups := p.groupFilesByChecker(files)
510+
if singleThreaded || len(p.checkers) == 1 {
511+
// Tasks run one after another, so the first would take every file.
512+
for checkerIdx, group := range groups {
513+
c := p.checkers[checkerIdx]
514+
p.locks[checkerIdx].Lock()
515+
for _, i := range group {
516+
cb(c, i, files[i])
517+
}
518+
p.locks[checkerIdx].Unlock()
519+
}
520+
return
521+
}
522+
queues := make([]fileQueue, len(groups))
523+
for i, group := range groups {
524+
queues[i].files = group
525+
}
526+
wg := core.NewWorkGroup(core.WorkGroupConcurrency(false))
527+
for checkerIdx := range groups {
501528
wg.Queue(func() {
529+
c := p.checkers[checkerIdx]
502530
p.locks[checkerIdx].Lock()
503531
defer p.locks[checkerIdx].Unlock()
504-
for i, file := range files {
505-
if checker := p.checkers[checkerIdx]; checker == p.fileAssociations[file] {
506-
cb(checker, i, file)
532+
for {
533+
i, ok := queues[checkerIdx].popFront()
534+
if !ok {
535+
i, ok = stealFile(queues)
536+
if !ok {
537+
return
538+
}
539+
p.fileCheckers[p.fileIndices[files[i]]].Store(int32(checkerIdx))
507540
}
541+
cb(c, i, files[i])
508542
}
509543
})
510544
}
511545
wg.RunAndWait()
512546
}
513547

548+
// A fileQueue holds the indices of the files a checker has yet to start.
549+
type fileQueue struct {
550+
mu sync.Mutex
551+
files []int
552+
}
553+
554+
func (q *fileQueue) popFront() (int, bool) {
555+
q.mu.Lock()
556+
defer q.mu.Unlock()
557+
if len(q.files) == 0 {
558+
return 0, false
559+
}
560+
i := q.files[0]
561+
q.files = q.files[1:]
562+
return i, true
563+
}
564+
565+
func (q *fileQueue) popBack() (int, bool) {
566+
q.mu.Lock()
567+
defer q.mu.Unlock()
568+
if len(q.files) == 0 {
569+
return 0, false
570+
}
571+
i := q.files[len(q.files)-1]
572+
q.files = q.files[:len(q.files)-1]
573+
return i, true
574+
}
575+
576+
func (q *fileQueue) len() int {
577+
q.mu.Lock()
578+
defer q.mu.Unlock()
579+
return len(q.files)
580+
}
581+
582+
// stealFile takes the last file of the queue with the most files left, the
583+
// one its checker would reach last.
584+
func stealFile(queues []fileQueue) (int, bool) {
585+
for {
586+
victim, most := -1, 0
587+
for i := range queues {
588+
if n := queues[i].len(); n > most {
589+
victim, most = i, n
590+
}
591+
}
592+
if victim < 0 {
593+
return 0, false
594+
}
595+
if i, ok := queues[victim].popBack(); ok {
596+
return i, true
597+
}
598+
}
599+
}
600+
514601
// groupFilesByChecker returns the indices of files grouped by the checker each file is associated with. Files keep
515602
// their order within each group.
516603
func (p *checkerPool) groupFilesByChecker(files []*ast.SourceFile) [][]int {
517604
p.createCheckers()
518605
groups := make([][]int, len(p.checkers))
519606
for i, file := range files {
520-
idx := slices.Index(p.checkers, p.fileAssociations[file])
521-
groups[idx] = append(groups[idx], i)
607+
if j, ok := p.fileIndices[file]; ok {
608+
idx := int(p.fileCheckers[j].Load())
609+
groups[idx] = append(groups[idx], i)
610+
}
522611
}
523612
return groups
524613
}

0 commit comments

Comments
 (0)