Skip to content

go: a faster workspace scan, edit walk and module extraction - #91

Merged
sipsma merged 17 commits into
mainfrom
sipsma/faster-workspace-scan
Nov 2, 2026
Merged

sipsma merged 17 commits into
mainfrom
sipsma/faster-workspace-scan

Conversation

@sipsma

@sipsma sipsma commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Three changes that make building Go with this module faster after an edit and on a first build, combined into one PR to save CI runs. Each keeps its own commits.

What users get. Building dagger's CLI with the go module:

Change Edit inside a dependency package Edit of the main package First build
A faster workspace scan −0.79 s −0.93 s about −0.85 s
A faster build after an edit −0.18 s, plus −0.15 s from the binary read-out −0.17 s, plus −0.12 s −0.38 s from the binary read-out
gobuild mounts a package's imports with Container.withMountedFiles — — −0.8 s
gobuild extracts modules with File.extract — — 12 CPU-s less engine work

Nothing measured got slower. Each change was measured on its own, against the tree without it, on 16-core arm64 machines with a fresh engine per run; the numbers aren't measured together.

gobuild now needs engine v1.0.0-beta.22. It declares engineVersion = "v1.0.0-beta.22" instead of v1.0.0-beta.17, the first release with File.extract and Container.withMountedFiles.

One behavior change. A syntax error after a Go file's imports no longer makes the workspace scan fail that file's whole module. The compiler, linter or test run reports it instead, as with go build, and the module's other packages still build. A syntax error in the package clause or the imports still fails the module's scan. One case this fixes: github.com/Khan/genqlient keeps a deliberately broken file under testdata/, which Go never builds, and that file used to make the scan refuse the whole genqlient module.

A faster workspace scan

gomod's workspace scan (go-includes --all) reruns on every edit of a Go file, and the go module's packages call, which reads it, runs before every build. The scan is now about 10x faster, and the packages call about 0.2 s faster on every build that compiles, with the same output.

  • One pass over the files. The scan read each Go file up to three times and parsed it twice, and spread its work over the CPUs one module at a time. Now each file is read once, over all modules at once, and the header pack, discovery and directives all come from that.
  • Mount the input. The scanner's workspace input is mounted into its container rather than copied.
  • Parse only up to the imports, as go/build does. The full parse was nearly all of what remained: about 3.3 s of CPU per scan of the dagger repository's 4,311 Go files. Directive comments (//go:embed, //go:generate and the module's own) are found with a lexical pass that skips strings, runes and block comments, as go/build's own search for //go:embed does. A file that may hold a //line directive goes through go/scanner, so positions stay exactly what a parse gives.
  • Parse each go.mod once, instead of five or more times.
  • Less work in the go module's packages. Discovery writes its package lines in byte order of their workspace paths, so packages no longer sorts them, and each package line is split once instead of once per field.

On every build that compiles, each run with these changes had a shorter packages call than every run without them. The scan exec itself went from about 1,015 ms to 286 ms on each edit with the first two changes, and the rest took it from about 276 ms to 89 ms, measured on a newer engine.

A faster build after an edit

On an edit of a large project, almost every package gobuild builds is a cache hit. Most of what's left is gobuild's own setup before the walk, and reading the finished binary out.

  • Read a binary out without hashing it twice. gobuild and the go module each read the link's binary from an evaluated directory, which hashes all of it: for dagger's CLI, ~150 MB and ~70 ms per read on the critical path. Both now read it by its recipe, already keyed by the content of what the link reads.
  • Render the packages' scripts at once, in parallel, instead of one after another before the walk could start.
  • Read the plan without regexps on lines they can't match. Each regexp and shell word split now runs only on lines that could match it. On dagger's CLI plan (31,340 lines), reading it went from 49 to 27 ms, with the blocks and scripts byte for byte the same.
  • Filter the plan's source while the rest of the setup runs, rather than after the module download it doesn't depend on.
  • Run gobuild's unit tests in a new gobuildCheck, the way gomod's helperCheck runs the scanner's.

gobuild on File.extract and Container.withMountedFiles

  • Imports mounted where they are. Each package's exec mounts the archives of the packages it imports with Container.withMountedFiles, read-only, instead of first copying them into a new directory. The engine makes no copy and no snapshot for each package.
  • Modules extracted by the engine. Each downloaded module's directory is its zip from the build's own download, extracted by File.extract, instead of a container exec per module version. The directory is still keyed by the zip's content, so every build reading that module version shares it. The checksum check the exec made before extracting is gone: the go command already verified the zip when the build downloaded it. Files come out 0644 and directories 0755 rather than read-only, and are mounted read-only as before. gohdr's modunzip goes with it.

Checked

  • gobuild's vet and unit tests pass, and so do the gomod, golangci-lint and staticcheck suites.
  • The scanner's unit tests pass on Go 1.26 and 1.27. New tests compare the lexical directive search with a full parse, on tricky inputs and on every file of the Go toolchain's own source that parses, and check discovery's order.
  • On the dagger repository, the scanners before and after write the same output in all four modes, apart from the intended order of discovery's package lines. packages returns the identical list of 198 packages.

@sipsma
sipsma force-pushed the sipsma/faster-workspace-scan branch from ba9f23b to be6e0f2 Compare November 2, 2026 19:36
@sipsma sipsma changed the title gomod: a faster workspace scan go: a faster workspace scan, edit walk and module extraction Nov 2, 2026
sipsma added 17 commits November 2, 2026 20:48
The workspace scan read every Go file up to three times and parsed it
twice: once in full for its directives, once to its imports for the
header pack, and once line by line for discovery. It also spread the
work over the CPUs one module at a time, so the many small modules of a
workspace left most CPUs idle.

Read and parse each file once, over all modules at once, and draw the
header pack, discovery and directives from that one result. The header
comes from the full parse when it succeeds, and from an imports-only
parse as before when it does not. Discovery tries each pattern only on
lines with its literal prefix.

Signed-off-by: Erik Sipsma <[email protected]>
The scan only reads its input, so the copy into the container's
filesystem was work on every edit for nothing.

Signed-off-by: Erik Sipsma <[email protected]>
The workspace scan parsed every Go file in full, and that parse was
nearly all of its time: on the dagger tree, about 3.3 s of CPU per scan,
mostly the parser and the collections its syntax trees cause. The scan
runs again on every edit, since any change to the source is a new input.

It needed the full parse only for its directive comments, which can sit
anywhere in a file. Find those as go/build finds go:embed lines instead:
a pass that skips strings, raw strings, runes and /* comments, and takes
each // comment that starts with go:. Positions come from the file's
line offsets, and a file that may hold a //line directive goes through
go/scanner, which applies it. The parse now stops after the imports, as
go/build's does, and still gives the header.

On the dagger tree the output is byte for byte the same in every mode,
and the scan takes about a fifth of the CPU it did.

One behavior changes: a syntax error after a file's imports no longer
makes the scan fail the whole module. The compiler, linter or test run
reports it, as with plain go, and the module's other packages still
build. An error in the imports still fails the module, as before. A
deliberately broken file under genqlient's testdata, which go never
builds, failed that whole module's scan.

Signed-off-by: Erik Sipsma <[email protected]>
The README still said a Go file that will not parse fails its module's
scan. Since the scan parses only up to the imports, an error after them
is the compiler's to report.

Signed-off-by: Erik Sipsma <[email protected]>
The go module's packages call sorted every package path with a merge
sort written in Dang, on every build, unchanged rebuilds included. On
the dagger tree that sort was about 14 ms of the call.

The scanner already has every package line in hand, so it now writes
them in byte order of their workspace paths, after all the module
lines, and packages takes that order as it is. The module lines keep
their order. The other reader of package lines only asks whether a
directory is among them.

Signed-off-by: Erik Sipsma <[email protected]>
packageKeys took each field of a package line with a split of its own:
two splits for every package line, and three more for each one
selected. Split each package line once and read its fields from that.

Signed-off-by: Erik Sipsma <[email protected]>
The scan read and parsed a module's go.mod at least five times: twice
to decide whether it is a module, once for discovery's module path,
twice for its Go version, and again for its local replaces each time a
module's includes reached it. A module that many others replace into,
such as a repository's root, was parsed again for every one of them. On
the dagger tree, whose modules mostly replace into the root, that was
about 25 ms of the scan, all on one CPU.

Keep each go.mod's parse, or why it failed, the first time it is asked
for. The errors and their text are the same.

Signed-off-by: Erik Sipsma <[email protected]>
Building a binary hashed the whole of it twice per link: gobuild read it
from the link's evaluated output directory, which keys a file by its
content, and the go module read it back out of the evaluated directory
gobuild returned. For dagger's CLI, a ~150 MB binary, each read took about
70 ms on the build's critical path.

No exec reads the link's output, so gobuild now reads it from the
unevaluated directory, and a package's binary is read from gobuild's
directory before that is evaluated. Both are keyed by their recipe, whose
link exec is itself keyed by the content of what it reads.

Signed-off-by: Erik Sipsma <[email protected]>
gobuild renders every package's script before the walk starts, one
after another. On an edit of dagger's CLI that was ~0.14 s of gobuild's
own time on the build's path, with no engine request in flight: the
walk can't start until it's done. Each package's script is its own, so
they're now rendered at once.

Signed-off-by: Erik Sipsma <[email protected]>
Reading the plan and rendering scripts run several regexps and a shell
word split on every line of the plan, ~31,000 lines for dagger's CLI,
most of which can't match: an import config's lines have no "./" or
"-o", and few lines have a -buildid. Each is now skipped where the line
lacks the text it needs, a directory change already found needed is no
longer checked again, and a line with no quotes or escapes is split on
its blanks.

The plan's blocks and scripts for dagger's CLI come out byte for byte
the same. Rendering them one after another went from ~230 ms to
~130 ms on an x86 host.

Signed-off-by: Erik Sipsma <[email protected]>
The plan mounts the source's files that aren't Go, filtered from it.
The engine makes that filter a copy, and on an edit of dagger's CLI it
took 0.12 s, which the plan waited for: it started only once the module
download was ready, ~50 ms into the setup. It doesn't depend on any of
that, so it now starts as the setup does.

Signed-off-by: Erik Sipsma <[email protected]>
No check ran gobuild's own Go tests, so a change to how it reads the build
plan or writes its scripts was checked only through the builds it makes.
gobuildCheck runs them, the way gomod's helperCheck runs the scanner's.

Signed-off-by: Erik Sipsma <[email protected]>
Reading the plan ran up to eight regexps and a shell word split on every
line, ~31,000 for dagger's CLI, before the walk can start. Most lines
can't match most of them: an import config's lines name only work
directories. Each regexp now runs only on lines with the text every match
starts with or holds, a line is split into words only when it could name a
source or module file, and the work directories a line names are found
without a regexp.

On a plan of dagger's CLI, reading it went from 49 to 27 ms, with the
blocks and scripts byte for byte the same. It runs while the engine waits
for the walk, on every build that plans.

Signed-off-by: Erik Sipsma <[email protected]>
Each package's exec mounts the archives of the packages it imports with
Container.withMountedFiles, instead of first assembling them into a new
directory with Directory.withFiles and mounting that. The engine mounts
the files read-only where they are, with no copy and no snapshot made for
each package.

Signed-off-by: Erik Sipsma <[email protected]>
Each downloaded module's directory is now its zip from the build's own
download, extracted by the engine's File.extract, instead of a container
exec running gohdr modunzip: one fewer exec per module version. The
directory is still keyed by the zip's content, so every build reading
that module version shares it.

The checksum check modunzip made before extracting is gone. It compared
the zip with the .ziphash beside it in the same module cache, and the go
command had already verified the zip when the build downloaded it. Files
come out 0644 and directories 0755 rather than read-only, and are
mounted read-only as before. A Go toolchain module needs no exec to make
its commands executable any more: its zip records their modes, which
File.extract keeps.

gohdr's modunzip and its test go with it.

Needs an engine with File.extract.

Signed-off-by: Erik Sipsma <[email protected]>
gobuild extracts modules with File.extract and mounts a package's
imports with Container.withMountedFiles. Both first ship in
v1.0.0-beta.22, so gobuild now declares that engine version rather than
v1.0.0-beta.17.

Signed-off-by: Erik Sipsma <[email protected]>
The generator's output for the v1.0.0-beta.22 API that gobuild now
declares: Container's asTarball, export, exportImage and publish take an
optional sourceDateEpoch. gobuild calls none of them.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/faster-workspace-scan branch from 3a348e0 to 1c35df8 Compare November 2, 2026 20:48
@sipsma
sipsma merged commit ec1fc4f into main Nov 2, 2026
80 checks passed
sipsma added a commit to dagger/dagger that referenced this pull request Nov 4, 2026
Move the workspace's Go module from dagger/go#84's merge commit to the
v0.4.0 release. That brings in Go 1.27 (dagger/go#90), the faster
workspace scan (dagger/go#91) and the module discovery index and test
check cuts (dagger/go#89).

dagger.lock records the tag and the sub-modules the release resolves,
as written by dagger.

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