This CL package exec.LookPath to internal/cfg.LookPath and adds cache.
BenchmarkLookPath-4
24149096 50.48 ns/op 0 B/op 0 allocs/op
Fixes #36768
Change-Id: I199a780d1eab9bd5397bb3759bb42191fff716e9
Change-Id: I199a780d1eab9bd5397bb3759bb42191fff716e9
GitHub-Last-Rev:
d67aa826f431affe829c23d1fdf2241fbb611303
GitHub-Pull-Request: golang/go#61464
Reviewed-on: https://go-review.googlesource.com/c/go/+/511458
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
--- /dev/null
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cfg
+
+import (
+ "internal/testenv"
+ "testing"
+)
+
+func BenchmarkLookPath(b *testing.B) {
+ testenv.MustHaveExecPath(b, "go")
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ _, err := LookPath("go")
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+}
"internal/cfg"
"io"
"os"
- "os/exec"
"path/filepath"
"runtime"
"strings"
if ctxt.CgoEnabled {
if os.Getenv("CC") == "" {
cc := DefaultCC(ctxt.GOOS, ctxt.GOARCH)
- if _, err := exec.LookPath(cc); err != nil {
+ if _, err := LookPath(cc); err != nil {
ctxt.CgoEnabled = false
}
}
--- /dev/null
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cfg
+
+import (
+ "cmd/go/internal/par"
+ "os/exec"
+)
+
+var lookPathCache par.ErrCache[string, string]
+
+// LookPath wraps exec.LookPath and caches the result
+// which can be called by multiple Goroutines at the same time.
+func LookPath(file string) (path string, err error) {
+ return lookPathCache.Do(file,
+ func() (string, error) {
+ return exec.LookPath(file)
+ })
+}
// intends to use the same 'go' as 'go generate' itself.
// Prefer to resolve the binary from GOROOT/bin, and for consistency
// prefer to resolve any other commands there too.
- gorootBinPath, err := exec.LookPath(filepath.Join(cfg.GOROOTbin, path))
+ gorootBinPath, err := cfg.LookPath(filepath.Join(cfg.GOROOTbin, path))
if err == nil {
path = gorootBinPath
}
"internal/platform"
"io/fs"
"os"
- "os/exec"
pathpkg "path"
"path/filepath"
"runtime"
goto omitVCS
}
if cfg.BuildBuildvcs == "auto" && vcsCmd != nil && vcsCmd.Cmd != "" {
- if _, err := exec.LookPath(vcsCmd.Cmd); err != nil {
+ if _, err := cfg.LookPath(vcsCmd.Cmd); err != nil {
// We fould a repository, but the required VCS tool is not present.
// "-buildvcs=auto" means that we should silently drop the VCS metadata.
goto omitVCS
package script
import (
+ "cmd/go/internal/cfg"
"cmd/go/internal/robustio"
"errors"
"fmt"
},
func(s *State, args ...string) (WaitFunc, error) {
lookPathOnce.Do(func() {
- path, pathErr = exec.LookPath(name)
+ path, pathErr = cfg.LookPath(name)
})
if pathErr != nil {
return nil, pathErr
import (
"bufio"
+ "cmd/go/internal/cfg"
"cmd/go/internal/script"
"errors"
"io"
- "os/exec"
"strings"
"testing"
)
return script.CachedCondition(
"<suffix> names an executable in the test binary's PATH",
func(name string) (bool, error) {
- _, err := exec.LookPath(name)
+ _, err := cfg.LookPath(name)
return err == nil, nil
})
}
"io/fs"
"log"
"os"
- "os/exec"
"path/filepath"
"runtime"
"strconv"
// Look in PATH for the toolchain before we download one.
// This allows custom toolchains as well as reuse of toolchains
// already installed using go install golang.org/dl/go1.2.3@latest.
- if exe, err := exec.LookPath(gotoolchain); err == nil {
+ if exe, err := cfg.LookPath(gotoolchain); err == nil {
execGoToolchain(gotoolchain, "", exe)
}
args = args[2:]
}
- _, err := exec.LookPath(v.Cmd)
+ _, err := cfg.LookPath(v.Cmd)
if err != nil {
fmt.Fprintf(os.Stderr,
"go: missing %s command. See https://golang.org/s/gogetcmd\n",
"fmt"
"go/build"
"os"
- "os/exec"
"path/filepath"
"runtime"
"strconv"
if cfg.Goos == runtime.GOOS && cfg.Goarch == runtime.GOARCH {
return ExecCmd
}
- path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", cfg.Goos, cfg.Goarch))
+ path, err := cfg.LookPath(fmt.Sprintf("go_%s_%s_exec", cfg.Goos, cfg.Goarch))
if err == nil {
ExecCmd = []string{path}
}
}
exe = fields[0]
if !strings.ContainsAny(exe, `/\`) {
- if lp, err := exec.LookPath(exe); err == nil {
+ if lp, err := cfg.LookPath(exe); err == nil {
exe = lp
}
}
}
var buf bytes.Buffer
- cmd := exec.Command(cmdline[0], cmdline[1:]...)
+ path, err := cfg.LookPath(cmdline[0])
+ if err != nil {
+ return nil, err
+ }
+ cmd := exec.Command(path, cmdline[1:]...)
if cmd.Path != "" {
cmd.Args[0] = cmd.Path
}
cmd.Env = append(cmd.Env, env...)
start := time.Now()
- err := cmd.Run()
+ err = cmd.Run()
if a != nil && a.json != nil {
aj := a.json
aj.Cmd = append(aj.Cmd, joinUnambiguously(cmdline))
//
// Otherwise, we compute a new validation description
// and compiler id (below).
- exe, err := exec.LookPath(compiler)
+ exe, err := cfg.LookPath(compiler)
if err != nil {
return cache.ActionID{}, false
}
if GccgoName == "" {
GccgoName = "gccgo"
}
- GccgoBin, gccgoErr = exec.LookPath(GccgoName)
+ GccgoBin, gccgoErr = cfg.LookPath(GccgoName)
}
func (gccgoToolchain) compiler() string {