Skip to content

cmd/go: build dependent packages as soon as export data is ready #15734

Closed

Description

@josharian

This is a trace of the activity on an 8 core machine running 'go build -a std':

trace_build_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.

@rsc implemented one enabling piece by adding a flag to emit export data separately from machine code.

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.

Activity

  1. added this to the Proposal milestone on May 18, 2016
  2. bradfitz commented on May 18, 2016

    @bradfitz
    Contributor

    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.

  3. mdempsky commented on May 18, 2016

    @mdempsky
    Contributor

    How would the compiler signal to cmd/go that it is done emitting export data?

    A relatively simple (but local-only) way would be:

    1. 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.
    2. In cmd/compile, if we see -signalexportdata, we close the pipe FD after writing out export data to disk.
    3. 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.

  4. griesemer commented on May 18, 2016

    @griesemer
    Contributor

    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.

  5. mwhudson commented on May 18, 2016

    @mwhudson
    Contributor

    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:

    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)

  6. mdempsky commented on May 18, 2016

    @mdempsky
    Contributor

    @mwhudson I think so, but that could be done even without this proposal.

  7. josharian commented on May 19, 2016

    @josharian
    ContributorAuthor

    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.

  8. self-assigned this
    on May 19, 2016
  9. 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
  10. modified the milestones: Unplanned, Proposal on Aug 22, 2016
  11. josharian commented on Jul 6, 2017

    @josharian
    ContributorAuthor

    @rsc is it safe to assume that this will be more or less independent of your 1.10 cmd/go work?

  12. rsc commented on Oct 25, 2017

    @rsc
    Contributor

    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.

  13. rsc commented on Apr 30, 2019

    @rsc
    Contributor

    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.

  14. 18 remaining items

  15. gopherbot commented on Sep 17, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/833964 mentions this issue: cmd/compile: pass -c value through a file descriptor

  16. gopherbot commented on Sep 17, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/833965 mentions this issue: cmd/go: give compiles their concurrency with -cfd

  17. matloob commented on Sep 23, 2026

    @matloob
    Contributor

    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

      CL 827844 CL 828184 CL 828185 CL 828524 Cumulative
    C4AS16          
    GoBuildKubelet-16 -0.82 -1.93   -4.7 -7.31%
    GoBuildKubeletLink-16 -0.63       -0.63%
    GoBuildIstioctl-16 -1.33 -2.12   -4.43 -7.70%
    GoBuildIstioctlLink-16          
    GoBuildFrontend-16 -1.15 -3.88 0.81 -6.67 -10.60%
    GoBuildFrontendLink-16          
    GoBuildTsgo-16 -2.61 -5.5 1.02 -31.15 -35.99%
    GoBuildTsgoLink-16          
               
    C4AH72          
    GoBuildKubelet-72 -1.88 -4.7 1.71 -17.35 -21.39%
    GoBuildKubeletLink-72          
    GoBuildIstioctl-72 -1.64 -3.87   -14.96 -19.59%
    GoBuildIstioctlLink-72          
    GoBuildFrontend-72 -1.6 -6.2   -19.23 -25.45%
    GoBuildFrontendLink-72          
    GoBuildTsgo-72 -2.66 -6.19   -31.07 -37.06%
    GoBuildTsgoLink-72          
               
    C2S16          
    GoBuildKubelet-16       -2.78 -2.78%
    GoBuildKubeletLink-16          
    GoBuildIstioctl-16 -3.71 -2.82   -3.3 -9.51%
    GoBuildIstioctlLink-16     0.93   0.93%
    GoBuildFrontend-16 -2.1 -1.95   -4.29 -8.13%
    GoBuildFrontendLink-16          
    GoBuildTsgo-16 -3.17 -5.51   -23.28 -29.81%
    GoBuildTsgoLink-16          
               
    C3H88          
    GoBuildKubelet-88   -3.33   -20.99 -23.62%
    GoBuildKubeletLink-88          
    GoBuildIstioctl-88 -3.71 -6.19   -16.16 -24.27%
    GoBuildIstioctlLink-88          
    GoBuildFrontend-88 -2.1 -5.45   -20.02 -25.97%
    GoBuildFrontendLink-88          
    GoBuildTsgo-88 -3.17 -5.14   -27.73 -33.62%
    GoBuildTsgoLink-88          
               
    C4DH96          
    GoBuildKubelet-96 -1.64 -4.62   -18.22 -23.28%
    GoBuildKubeletLink-96          
    GoBuildIstioctl-96   -3.79   -16.93 -20.08%
    GoBuildIstioctlLink-96          
    GoBuildFrontend-96 -2.1 -4.92   -20.3 -25.81%
    GoBuildFrontendLink-96          
    GoBuildTsgo-96 -2.83 -6.68   -28.48 -35.15%
    GoBuildTsgoLink-96          
               

    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.

  18. mvdan commented on Sep 23, 2026

    @mvdan
    Member

    @matloob I have to say that this is really exciting! Thanks very much for taking this on ❤️

  19. gopherbot commented on Oct 1, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/842645 mentions this issue: cmd/go/internal/work: disable early export with -toolexec

  20. gopherbot commented on Oct 1, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/842665 mentions this issue: cmd/toolstash: check -linkobj files to support early export

  21. added a commit that references this issue on Oct 5, 2026
    ebe2fee
  22. gopherbot commented on Nov 16, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/864562 mentions this issue: cmd/go, cmd/compile: use named pipes for early export on Unix

  23. gopherbot commented on Nov 16, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/864564 mentions this issue: cmd/go: support early export with -toolexec

  24. gopherbot commented on Nov 16, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/864563 mentions this issue: cmd/go: support early export on Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions