`go build`不必要地重建 [英] `go build` rebuilds unnecessarily

查看:141
本文介绍了`go build`不必要地重建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个小程序中构建并运行速度非常缓慢(尤其是cgo调用)。我想要缓存二进制文件,以便只在源文件较新时重新生成。我会用一个带有%规则的简单Makefile,但是语言设计者声称去构建支持不需要Makefiles。



是否有另一个我忽略的选择? go社区是否更喜欢另一个构建系统,或许是基于散列的缓存和重用构建产品? 工具恰巧可以解决这个问题。 go build 不会检查它生成的可执行文件是否是最新的。 go install 确实如此,如果您调整它以安装到您选择的位置,那么您将得到所需的结果,类似于 go build 。



您可以通过如下方式查看您描述的行为:

  $ go get -d github.com/anacrolix/missinggo/cmd/nop 
$ time go run$ GOPATH/src/github.com/anacrolix/missinggo/cmd /nop/*.go

real 0m0.176s
user 0m0.142s
sys 0m0.048s

这是一场温暖的运行。 go run 会在每次调用时链接,就像 go build 会。请注意, github.com/anacrolix/missinggo/cmd/nop 是一个完全没有任何功能的程序。



使用我的工具godo调用相同的包:

  $ time godo github.com/anacrolix/missinggo/cmd/nop 

real 0m0.073s
user 0m0.029s
sys 0m0.033s

对于较大的程序,差异应该更加明显。因此,总而言之,您的标准工具选项是使用 go安装,或者像godo这样的替代品。


go build and go run are very slow on a tiny program I have (cgo invocations in particular). I'd like go to cache the binary so that it only rebuilds when the source is newer. I would use a simple Makefile with a % rule, but the language designers claim that go's build support doesn't need Makefiles.

Is there another alternative I've overlooked? Does the go community prefer another build system, maybe hash-based instead, for caching and reusing build products?

解决方案

I wrote a tool that happens to solve this as a side effect. go build alone will not check if the executable it's producing is already up to date. go install does, and if you tweak it to install to a location of your choice, then you'll get the desired result, similar to go build.

You can see the behaviour you describe by doing something like this:

$ go get -d github.com/anacrolix/missinggo/cmd/nop
$ time go run "$GOPATH"/src/github.com/anacrolix/missinggo/cmd/nop/*.go

real    0m0.176s
user    0m0.142s
sys     0m0.048s

That's on a warm run. go run will link on every invocation, just as go build would. Note that github.com/anacrolix/missinggo/cmd/nop is an program that does absolutely nothing.

Here's invoking the same package, using my tool, godo:

$ time godo github.com/anacrolix/missinggo/cmd/nop

real    0m0.073s
user    0m0.029s
sys     0m0.033s

For larger programs, the difference should be more pronounced.

So in summary, your standard tooling option is to use go install, or an alternative like godo.

这篇关于`go build`不必要地重建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆