Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions tsc/internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Comment on lines +686 to 691

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core.Filter will avoid allocating if none of the files are bound.

Suggested change
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)
}
}
unbound := core.Filter(p.files, func (file *ast.SourceFile) {
file.IsBound()
}

or

Suggested change
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)
}
}
unbound := core.Filter(p.files, ast.(*SourceFile).IsBound)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just me or is GitHub rendering this in an insane way?

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.
Expand Down
40 changes: 40 additions & 0 deletions tsc/internal/core/workgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"context"
"runtime"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -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()
}
27 changes: 27 additions & 0 deletions tsc/internal/core/workgroup_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core_test

import (
"context"
"runtime"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -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))
}
Loading