You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/go: build dependent packages as soon as export data is ready #15734
This is a trace of the activity on an 8 core machine running 'go build -a std':
For those who want to explore more, here is an html version. (Hint: Use a, d, w, and s keys to navigate.)
There are a few early bottlenecks (runtime, reflect, fmt) and a long near linear section at the end (net, crypto/x509, crypto/tls, net/http). Critical path scheduling (#8893) could help some with this, as could scheduling cgo invocations earlier (#15681). This issue is to discuss another proposal that complements those.
We currently wait until a package is finished building before building packages that depend on it. However, dependent packages only need export data, not machine code, to start building. I believe that that could be available once we're done with escape analysis and closure transformation, and before we run walk.
For the bottlenecks listed above:
package
time until export data available
total compilation time
runtime
226ms
1300ms
reflect
174ms
960ms
fmt
33ms
229ms
net
114ms
846ms
crypto/x509
66ms
253ms
crypto/tls
82ms
461ms
net/http
168ms
1310ms
Though slightly optimistic (writing export data isn't instantaneous), this does suggest that this would in general significantly reduce time spent waiting for dependencies to compile.
This pattern of large, slow, linear dependency chains also shows up in bigger projects, like juju.
Remaining work to implement this, and open questions:
Emitting export data before walk means that inlined functions would get walked and expanded at use rather than at initial package compilation. Does this matter? If so, an alternative is to change the compiler structure to walk all functions and then compile all functions. Would this increase high water memory mark?
How would the compiler signal to cmd/go that it is done emitting export data? I don't know of a clean, simple, portable cross-process semaphore.
This would be a pretty major upheaval in how cmd/go schedules builds. Making this work more fine-grained would be useful anyway, but it'd be a lot of high risk change.
Given the scope of the change, I'm marking this as a proposal. I'd love feedback.
Reacted by Caleb Spare, Brad Fitzpatrick, Aliaksandr Valialkin, bourbonspecial, Ingo Oeser, Ondrej Fabry, Giovanni Bajo, Tom Thorogood, Bryan C. Mills and Kai Ase Siren
Love it. Also glad that traceview ended up working out.
How would the compiler signal to cmd/go that it is done emitting export data? I don't know of a clean, simple, portable cross-process semaphore.
Localhost TCP? Later: multi-machine TCP and making cmd/compile and cmd/link use a VFS (and network impls) rather than the os package for file access. Imagining running a large build on a cloud machine and cmd/go spinning up some helper Kubernetes (or whatever) containers to speed the build, then going away when the build is done, paying for them by the number of seconds they were running.
How would the compiler signal to cmd/go that it is done emitting export data?
A relatively simple (but local-only) way would be:
Each time cmd/go execs cmd/compile, it creates a new os.Pipe, passes the writing end as one of cmd/compile's ExtraFiles, along with a command line flag like -signalexportdata.
In cmd/compile, if we see -signalexportdata, we close the pipe FD after writing out export data to disk.
Back in cmd/go, when we see the pipe has been closed, we can assume export data is written out.
I also suspect you don't actually need separate export data file functionality for this. Since the export data is at the beginning of the .a file, we can just write a partial .a file, signal cmd/go, and then finish writing later.
Interesting. I suspect also that the time from start of compilation until the time the export data becomes available will become smaller over time (faster frontend), while the backend may become slower (relatively), due to more powerful optimizations. Seems like a good idea to me.
Interesting. I suspect also that the time from start of compilation until
the time the export data becomes available will become smaller over time
(faster frontend), while the backend may become slower (relatively), due to
more powerful optimizations. Seems like a good idea to me.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub #15734 (comment)
changed the title [-]proposal: build dependent packages as soon as export data is ready[/-][+]cmd/go: build dependent packages as soon as export data is ready[/+]on Aug 22, 2016
I'm very skeptical this is worth the complexity. It would require support for "half-completed" actions in the go command where the compile step half-completes early and then fully-completes later. I don't believe the payoff here would be worth the significant increase in complexity. I guess you could run two compiles, so that you generate the export data first and then the whole object second, but that just does more work overall. Even if it improves latency in certain cases, more work overall is a net loss.
Critical path scheduling or just working on making the compiler faster seems like a better use of time.
Especially with good caching I think this is less and less important, and no less complex to implement. Closing, to better reflect our intention not to do this.
I'm adding some more context on why I think we should do this now, and the improvements we've seen. I have numbers from the perf builders and information about the attached traces below.
Rationale for doing this now when we decided not to before:
Newer machines have many more cores than older ones did, so improving parallelism is more important now than it was back then. Ideally each compile process would be much more parallel: when that is the case, early export will not make that much of a difference. But for now we can see a 10-30% improvement from unblocking dependent compiles earlier.
Sketch of how we'd implement backend export data:
First, the compiler would have to emit an extra export section in its archive file: this would go in the linkobj file. We'd then have to make the Go command add dependencies between a backend action and its dependent backend actions. (Note that while the compiler runs as a single process, in the early export CLs, we model it as two actions in cmd/go to make it easier to model the export data as an output dependent actions can depend on.) In early export, we have a mechanism using a message sent on a pipe for the compiler to signal to the go command that the export data is ready. We'd add a pipe in the other direction (which is something we are already planning to do to better manage the -c concurrency of the compiler by the go command) which would allow the go command to signal to the compiler that the backends of all the dependencies have finished. That would point to a file similar to an importcfg mapping each import to its backend export file.
Just like early export still caches the export and object files together, adding a backend export would still result in a single cached object per package: it would contain the frontend export, backend export, and the objects for the linker. And the compiler would still produce a single file with both exports and the objects if the linkobj flag is not provided.
Traces were generated as follows: I cherry picked each of the CLs (827844, 828184, 828185, 828524, 933964, 833965, 833966 (this last one hasn't been sent out for review yet)) on top of the tree at CL 825084 (before the bottom cl was submitted). I then ran traces using toolchains built at the top and bottom of the stack. I downloaded the head versions of istioctl, kubectl, frontend, and tsc, so they don't quite match up with the sweet benchmarks.
Activity
bradfitz commentedon May 18, 2016
Love it. Also glad that traceview ended up working out.
Localhost TCP? Later: multi-machine TCP and making cmd/compile and cmd/link use a VFS (and network impls) rather than the os package for file access. Imagining running a large build on a cloud machine and cmd/go spinning up some helper Kubernetes (or whatever) containers to speed the build, then going away when the build is done, paying for them by the number of seconds they were running.
mdempsky commentedon May 18, 2016
A relatively simple (but local-only) way would be:
I also suspect you don't actually need separate export data file functionality for this. Since the export data is at the beginning of the .a file, we can just write a partial .a file, signal cmd/go, and then finish writing later.
griesemer commentedon May 18, 2016
Interesting. I suspect also that the time from start of compilation until the time the export data becomes available will become smaller over time (faster frontend), while the backend may become slower (relatively), due to more powerful optimizations. Seems like a good idea to me.
mwhudson commentedon May 18, 2016
Only vaguely connected random idea: If the export data hasn't changed, can
you skip the compilation of dependent packages?
On 19 May 2016 at 10:16, Robert Griesemer notifications@github.com wrote:
mdempsky commentedon May 18, 2016
@mwhudson I think so, but that could be done even without this proposal.
josharian commentedon May 19, 2016
This proposal also got discussed at #15736. Based on that discussion, I will re-evaluate this proposal once cmd/compile itself is more concurrent.
@mwhudson moved your suggestion to #15752.
[-]proposal: build dependent packages as soon as export data is ready[/-][+]cmd/go: build dependent packages as soon as export data is ready[/+]josharian commentedon Jul 6, 2017
@rsc is it safe to assume that this will be more or less independent of your 1.10 cmd/go work?
rsc commentedon Oct 25, 2017
I'm very skeptical this is worth the complexity. It would require support for "half-completed" actions in the go command where the compile step half-completes early and then fully-completes later. I don't believe the payoff here would be worth the significant increase in complexity. I guess you could run two compiles, so that you generate the export data first and then the whole object second, but that just does more work overall. Even if it improves latency in certain cases, more work overall is a net loss.
Critical path scheduling or just working on making the compiler faster seems like a better use of time.
rsc commentedon Apr 30, 2019
Especially with good caching I think this is less and less important, and no less complex to implement. Closing, to better reflect our intention not to do this.
18 remaining items
gopherbot commentedon Sep 17, 2026
Change https://go.dev/cl/833964 mentions this issue:
cmd/compile: pass -c value through a file descriptorgopherbot commentedon Sep 17, 2026
Change https://go.dev/cl/833965 mentions this issue:
cmd/go: give compiles their concurrency with -cfdfix(toolexec): forward unknown tool flags instead of failing (DataDog…
matloob commentedon Sep 23, 2026
I'm adding some more context on why I think we should do this now, and the improvements we've seen. I have numbers from the perf builders and information about the attached traces below.
Rationale for doing this now when we decided not to before:
Newer machines have many more cores than older ones did, so improving parallelism is more important now than it was back then. Ideally each compile process would be much more parallel: when that is the case, early export will not make that much of a difference. But for now we can see a 10-30% improvement from unblocking dependent compiles earlier.
Sketch of how we'd implement backend export data:
First, the compiler would have to emit an extra export section in its archive file: this would go in the linkobj file. We'd then have to make the Go command add dependencies between a backend action and its dependent backend actions. (Note that while the compiler runs as a single process, in the early export CLs, we model it as two actions in cmd/go to make it easier to model the export data as an output dependent actions can depend on.) In early export, we have a mechanism using a message sent on a pipe for the compiler to signal to the go command that the export data is ready. We'd add a pipe in the other direction (which is something we are already planning to do to better manage the -c concurrency of the compiler by the go command) which would allow the go command to signal to the compiler that the backends of all the dependencies have finished. That would point to a file similar to an importcfg mapping each import to its backend export file.
Just like early export still caches the export and object files together, adding a backend export would still result in a single cached object per package: it would contain the frontend export, backend export, and the objects for the linker. And the compiler would still produce a single file with both exports and the objects if the linkobj flag is not provided.
Sweet GoBuild numbers from perf gomotes
Traces
traces.zip
View the traces at ui.perfetto.dev.
Traces were generated as follows: I cherry picked each of the CLs (827844, 828184, 828185, 828524, 933964, 833965, 833966 (this last one hasn't been sent out for review yet)) on top of the tree at CL 825084 (before the bottom cl was submitted). I then ran traces using toolchains built at the top and bottom of the stack. I downloaded the head versions of istioctl, kubectl, frontend, and tsc, so they don't quite match up with the sweet benchmarks.
mvdan commentedon Sep 23, 2026
@matloob I have to say that this is really exciting! Thanks very much for taking this on ❤️
gopherbot commentedon Oct 1, 2026
Change https://go.dev/cl/842645 mentions this issue:
cmd/go/internal/work: disable early export with -toolexecgopherbot commentedon Oct 1, 2026
Change https://go.dev/cl/842665 mentions this issue:
cmd/toolstash: check -linkobj files to support early exportcmd/toolstash: check -linkobj files to support early export
cmd/go/internal/work: disable early export with -toolexec
cmd/compile: preassign symbol indices for symbols in export data
cmd/compile: write the export data early
gopherbot commentedon Nov 16, 2026
Change https://go.dev/cl/864562 mentions this issue:
cmd/go, cmd/compile: use named pipes for early export on Unixgopherbot commentedon Nov 16, 2026
Change https://go.dev/cl/864564 mentions this issue:
cmd/go: support early export with -toolexecgopherbot commentedon Nov 16, 2026
Change https://go.dev/cl/864563 mentions this issue:
cmd/go: support early export on Windows