diff --git a/tsc/internal/compiler/program.go b/tsc/internal/compiler/program.go index 30dc482904816..b2038c7a472f7 100644 --- a/tsc/internal/compiler/program.go +++ b/tsc/internal/compiler/program.go @@ -663,24 +663,38 @@ func (p *Program) BindSourceFiles() { } func (p *Program) bindSourceFiles(ctx context.Context) { - wg := core.NewWorkGroup(core.WorkGroupConcurrency(p.SingleThreaded())) - for _, file := range p.files { - if ctx.Err() != nil { - break + if p.hosts.Tracing != nil { + wg := core.NewWorkGroup(core.WorkGroupConcurrency(p.SingleThreaded())) + for _, file := range p.files { + if ctx.Err() != nil { + break + } + if !file.IsBound() { + wg.Queue(func() { + if ctx.Err() != nil { + return + } + defer p.hosts.Tracing.Push(tracing.PhaseBind, "bindSourceFile", map[string]any{"path": string(file.PathKey())}, true)() + binder.BindSourceFile(file) + }) + } } + wg.RunAndWait() + return + } + // Avoid starting workers when all files are already bound (e.g. concurrent NewChecker calls). + var unbound []*ast.SourceFile + for _, file := range p.files { if !file.IsBound() { - wg.Queue(func() { - if ctx.Err() != nil { - return - } - if p.hosts.Tracing != nil { - defer p.hosts.Tracing.Push(tracing.PhaseBind, "bindSourceFile", map[string]any{"path": string(file.PathKey())}, true)() - } - binder.BindSourceFile(file) - }) + unbound = append(unbound, file) } } - wg.RunAndWait() + if len(unbound) == 0 { + return + } + core.ParallelFor(ctx, len(unbound), core.WorkGroupConcurrency(p.SingleThreaded()), func(i int) { + binder.BindSourceFile(unbound[i]) + }) } // Return the type checker associated with the program. diff --git a/tsc/internal/core/workgroup.go b/tsc/internal/core/workgroup.go index dc3b27f2adf3b..de0a65e635920 100644 --- a/tsc/internal/core/workgroup.go +++ b/tsc/internal/core/workgroup.go @@ -2,6 +2,7 @@ package core import ( "context" + "runtime" "sync" "sync/atomic" @@ -135,3 +136,42 @@ func (tg *ThrottleGroup) Go(fn func() error) { func (tg *ThrottleGroup) Wait() error { return tg.group.Wait() } + +// ParallelFor invokes fn(i) for i in [0, n) using at most concurrency worker goroutines. +// A concurrency of 0 defaults to runtime.GOMAXPROCS(0); a concurrency of 1 runs sequentially +// on the caller goroutine. Execution stops claiming new indices once ctx is canceled. +func ParallelFor(ctx context.Context, n int, concurrency int, fn func(i int)) { + if n <= 0 { + return + } + if concurrency <= 0 { + concurrency = runtime.GOMAXPROCS(0) + } + if concurrency == 1 || n == 1 { + for i := range n { + if ctx.Err() != nil { + return + } + fn(i) + } + return + } + workers := min(concurrency, n) + var next atomic.Int64 + var wg sync.WaitGroup + for range workers { + wg.Go(func() { + for { + if ctx.Err() != nil { + return + } + i := int(next.Add(1) - 1) + if i >= n { + return + } + fn(i) + } + }) + } + wg.Wait() +} diff --git a/tsc/internal/core/workgroup_test.go b/tsc/internal/core/workgroup_test.go index f436b493c1a4a..99b4eeb33bbb6 100644 --- a/tsc/internal/core/workgroup_test.go +++ b/tsc/internal/core/workgroup_test.go @@ -1,6 +1,7 @@ package core_test import ( + "context" "runtime" "sync" "sync/atomic" @@ -50,3 +51,29 @@ func TestWorkGroupConcurrencyLimit(t *testing.T) { assert.Equal(t, completed.Load(), int32(limit*(1+children+children*children))) assert.Equal(t, maxRunning.Load(), int32(limit)) } + +func TestParallelFor(t *testing.T) { + t.Parallel() + const n = 200 + seen := make([]atomic.Int32, n) + core.ParallelFor(t.Context(), n, 4, func(i int) { + seen[i].Add(1) + }) + for i := range n { + assert.Equal(t, seen[i].Load(), int32(1)) + } + + var sequential []int + core.ParallelFor(t.Context(), 5, 1, func(i int) { + sequential = append(sequential, i) + }) + assert.DeepEqual(t, sequential, []int{0, 1, 2, 3, 4}) + + ctx, cancel := context.WithCancel(t.Context()) + cancel() + var ran atomic.Int32 + core.ParallelFor(ctx, 10, 4, func(i int) { + ran.Add(1) + }) + assert.Equal(t, ran.Load(), int32(0)) +}