how to diagnose slow build

106 views
Skip to first unread message

王富民awaw

unread,
Jun 29, 2023, 8:11:07 AM6/29/23
to golang-nuts
My builds have been very slow recently and I want to know why is `go build` so slow for me.
Is it due to reading disks or something else?

What is the recommended way of profiling/debugging `go build`?

I know that the `-x` flag prints detailed information of `go build` but it doesn't tell where is time spent.
I am familiar with profiling ordinary Go programs with pprof, but how can I apply it to `go build`.
An extensive search returned very few information as it hard to come up with a query for profiling `go build`, not profiling ordinary Go programs nor info about the `build` command itself.

Thanks

Brian Candler

unread,
Jun 29, 2023, 8:29:12 AM6/29/23
to golang-nuts
What platform are you building under - in particular, is it Windows?

王富民awaw

unread,
Jun 29, 2023, 9:19:52 PM6/29/23
to golang-nuts
Hi Brian and all

I am running in Windows, to be specific:

go version go1.20.4 windows/amd64

I just found a neat way to profile `go build`, which is the debug-trace flag:

```
go build -debug-trace=build.trace .\cmd\server\GoServer.go
```

and have uploaded the results in the attachment below.

Interestingly, the trace suggests that while compilation itself (exec.Builder.Do) is pretty fast taking 7s,
loading modules (load.PackagesAndErrors) is excruciating slow, taking 31s!

Most of the time is spent on

modfetch.download
modload.loadImport

I wonder why is module loading so slow?
Loading modules should be a pretty fast operation, as there is no internet connection involved (I can build without network).
How can we make `modfetch` and `modload` faster, or even better, eliminate them?
build.trace
Reply all
Reply to author
Forward
0 new messages