Skip to content

Build binaries with one cached exec per package - #74

Merged
sipsma merged 14 commits into
mainfrom
perf/discovery-3
Oct 22, 2026
Merged

sipsma merged 14 commits into
mainfrom
perf/discovery-3

Conversation

@sipsma

@sipsma sipsma commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Binaries were built by one go build exec, with the Go build cache in a cache volume. A cold build compiled everything in one exec, and the cache lived on one engine.

They are now built by a new helper module, gobuild, which replays the go command's own plan (go build -n) one package at a time. Each package's compile is an ordinary Dagger result. There is no cache volume and no engine-local store on the build path, so any engine can pull a compile from the remote cache, and an edit re-runs only the packages it reaches. There is no fallback: per-package is the only path, except that gccgo builds stay one go build.

On dagger/dagger's CLI, against main, an edit takes about 6 s instead of 22, a call with no change about 1 s instead of 14, and a first build about 19 s instead of 28.5.

How it builds

  • The plan. It is made from Go files' headers and the source's other files, so an edit inside function bodies doesn't re-plan. It runs with the build's own flags and environment, and its steps are replayed as written.
  • Dependencies. Packages outside the source (the standard library and dependencies) build in one exec. A heavy tail, such as modernc's sqlite, builds in a second exec, and workspace packages that don't import it don't wait for it.
  • Workspace packages. These build one exec each. The main package is compiled and linked in one. A package whose archive comes out unchanged leaves its dependents cached.
  • Where they run. Package execs run in the toolchain image with the base's environment, or in a custom base as it is, with its own mounts kept. Compiles get a fixed -c, so an exec is the same on every engine.
  • Test binaries. GoPackage.testBinary and GoPlatform.testBinary build a package's test binary the same way. They plan from go test -c -n, run one cached exec per package, and take buildFlags as go test -c does (-race, -ldflags, tags). Unlike go test -c, they don't run go vet; that's left to the lint checks.

Also included: package discovery and the include scan do less repeated work on large multi-module repositories.

Timing

Medians in seconds, on a benchmark machine. The edits are one-line Go changes, the same in every column.

  • Before (Oct 7): a Dagger development engine built from Dagger main as of October 7, with this module as of October 2, before Speed up package discovery in large workspaces #72 and Cut per-package module calls from package discovery #73. Two runs for dagger's CLI, four for yq, each on a fresh engine.
  • main and this PR: a Dagger development engine with v1.0.0-beta.19 plus unreleased commits on Dagger main, a fresh engine per run. Four runs per project, alternating which version went first. This PR was loaded from a fork at the same commit, so neither version could reuse the other's builds.
  • plain go build: go build alone, in a container from the same Go image the module picks, with no Dagger call around it. Four rounds, each starting from an empty Go build cache, standard library included. Then the same edits and a no-change build ran on the warm cache.
dagger/dagger's CLI (cmd/dagger) before (Oct 7) main this PR plain go build
First build 189.8 28.5 19.4 15.5
Edit, internal package 184.7 22.4 6.6 3.3
Edit, top-level package 184.6 21.5 5.9 2.4
No change 176.3 14.3 1.2 0.2
yq before (Oct 7) main this PR plain go build
First build 10.90 8.13 7.88 7.05
Edit 4.03 1.36 1.25 0.61
No change 3.29 0.71 0.53 0.07

The runs of main and this PR didn't overlap anywhere, except yq's first build, which is about even. Before the performance work, almost all of each call on dagger's CLI went to the module finding packages, through per-module file listing and about 9,700 search-result reads. The go build exec itself was 8–15 s of it.

This PR isn't at plain go build yet, but it is much closer to it than before, and the work continues.

Test binaries. dagger/dagger's core/integration test binary, built with the race detector (237 MB; -race -vet=off, one -X stamp). Medians of two runs, in seconds, on a fresh engine per run, on the same benchmark machine.

core/integration (race) dagger/dagger's CI module today this PR plain go test -c
First build 96.3 93.8 83.6
Edit, internal package 29.7 27.6 20.9
Edit, test file 25.2 27.3 18.3
No change 15.4 2.6 0.3

On one engine, edits are about even with today, because linking the race binary dominates. Today's warm Go cache lives in volumes on one engine, while this PR's package results are ordinary results the remote cache can share across engines.

Behaviour

Everything the module builds today still builds, checked on released v1.0.0-beta.19. Each binary runs as a plain go build of the same package does. This covers:

  • cgo, -race, -asan and -cover;
  • -toolexec, including from a wrapper mounted in a custom base;
  • PGO, from default.pgo or -pgo;
  • pie, c-shared and c-archive with the C header, and plugin;
  • build tags and other GOFLAGS, split as Go splits them;
  • -modfile;
  • //go:embed, including directories named like Go files, test files and symlinks, with go build's errors.

Test binaries are byte-identical to plain go test -c -vet=off apart from build IDs. That holds with and without -race and an -ldflags stamp, with -cover and -coverpkg, and for linux/arm64. The same tests are listed and pass.

Changes:

  • gccgo builds stay one go build, without the cache volumes.
  • -linkshared and -buildmode=shared are clear errors. Neither could produce a usable binary before. go build rebuilds the shared standard library inside its own container, so a -linkshared binary can't run anywhere else, and -buildmode=shared makes no binary.
  • C headers: c-shared and c-archive builds return the C header next to the library when cgo writes one, as go build does. A package that exports nothing gets none.
  • Source files: an alternate -modfile go.mod must be in the module's source (includeExtraFiles), as before.
  • default.pgo is now used automatically, as go build uses it. Before, it was used only if includeExtraFiles listed it, and was otherwise silently ignored.
  • Minimum engine: the module requires Dagger v1.0.0-beta.17 or later.
  • Vendored modules now build, test and lint. Before, every command failed with "inconsistent vendoring", because vendor/modules.txt was never mounted; that is also true of the module on main today. In vendor mode the build skips the module download.
  • A workspace made from a directory or a git ref (asWorkspace) is mounted at /src. Before, its mount path was named after its content digest, so every edit changed every package's compile and nothing was reused.

The repository's dagger check suite passes.

Binaries are the same as today's, apart from the build ID values.

  • What was compared: six targets, built with today's module and with this PR, on two fresh engines each. The targets were dagger's CLI, yq, a cgo program, a -race build, pie, and c-shared (the library and its C header). Every file was compared byte for byte.
  • Result: every binary is the same size and differs in at most 99 bytes, all inside the two build-ID notes. Code, data, symbol tables, link order and all debug information are identical, C included, as are go version -m and the C header.
  • The build ID: the binary's Go build ID is a hash of what the link reads, so it, and the GNU build ID derived from it, still change with whatever the binary is made from. The value differs from the one go build computes.
  • Determinism: each version's outputs were byte-identical across fresh engines.

@sipsma sipsma changed the title List each module's Go files and search for tests and markers once per workspace Speed up package discovery and the include scan on large workspaces Oct 21, 2026
@sipsma sipsma changed the title Speed up package discovery and the include scan on large workspaces Build binaries with one cached exec per package, and speed up discovery and the include scan Oct 22, 2026
@sipsma sipsma changed the title Build binaries with one cached exec per package, and speed up discovery and the include scan Build binaries with one cached exec per package Oct 22, 2026
sipsma added 14 commits October 22, 2026 21:57
Gomod.modules globbed each module's tree for its Go files, one workspace
call per module, and each glob covered the modules nested in it again. It
now globs once for each outermost module and gives each file to the
deepest module root above it, with the same nested-module and ignore rules
as ownFiles.

Signed-off-by: Erik Sipsma <[email protected]>
Package discovery searched each module's tree twice, once for test files
and once for marker lines, and read every result's fields. Each module's
search covered the modules nested in it again, so on a repository whose
root is a module most results were read twice. Go.packages now runs each
search once over the outermost module trees, and each module keeps the
files that are its own, as it already did with its own search's results.

Signed-off-by: Erik Sipsma <[email protected]>
The scan parsed a module's Go files again for every module reaching it
through a local replace. In dagger's repository that is the root
module's ~3000 files, parsed once for each of the modules replacing it,
and the scan took ~14s; every Go edit pays it, since any change to a Go
file changes the scan's input.

Each module's directives are now parsed the first time they are asked
for, its files on as many goroutines as there are CPUs, and the parse
skips object resolution, which nothing here reads. The output is
unchanged: the same files, byte for byte, on dagger's source, where the
scan now takes ~1.5s.

Signed-off-by: Erik Sipsma <[email protected]>
The scan writes two more files, both from the files it already reads:

- _headers_.gohdr: every non-test Go file of the workspace's modules up to
  the end of its imports, plus its go:embed lines, and every go.mod, in one
  pack. That is what a "go build -n" plan reads of Go source, so a plan
  made from it holds across edits inside function bodies.
- _discovery_: a line per module (path, module path, Go version) and per
  package directory with something to do (tests to run, a main package to
  build, go:generate commands to run). Tests are found with the go
  module's own pattern, Unicode names included, and lines of any length
  are read.

A file that can't be read is left out of both, and its module's scan
records why, as before, so other modules are unaffected.

The scanner no longer mounts the shared Go cache volumes, so the scan is
an ordinary result another engine can pull from the remote cache.
goImage names the toolchain image goContainer starts from.

Signed-off-by: Erik Sipsma <[email protected]>
Binaries were built by one "go build" exec with the Go build cache in a
cache volume: a cold build compiled everything in one exec, and the cache
lived on one engine. They are now built by a new helper module, gobuild,
which replays the go command's own plan ("go build -n") one package at a
time, so Dagger caches each package's compile as an ordinary result, any
engine can pull it from the remote cache, and an edit re-runs only the
packages it reaches.

- The plan is made from the scan's header pack and the source's other
  files, so an edit inside function bodies doesn't re-plan. It is run
  with the build's own flags and environment, and its steps are replayed
  as written: cgo (the cgo tool and the C compiler, from the base image),
  -race, -asan, -cover, -toolexec, PGO (default.pgo or -pgo, its
  preprocessing run once and read by every compile), build modes such as
  pie and c-shared, build tags and other GOFLAGS. A c-archive or c-shared
  build returns the C header go build writes next to the library.
- go build -n answers the go command's C compiler probes "unsupported"
  without running them. The plan's exec runs them, and the flags go build
  adds when they pass go back into the C compiles: the debug-path
  mappings of the work directory, GOROOT and, with -trimpath, the module;
  -gno-record-gcc-switches; -frandom-seed; and clang's and zig's. A cgo
  binary's C debug information is then the same as go build's.
- Packages outside the source (the standard library and dependencies)
  build in one exec, a tier. When the heaviest chain of dependencies ends
  in a heavy tail, such as modernc's sqlite, the tail builds in a second
  tier on top of the first, and workspace packages that import none of it
  start without waiting for it.
- Workspace packages build one exec each; the main package is compiled
  and linked in one. A package whose archive comes out unchanged leaves
  its dependents cached. Archives have no Go build ID. The link's build ID
  is a hash of what it reads (its import config, the archives and its
  command), so the binary's Go build ID and the GNU build ID the linker
  makes from it change with whatever the binary is made from; the value
  differs from the one go build computes.
- Package execs run in the toolchain image with the base's environment
  alone, or in a custom base as it is, its own mounts kept. The Go cache
  volumes are dropped, and compiles get a fixed -c, so an exec is the same
  on every engine.
- gccgo compiles differently from the gc toolchain the plan describes, so
  a gccgo build is one go build, as before, without the cache volumes.
- -linkshared and -buildmode=shared are errors: go build rebuilds the
  shared standard library inside its own container, so a -linkshared
  binary can't run anywhere else, and -buildmode=shared makes no binary.

gobuild needs Dagger v1.0.0-beta.17 or later, where one withFiles takes
any number of files; the go module now declares it. Packages are found
from the scan's discovery file rather than by searching the workspace.

Signed-off-by: Erik Sipsma <[email protected]>
A module with a vendor directory failed every command: its builds, tests
and lint. Go runs such a module in vendor mode, and vendor mode checks the
vendored modules against vendor/modules.txt, which was never mounted:

    go: inconsistent vendoring in /src/...:
        example.com/[email protected]: is explicitly required in go.mod, but
        not marked as explicit in vendor/modules.txt

The module's source now includes vendor/modules.txt at each module root,
in the scanner's patterns and in includeBase.

The per-package build also downloaded every module go.mod requires before
planning, which in vendor mode the go command never reads: wasted for a
dependency the proxy has, and a failure for one it doesn't. It now skips
the download when go would build in vendor mode: -mod=vendor in the flags
or GOFLAGS, or, with no -mod, a vendor/modules.txt at the main module's
root (or the go.work's) and a go directive from 1.14 (1.22 for go.work)
in the go.mod the build reads, an alternate -modfile one included.
A source with no vendor/modules.txt doesn't ask go at all.

testdata/go-module-vendored vendors a dependency that exists nowhere
else; vendoredCheck tests it and builds and runs its binary.

Signed-off-by: Erik Sipsma <[email protected]>
The workspace mount path defaults to /src/<workspace name>, the last
segment of the workspace address. A workspace made with asWorkspace, from
a directory or a git ref, is addressed by its content digest or commit
("directory://xxh3:..."), so the mount path changed with every edit, and
with it every package's compile: nothing was reused between two versions
of the same source. Such a workspace is now mounted at /src. A workspace
with a name, and a configured mountPath, mount as before.

Signed-off-by: Erik Sipsma <[email protected]>
The per-package build plans from a view of the source with Go files cut
to their headers. Three things went wrong in it for //go:embed:

- The view leaves out every path matching **/*.go, directories included,
  so a directory named like a Go file vanished with all it held.
  dagger's codegen embeds its Go SDK templates from one, _dagger.gen.go,
  and its binary came out without them: its Go SDK generation failed
  with `template "_dagger.gen.go/defs.go.tmpl" not defined`.
- The header pack left test files out, so a pattern naming one, such as
  "all:src" over a directory holding *_test.go files, embedded nothing
  for it.
- A pattern go build rejects failed with only an exit code: the plan's
  output, which holds go build's error, stayed in its file.

The header pack now holds test files' headers, which a build never
compiles, and the other files of a directory named like a Go file,
whole, as the view holds every other file. A symlink among them stays
one. A Go file's header is as before, so an edit inside a function body
still leaves the plan cached. A failed plan reports go build's error,
then the exec's with its exit code; a position in a Go file is moved
from its header's line to the source's.

embedCheck builds one main package per embed shape and compares each
with plain go build: a directory named like a Go file, with and without
all:, test files and files starting with _ or ., a Go file's whole
source, a pattern into another module, one matching nothing, and a
symlink in a directory pattern and named by one, a test file among them.
A test file's embed reaches its test.

Signed-off-by: Erik Sipsma <[email protected]>
A cgo main package built with -cover failed:

    cgo: open /src/work/p.../x.cover.go: no such file or directory

go build -n prints its work directory as "$WORK", which is not an
absolute path, so a work file it hands the cgo tool from the package's
source directory, here the cover tool's output, comes out as
"./$WORK/b001/x.cover.go". The per-package build replayed that as a path
under the source directory. go build runs it as the absolute work path,
and so does the plan now. A plan without such a path, which means every
build that isn't a cgo one with -cover, is unchanged.

cgoCoverCheck builds a cgo main package with -cover, compares it with
plain go build -cover byte for byte but for its build IDs, and runs it.

Signed-off-by: Erik Sipsma <[email protected]>
A c-archive or c-shared build of a package that exports nothing failed:

    stat _cgo_install.h: no such file or directory

cgo is asked for the header of a package's exported functions in both
modes, but writes it only when there is one to export, and go build
installs it only when it was written. The per-package build always read
it. It now returns the header beside the library only when cgo wrote
it. The plan and every exec are as before: only what is read from the
link's output changes.

cHeaderlessCheck builds a cgo package with no //export as c-shared and
as c-archive, compares each with plain go build byte for byte but for
the build IDs, and checks that nothing else is returned.

Signed-off-by: Erik Sipsma <[email protected]>
A build whose -pgo flag names a profile outside the source, such as one
in the base image, failed in a tier:

    preprofile: error creating output file:
        open /work/p.../pgo.preprofile: read-only file system

The tiers build the plan's steps that read nothing from the source, and
such a profile's preprocessing reads nothing from it either, so it was
built in one. But it runs on its own first, and the tiers mount its
output read-only for the compiles that read it. It now stays out of the
tiers, wherever the profile comes from. Every other plan is as before.

pgoCheck builds with a profile a -pgo flag names in the base image and
compares the binary with plain go build: the profile it records, and its
bytes but for the build IDs.

Signed-off-by: Erik Sipsma <[email protected]>
go build uses a main package's default.pgo for profile-guided
optimization unless a -pgo flag says otherwise, but the source mounted
for a module's commands never held it, unless includeExtraFiles listed
it. A build then went on without the profile and said nothing: plain go
build records -pgo=/src/default.pgo in the binary, the module's binary
recorded none. A -pgo flag naming default.pgo failed outright:

    preprofile: error opening profile: open /src/default.pgo:
        no such file or directory

The module's source now includes **/default.pgo, in the scanner's
patterns and in includeBase. A module with no default.pgo mounts the
same files as before.

pgoCheck also builds with the profile go finds as default.pgo, and with
a -pgo flag naming it, and compares each binary with plain go build.

Signed-off-by: Erik Sipsma <[email protected]>
beta.20's schema adds a workspace main (Workspace.main, withMain,
withoutMain, WorkspaceModule.main and Module.serve's main argument) and
renames "entrypoint" to it in descriptions, so the generated client is
stale against it. It only adds: no type, field or argument the client
had is gone, and gobuild calls none of the new ones, so it sends the
same queries to engines from v1.0.0-beta.17 on as before.

Signed-off-by: Erik Sipsma <[email protected]>
A package's test binary was built only by go test itself, one exec over
everything with the Go cache in a volume. GoPackage.testBinary and
GoPlatform.testBinary now build it as binary builds a binary: gobuild's
TestBinary plans "go test -c -n" and replays the plan one cached exec
per package, so an edit re-runs only the packages it reaches and the
link, and every package compile is a result the remote cache can share.
buildFlags reach it as they reach go test -c, so -race, -ldflags (whose
-X values reach only the link) and tags work as for a binary.

- A test plan compiles the package with its test files, its external
  test package and the packages it is imported through, against the
  test variant; those are blocks like any other. Its main package's work
  directory is made before any other's, so its steps (with -cover, a
  cover run before its header) and its header come long after its
  mkdir: a line working in a block that has no header yet is that
  block's, and a header waiting names it. The main package is the block
  that links: b001, or with -coverpkg another.
- The go command prints a path under the directory a command runs in
  as "./" and runs it whole. A cover run in a work directory writes the
  path it is given, a test main's _testmain.go, into the line
  directives of what it generates, so it gets the whole path.
- The plan names the test main's source, _testmain.go, but never holds
  it: the go command writes it before running any tool. One exec runs
  go test -c with -work and -toolexec=false, which stops before the
  first tool, and keeps the file. It reads the plan's view of the source
  with the package's own test files whole, which name the tests and
  examples, so it re-runs when they change. The test main's exec gets it
  in its work directory.
- The test link writes the binary where it stays; go test copies it to
  -o itself. Its output is read from the link.
- Unlike go test -c, it does not run go vet on the package: vet is left
  to the lint checks.
- A package has a test binary when it has test files, as for go test
  -c, whatever they hold.
- A gccgo build is one whole go test -c, as for a binary.

testBinaryCheck compares the module's test binary with plain go test -c
-vet=off of the same source: byte for byte but for the build IDs, with
and without -race and an -ldflags stamp, with -cover and -coverpkg, and
for linux/arm64, and for packages whose test files hold only an aliased
testing import, a benchmark or a TestMain. The
package has internal and external tests, a dependency only its tests
import, an embed in a test file, and an importer recompiled against its
test variant. The binaries list the same tests and pass and fail the
same ones, -race finds the race in TestRace, and the stamp reaches the
test. TestParsePlanTest and TestParsePlanTestCover cover test plans'
shapes.

Signed-off-by: Erik Sipsma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants