Skip to content

Commit

Permalink
Cache GOROOT at init time
Browse files Browse the repository at this point in the history
When GOROOT is not set as an environment variable, GetBuildContext
fetches it from 'go env GOROOT', which takes about 50ms on my machine.
This overhead can almost double the time it takes to run an update since
GetBuildContext is called relatively frequently.

Instead, read GOROOT during initialization and cache it for
GetBuildContext.
  • Loading branch information
Anthony Romano committed Dec 16, 2016
1 parent b5e17dc commit f9cc015
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,25 @@ import (
// other needs arise it may need to be re-written.
var ResolveCurrent = false

// goRoot caches the GOROOT variable for build contexts. If $GOROOT is not set in
// the user's environment, then the context's root path is 'go env GOROOT'.
var goRoot string

func init() {
// Precompile the regular expressions used to check VCS locations.
for _, v := range vcsList {
v.regex = regexp.MustCompile(v.pattern)
}
if goRoot = os.Getenv("GOROOT"); len(goRoot) == 0 {
goExecutable := os.Getenv("GLIDE_GO_EXECUTABLE")
if len(goExecutable) <= 0 {
goExecutable = "go"
}
out, err := exec.Command(goExecutable, "env", "GOROOT").Output()
if err == nil {
goRoot = strings.TrimSpace(string(out))
}
}
}

func toSlash(v string) string {
Expand Down Expand Up @@ -272,6 +286,11 @@ func (b *BuildCtxt) PackageName(base string) string {
//
// TODO: This should be moved to the `dependency` package.
func GetBuildContext() (*BuildCtxt, error) {
if len(goRoot) == 0 {
return nil, fmt.Errorf("Please set the $GOROOT environment " +
"variable to use this command\n")
}

buildContext := &BuildCtxt{build.Default}

// If we aren't resolving for the current system set to look at all
Expand All @@ -282,18 +301,7 @@ func GetBuildContext() (*BuildCtxt, error) {
buildContext.UseAllFiles = true
}

if goRoot := os.Getenv("GOROOT"); len(goRoot) == 0 {
goExecutable := os.Getenv("GLIDE_GO_EXECUTABLE")
if len(goExecutable) <= 0 {
goExecutable = "go"
}
out, err := exec.Command(goExecutable, "env", "GOROOT").Output()
if goRoot = strings.TrimSpace(string(out)); len(goRoot) == 0 || err != nil {
return nil, fmt.Errorf("Please set the $GOROOT environment " +
"variable to use this command\n")
}
buildContext.GOROOT = goRoot
}
buildContext.GOROOT = goRoot
return buildContext, nil
}

Expand Down

0 comments on commit f9cc015

Please sign in to comment.