Skip to content

Commit 232406e

Browse files
committed
Steal files between checkers when emitting
Emit transforms the files of each checker in one task, so as with checking, the checker with the most files left finishes last. Results do not depend on which checker transforms a file, so a task that runs out of files takes one from another checker, as checking does. Emit does not hold the checker's lock, as before: the emit resolver takes it for each call, since a program may emit files concurrently.
1 parent 1ad29d5 commit 232406e

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

‎tsc/internal/compiler/checkerpool.go‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,9 @@ func (p *checkerPool) GetGlobalDiagnostics() []*ast.Diagnostic {
499499
return SortAndDeduplicateDiagnostics(slices.Concat(globalDiagnostics...))
500500
}
501501

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.
502+
// forEachCheckerGroupDo runs one task per checker in parallel, each processing
503+
// the files associated with it in their original order, and then files taken
504+
// from other checkers; see forEachFileStealing.
508505
func (p *checkerPool) forEachCheckerGroupDo(ctx context.Context, files []*ast.SourceFile, singleThreaded bool, cb func(c *checker.Checker, fileIndex int, file *ast.SourceFile)) {
509506
groups := p.groupFilesByChecker(files)
510507
if singleThreaded || len(p.checkers) == 1 {
@@ -519,6 +516,19 @@ func (p *checkerPool) forEachCheckerGroupDo(ctx context.Context, files []*ast.So
519516
}
520517
return
521518
}
519+
p.forEachFileStealing(files, groups, true /*lock*/, func(c *checker.Checker, i int) {
520+
cb(c, i, files[i])
521+
})
522+
}
523+
524+
// forEachFileStealing runs one task per checker in parallel, each running fn
525+
// with its checker for the indices of files in its group, in order, holding
526+
// the checker's lock if lock is set. A task
527+
// that runs out of files takes the last file not yet started from the group
528+
// with the most left, and the file is associated with its checker from then
529+
// on. Which checker processes a file and what it processed before do not
530+
// change the results, only how much each checker can reuse.
531+
func (p *checkerPool) forEachFileStealing(files []*ast.SourceFile, groups [][]int, lock bool, fn func(c *checker.Checker, i int)) {
522532
queues := make([]fileQueue, len(groups))
523533
for i, group := range groups {
524534
queues[i].files = group
@@ -527,8 +537,10 @@ func (p *checkerPool) forEachCheckerGroupDo(ctx context.Context, files []*ast.So
527537
for checkerIdx := range groups {
528538
wg.Queue(func() {
529539
c := p.checkers[checkerIdx]
530-
p.locks[checkerIdx].Lock()
531-
defer p.locks[checkerIdx].Unlock()
540+
if lock {
541+
p.locks[checkerIdx].Lock()
542+
defer p.locks[checkerIdx].Unlock()
543+
}
532544
for {
533545
i, ok := queues[checkerIdx].popFront()
534546
if !ok {
@@ -538,7 +550,7 @@ func (p *checkerPool) forEachCheckerGroupDo(ctx context.Context, files []*ast.So
538550
}
539551
p.fileCheckers[p.fileIndices[files[i]]].Store(int32(checkerIdx))
540552
}
541-
cb(c, i, files[i])
553+
fn(c, i)
542554
}
543555
})
544556
}

‎tsc/internal/compiler/program.go‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,26 +2028,22 @@ func (p *Program) Emit(ctx context.Context, options EmitOptions) *EmitResult {
20282028
writerPool.Put(writer)
20292029
}
20302030
if p.compilerCheckerPool != nil && !p.SingleThreaded() {
2031-
// Transform the files of each checker in one task. Transforming can create types and resolve symbols, so a
2032-
// checker shared by concurrent tasks would do that work in an order that depends on scheduling. Files are
2033-
// transformed in reverse program order, the order in which a single-threaded work group runs per-file tasks,
2034-
// so that a program with one checker produces the same output whether or not it is single-threaded. Printing
2035-
// does not use the checker, so it runs in tasks of its own. Single-threaded programs emit file by file instead,
2036-
// since queued prints would only run after every file is transformed.
2037-
for _, group := range p.compilerCheckerPool.groupFilesByChecker(sourceFiles) {
2038-
if len(group) == 0 {
2039-
continue
2040-
}
2031+
// Transform the files of each checker in one task, taking files from other checkers once out of its own; see
2032+
// forEachFileStealing. The emit resolver locks the checker itself, as programs may emit files concurrently. Files are transformed in reverse program order,
2033+
// the order in which a single-threaded work group runs per-file tasks. Printing does not use the checker, so it
2034+
// runs in tasks of its own. Single-threaded programs emit file by file instead, since queued prints would only
2035+
// run after every file is transformed.
2036+
groups := p.compilerCheckerPool.groupFilesByChecker(sourceFiles)
2037+
for _, group := range groups {
2038+
slices.Reverse(group)
2039+
}
2040+
p.compilerCheckerPool.forEachFileStealing(sourceFiles, groups, false /*lock*/, func(_ *checker.Checker, i int) {
2041+
emitter := emitters[i]
2042+
transformFile(emitter)
20412043
wg.Queue(func() {
2042-
for _, i := range slices.Backward(group) {
2043-
emitter := emitters[i]
2044-
transformFile(emitter)
2045-
wg.Queue(func() {
2046-
printFile(emitter)
2047-
})
2048-
}
2044+
printFile(emitter)
20492045
})
2050-
}
2046+
})
20512047
} else {
20522048
for _, emitter := range emitters {
20532049
wg.Queue(func() {

0 commit comments

Comments
 (0)