Skip to content

Commit dd52646

Browse files
adonovangopherbot
authored andcommitted
go/ssa: create fewer goroutines
This CL limits the number of goroutines created by Program.Build to the number of P's, since building is entirely CPU bound. Creating more than this number of goroutines just increases churn, and potentially increases cache and allocator contention, and increases peak memory usage and GC. The effect on TestStdlib's "build SSA" timing metric seems to be a reduction of around 2-3%, but it's fairly noisy. Change-Id: Ie85e93b99c27ece245be05892818d52ae3b7fbc1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/572796 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Tim King <[email protected]>
1 parent 813e70a commit dd52646

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

go/ssa/builder.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import (
7979
"go/token"
8080
"go/types"
8181
"os"
82+
"runtime"
8283
"sync"
8384

8485
"golang.org/x/tools/internal/aliases"
@@ -2622,15 +2623,20 @@ func (prog *Program) Build() {
26222623
p.Build()
26232624
} else {
26242625
wg.Add(1)
2626+
cpuLimit <- struct{}{} // acquire a token
26252627
go func(p *Package) {
26262628
p.Build()
26272629
wg.Done()
2630+
<-cpuLimit // release a token
26282631
}(p)
26292632
}
26302633
}
26312634
wg.Wait()
26322635
}
26332636

2637+
// cpuLimit is a counting semaphore to limit CPU parallelism.
2638+
var cpuLimit = make(chan struct{}, runtime.GOMAXPROCS(0))
2639+
26342640
// Build builds SSA code for all functions and vars in package p.
26352641
//
26362642
// CreatePackage must have been called for all of p's direct imports

0 commit comments

Comments
 (0)