Commit 1f631a2f authored by Dmitry Vyukov's avatar Dmitry Vyukov Committed by Ian Lance Taylor

cmd/go: parallelize fmt

Currently go fmt formats all files sequentially.
That's a shame. Parallelize it over files.

Reduces time of go fmt ./... in std lib
from ~6.1s to ~0.9s.

Reduces time of go fmt github.com/google/syzkaller/...
from ~5.2s to ~1.8s.

Change-Id: I3d27fc25326106b2a4781e13506a25c12d5bcdc5
Reviewed-on: https://go-review.googlesource.com/45491
Run-TryBot: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 8b2f8439
...@@ -8,6 +8,8 @@ package fmtcmd ...@@ -8,6 +8,8 @@ package fmtcmd
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"sync"
"cmd/go/internal/base" "cmd/go/internal/base"
"cmd/go/internal/cfg" "cmd/go/internal/cfg"
...@@ -41,13 +43,29 @@ See also: go fix, go vet. ...@@ -41,13 +43,29 @@ See also: go fix, go vet.
func runFmt(cmd *base.Command, args []string) { func runFmt(cmd *base.Command, args []string) {
gofmt := gofmtPath() gofmt := gofmtPath()
procs := runtime.GOMAXPROCS(0)
var wg sync.WaitGroup
wg.Add(procs)
fileC := make(chan string, 2*procs)
for i := 0; i < procs; i++ {
go func() {
defer wg.Done()
for file := range fileC {
base.Run(str.StringList(gofmt, "-l", "-w", file))
}
}()
}
for _, pkg := range load.Packages(args) { for _, pkg := range load.Packages(args) {
// Use pkg.gofiles instead of pkg.Dir so that // Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package, // the command only applies to this package,
// not to packages in subdirectories. // not to packages in subdirectories.
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles)) files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
base.Run(str.StringList(gofmt, "-l", "-w", files)) for _, file := range files {
fileC <- file
}
} }
close(fileC)
wg.Wait()
} }
func gofmtPath() string { func gofmtPath() string {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment