From ad1106983a3446fafdfe7a32edd3546e528c52b6 Mon Sep 17 00:00:00 2001 From: philhofer Date: Sat, 6 Jun 2015 15:07:08 -0700 Subject: [PATCH] run goimports in parallel --- printer/print.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/printer/print.go b/printer/print.go index 6a51fa79..44c31786 100644 --- a/printer/print.go +++ b/printer/print.go @@ -24,11 +24,14 @@ func PrintFile(file string, f *parse.FileSet, mode gen.Method) error { if err != nil { return err } - err = format(file, out.Bytes()) - if err != nil { - return err - } - infof(">>> Wrote and formatted \"%s\"\n", file) + + // we'll run goimports on the main file + // in another goroutine, and run it here + // for the test file. empirically, this + // takes about the same amount of time as + // doing them in serial when GOMAXPROCS=1, + // and faster otherwise. + res := goformat(file, out.Bytes()) if tests != nil { testfile := strings.TrimSuffix(file, ".go") + "_test.go" err = format(testfile, tests.Bytes()) @@ -37,6 +40,10 @@ func PrintFile(file string, f *parse.FileSet, mode gen.Method) error { } infof(">>> Wrote and formatted \"%s\"\n", testfile) } + err = <-res + if err != nil { + return err + } infof(">>> Done.\n") return nil } @@ -49,6 +56,15 @@ func format(file string, data []byte) error { return ioutil.WriteFile(file, out, 0600) } +func goformat(file string, data []byte) <-chan error { + out := make(chan error, 1) + go func(file string, data []byte, end chan error) { + end <- format(file, data) + infof(">>> Wrote and formatted \"%s\"\n", file) + }(file, data, out) + return out +} + func generate(f *parse.FileSet, mode gen.Method) (*bytes.Buffer, *bytes.Buffer, error) { outbuf := bytes.NewBuffer(make([]byte, 0, 4096)) writePkgHeader(outbuf, f.Package)