Repository navigation
go: a faster workspace scan, edit walk and module extraction - #91
Merged
Merged
Conversation
sipsma
force-pushed
the
sipsma/faster-workspace-scan
branch
from
November 2, 2026 19:36
ba9f23b to
be6e0f2
Compare
This was referenced Nov 2, 2026
kpenfound
approved these changes
Nov 2, 2026
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
force-pushed
the
sipsma/faster-workspace-scan
branch
from
November 2, 2026 20:48
3a348e0 to
1c35df8
Compare
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Container.withMountedFilesFile.extractNothing 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 withFile.extractandContainer.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 undertestdata/, 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'spackagescall, which reads it, runs before every build. The scan is now about 10x faster, and thepackagescall about 0.2 s faster on every build that compiles, with the same output.//go:embed,//go:generateand 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:embeddoes. A file that may hold a//linedirective goes through go/scanner, so positions stay exactly what a parse gives.packages. Discovery writes its package lines in byte order of their workspace paths, sopackagesno 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
packagescall 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.
gobuildCheck, the way gomod'shelperCheckruns the scanner's.gobuild on
File.extractandContainer.withMountedFilesContainer.withMountedFiles, read-only, instead of first copying them into a new directory. The engine makes no copy and no snapshot for each package.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'smodunzipgoes with it.Checked
packagesreturns the identical list of 198 packages.