From ed1ac056735e67c0f6bc23c60a9c2a0f999c80cb Mon Sep 17 00:00:00 2001 From: Anthony Martin Date: Thu, 21 Feb 2013 20:09:31 -0800 Subject: [PATCH] cmd/go: don't call ImportDir unnecessarily This significantly speeds up the go tool on slow file systems (or those with cold caches). The following numbers were obtained using an encrypted ext4 file system running on Linux 3.7.9. # Before $ sudo sysctl -w 'vm.drop_caches=3' $ time go list code.google.com/p/go.net/... | wc -l 9 real 0m16.921s user 0m0.637s sys 0m0.317s # After $ sudo sysctl -w 'vm.drop_caches=3' $ time go list code.google.com/p/go.net/... | wc -l 9 real 0m8.175s user 0m0.220s sys 0m0.177s R=rsc, r CC=golang-dev https://golang.org/cl/7369044 --- src/cmd/go/main.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/cmd/go/main.go b/src/cmd/go/main.go index a7841d26551..bf1dad40f34 100644 --- a/src/cmd/go/main.go +++ b/src/cmd/go/main.go @@ -453,19 +453,20 @@ func matchPackages(pattern string) []string { return filepath.SkipDir } + // We use, e.g., cmd/gofmt as the pseudo import path for gofmt. + name = "cmd/" + name + if have[name] { + return nil + } + have[name] = true + if !match(name) { + return nil + } _, err = buildContext.ImportDir(path, 0) if err != nil { return nil } - - // We use, e.g., cmd/gofmt as the pseudo import path for gofmt. - name = "cmd/" + name - if !have[name] { - have[name] = true - if match(name) { - pkgs = append(pkgs, name) - } - } + pkgs = append(pkgs, name) return nil }) @@ -493,14 +494,14 @@ func matchPackages(pattern string) []string { return nil } have[name] = true - + if !match(name) { + return nil + } _, err = buildContext.ImportDir(path, 0) if err != nil && strings.Contains(err.Error(), "no Go source files") { return nil } - if match(name) { - pkgs = append(pkgs, name) - } + pkgs = append(pkgs, name) return nil }) }