Repository navigation
Bind in parallel more efficiently - #65800
Open
Jake Bailey (jakebailey) wants to merge 1 commit into
Open
Jake Bailey (jakebailey) wants to merge 1 commit into
Jake Bailey (jakebailey) wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Already-bound files are not filtered before worker-pool startup, causing concurrent checker creation to launch unnecessary workers and contend on an atomic index despite having no binding work.
1 open issue
What changed in this PR
This PR reduces goroutine overhead when binding compiler source files, while preserving the existing tracing path.
Changes:
- Adds a bounded, cancellation-aware
ParallelForhelper. - Uses it for source-file binding when tracing is disabled.
- Tests index coverage, sequential execution, and cancellation.
| File | Description |
|---|---|
tsc/internal/core/workgroup.go |
Adds the bounded parallel loop helper. |
tsc/internal/core/workgroup_test.go |
Tests parallel-loop behavior. |
tsc/internal/compiler/program.go |
Uses worker-based binding outside tracing. |
Review effort: Balanced
Comment on lines
+685
to
+690
| core.ParallelFor(ctx, len(p.files), core.WorkGroupConcurrency(p.SingleThreaded()), func(i int) { | ||
| file := p.files[i] | ||
| if !file.IsBound() { | ||
| binder.BindSourceFile(file) | ||
| } | ||
| }) |
…Files
Program.bindSourceFiles previously queued one goroutine per source file
via parallelWorkGroup, spawning thousands of goroutines with separate
stacks on large projects.
Add core.ParallelFor to run indexed work across a fixed pool of
worker goroutines (defaulting to GOMAXPROCS) pulling indices from an
atomic counter, and use it in bindSourceFiles when tracing is inactive.
benchstat (-pgo=off, parent vs commit, n=6):
goos: linux
goarch: amd64
│ old │ new │
│ sec/op │ sec/op vs base │
Tsc/vscode 6.535 ± 1% 6.434 ± 2% -1.54% (p=0.015 n=6)
Tsc/mui-docs 3.916 ± 1% 3.923 ± 1% ~ (p=0.818 n=6)
geomean 5.059 5.024 -0.68%
│ old │ new │
│ B-rss │ B-rss vs base │
Tsc/vscode 4.752Gi ± 2% 4.850Gi ± 4% +2.06% (p=0.041 n=6)
Tsc/mui-docs 2.166Gi ± 5% 2.205Gi ± 6% ~ (p=0.937 n=6)
geomean 3.209Gi 3.270Gi +1.92%
Jake Bailey (jakebailey)
force-pushed
the
fast-pr/parallel-for
branch
from
December 4, 2026 23:13
29f7be2 to
a3f1926
Compare
Member
Author
|
TypeScript Bot (@typescript-bot) perf test this faster |
Contributor
Comment on lines
+686
to
691
| 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) | ||
| } | ||
| } |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Is it just me or is GitHub rendering this in an insane way?
Contributor
|
Jake Bailey (@jakebailey) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This code made a bunch of goroutines; we can do this more cleverly when we don't care about ordering (so, not tracing).
This saves about 20% of the bind time in vscode.