Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
main: optimise "runtime" steps (#80)
Browse files Browse the repository at this point in the history
gobin maintains binaries in its own cache. The key for a cache entry is
a function of main package path, version and build tags. Assuming nobody
tampers with the build cache (making it readonly is the subject of #5),
then we need only install to the cache entry if a file does not already
exist.

Also, optimise go list calls by using -find; we don't care for the
dependencies in these steps.

No change in tests because there should be no change in behaviour.

With some incredibly unscientific timing tests on my machine, this
reduces the time taken for "gobin -run github.com/myitcv/gobin -help"
from 0.636s to 0.246s. "gobin -help" times at 0.070s which means ~0.176s
of gobin "overhead".
  • Loading branch information
myitcv committed Aug 27, 2019
1 parent d94d8e5 commit 6038506
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 32 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ cache:
- $GOPATH/pkg/mod/cache

go:
- "1.11.12"
- "1.12.7"
- "1.11.13"
- "1.12.9"
- "1.13beta1"

branches:
only:
- master

os:
- linux
- osx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
diff --git b/vendor/modules.txt a/vendor/modules.txt
index 49894ad..5b8d57c 100644
index 01abe41..ebc5500 100644
--- b/vendor/modules.txt
+++ a/vendor/modules.txt
@@ -1,16 +1,16 @@
# github.com/rogpeppe/go-internal v1.3.0
# github.com/rogpeppe/go-internal v1.3.1
github.com/rogpeppe/go-internal/cmd/txtar-addmod
-github.com/rogpeppe/go-internal/module
-github.com/rogpeppe/go-internal/txtar
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
diff --git b/vendor/modules.txt a/vendor/modules.txt
index 49894ad..5b8d57c 100644
index 01abe41..ebc5500 100644
--- b/vendor/modules.txt
+++ a/vendor/modules.txt
@@ -1,16 +1,16 @@
# github.com/rogpeppe/go-internal v1.3.0
# github.com/rogpeppe/go-internal v1.3.1
github.com/rogpeppe/go-internal/cmd/txtar-addmod
-github.com/rogpeppe/go-internal/module
-github.com/rogpeppe/go-internal/txtar
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/myitcv/gobin

require github.com/rogpeppe/go-internal v1.3.0
require github.com/rogpeppe/go-internal v1.3.1

go 1.11
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.1 h1:pgz0lCb+F99TrCwoy7d83j5kI//45fBQ34KzZ7t5as0=
github.com/rogpeppe/go-internal v1.3.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
Expand Down
47 changes: 26 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,25 +364,30 @@ func mainerr() error {
base := filepath.Base(mp.Target)
target := filepath.Join(gobin, base)

// optimistically remove our target in case we are installing over self
// TODO work out what to do for Windows
if mp.ImportPath == "github.com/myitcv/gobin" {
_ = os.Remove(target)
}

var stdout bytes.Buffer

installCmd := goCommand("install")
if *fTags != "" {
installCmd.Args = append(installCmd.Args, "-tags", *fTags)
}
installCmd.Args = append(installCmd.Args, mp.ImportPath)
installCmd.Dir = pkg.wd
installCmd.Env = append(buildEnv(localCacheProxy), "GOBIN="+gobin)
installCmd.Stdout = &stdout

if err := installCmd.run(); err != nil {
return err
// Only install if the target (within the gobin cache) does not
// exist. For now this cache is not read-only so this isn't as
// safe as it could/should be, but people shouldn't be messing with
// the cache anyway. The target in the cache is already hash by
// build tags so we should never have "overlapping" gobin runs.
if _, err := os.Stat(target); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to read %v: %v", target, err)
}
// optimistically remove our target in case we are installing over self
// TODO work out what to do for Windows
if mp.ImportPath == "github.com/myitcv/gobin" {
_ = os.Remove(target)
}
installCmd := goCommand("install")
if *fTags != "" {
installCmd.Args = append(installCmd.Args, "-tags", *fTags)
}
installCmd.Args = append(installCmd.Args, mp.ImportPath)
installCmd.Dir = pkg.wd
installCmd.Env = append(buildEnv(localCacheProxy), "GOBIN="+gobin)
if err := installCmd.run(); err != nil {
return err
}
}

switch {
Expand Down Expand Up @@ -511,7 +516,7 @@ func (a *arg) list(proxy string) error {

var stdout bytes.Buffer

listCmd := goCommand("list", "-json", a.pkgPatt)
listCmd := goCommand("list", "-find", "-json", a.pkgPatt)
listCmd.Dir = a.wd
listCmd.Stdout = &stdout
listCmd.Env = env
Expand Down Expand Up @@ -649,7 +654,7 @@ func (a *arg) list(proxy string) error {
// target module (including replace directives), list to ensure they
// have been resolved

listCmd := goCommand("list", "-json", pkg.ImportPath)
listCmd := goCommand("list", "-find", "-json", pkg.ImportPath)
listCmd.Dir = a.wd
listCmd.Env = buildEnv(proxy)

Expand Down
7 changes: 6 additions & 1 deletion vendor/github.com/rogpeppe/go-internal/goproxytest/proxy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6038506

Please sign in to comment.