Skip to content

Speed up package discovery in large workspaces - #72

Merged
sipsma merged 5 commits into
mainfrom
perf/merge-sort
Oct 21, 2026
Merged

sipsma merged 5 commits into
mainfrom
perf/merge-sort

Conversation

@sipsma

@sipsma sipsma commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

This makes package discovery much cheaper on large repositories. Building one binary from the dagger repository takes about 77 s even with nothing to rebuild, and most of that is Go.packages discovering the packages of every module. Five commits:

Merge sort. sorted() inserted items one at a time with takeWhile and dropWhile. That is about n² comparisons, each a call into a Dang closure. ownFiles sorts every Go file of a module with it. In the dagger repository, whose root module has about 4,000 Go files, the root module's list was sorted three times per call at about 10 s each. All three copies of sorted() become a merge sort whose merge walks the two halves once with map. A sort takes n log n comparisons and copies each list once per level, and the result is the same byte order as Go's sort.Strings. Sorting the dagger repository's 4,004 Go file paths with the engine's Dang took 20.8 s with the old sort and 1.0 s with this one. Those times are for the sort run on its own, including Dang start-up.

Nested modules by set lookup. ownFiles left out files that belong to nested modules by comparing every file with every nested module root. For a repository's root module that is files times modules closure calls, about 4,000 times 80 on the dagger repository. It now looks up each directory above a file in a set of the nested roots.

Each module's files listed once. Gomod.modules listed every module's Go files to check that the module holds any, sorting a list it only needed the length of. Then Go.packages listed them again through a separate call into gomod for each module. Gomod.modules now lists them once, unsorted, and returns them as GoMod.goFiles, and Go.packages uses them without calling gomod per module. isGoModule still lists the files of the workspace it is given, and a GoMod from at or module has no goFiles.

Test files by a files-only search. To find packages with tests, discovery only needs which _test.go files declare a test, fuzz or example function. It read every matching function's path and line through the search API instead: one result per function and two field reads on each, thousands per call on the dagger repository. It now asks the search for files only and reads just their paths. Test names still read the matched lines, one package at a time.

One marker search read once per module. Finding a module's main and go:generate packages ran the same marker search three times, once each for package main clauses, ignore build tags and go:generate commands, and read every result's path and line each time. It is now read once per module and narrowed to each kind in process.

The module loads and discovers yq's packages on a released engine, with the same keys before and after each commit, and builds yq's binary.

Overall

Before is dagger/go main just before this PR; after is this PR. Both ran on one Dagger Cloud engine, with the beta.19 CLI running inside it. Each module was loaded once first and its Go modules downloaded, untimed. Then, per workspace: a first build with an empty Go build cache, a call with no change, a call after a one-line edit, another call with no change, and three discovery-only calls (packages | keys). That is one run each, on a shared engine, so differences of a few seconds, or of 10–20 s on the dagger repository, are noise.

This ran on the PR's third push, without the last commit (one marker search read once per module). A separate discovery-only run of the final head on the dagger repository is listed below.

Building cmd/dagger in the dagger repository (80 Go modules, about 4,000 Go files):

before after
First build 134 s 127 s
After an edit 485 s 52 s
No change 203 s, 284 s 57 s, 65 s
Discovery only 210 s, 142 s, 137 s 47 s, 36 s, 61 s

The first build barely moves here because compiling dominates it. Every call with the old module still spent minutes in discovery, and the edit call took 485 s.

Discovery only on the PR's final head, from a separate run on another Cloud engine: 46 s and 47 s after a first run, all three returning the same 810 package keys as before.

Building yq (one module):

before after
First build 48 s 24 s
After an edit 7.8 s 6.8 s
No change 16.5 s, 7.2 s 5.2 s, 4.3 s
Discovery only 2.3 s, 2.0 s, 2.3 s 2.2 s, 3.2 s, 2.9 s

On a repository as small as yq, discovery was already about 2 s and this PR doesn't change it. So the yq first-build and no-change differences are probably the shared engine, not this PR.

The edit floor that remains on the dagger repository is still mostly discovery. A follow-up PR continues there.

sorted() inserted one item at a time with takeWhile and dropWhile: about
n² comparisons, each a call into a Dang closure. ownFiles sorts every Go
file of a module with it. On the dagger repository, with about 4,000 Go
files in its root module, that one sort takes about 10 s of every call.

A merge sort whose merge walks both halves once with map takes n log n
comparisons and copies each list once per level. Sorting the dagger
repository's 4,004 Go files took 20.8 s before and 1.0 s after, run
standalone with the engine's dang. The order is the same byte order as
Go's sort.Strings.

Signed-off-by: Erik Sipsma <[email protected]>
ownFiles left out files in nested modules by comparing every file with
every nested module root. In a repository's root module that is files
times modules closure calls: about 4,000 times 80 on the dagger
repository, on every call. It now looks each directory above a file up
in a set of the nested roots.

Signed-off-by: Erik Sipsma <[email protected]>
Package discovery listed every module's own Go files twice. Gomod.modules
listed them to check that a module holds Go files, sorting a list it only
needed the length of. Then Go.packages listed them again for each module
through a separate call to gomod, so each module cost one more call into
a Dang module: about 80 on the dagger repository.

Gomod.modules now lists each module's Go files once, unsorted, checks
them there, and returns them as GoMod.goFiles. Go.packages passes them to
each GoModule, so finding a module's packages makes no call to gomod.
isGoModule still lists the files of the workspace it is given, and a
GoMod from at or module has no goFiles.

Signed-off-by: Erik Sipsma <[email protected]>
Package discovery needs only which _test.go files declare a test, fuzz or
example function, but it read every matching function's path and line
through the search API: one result per function, and two field reads on
each. On the dagger repository that was thousands of results per call.

It now asks the search for files only and reads just their paths. The
matched lines are still read where they are used, for a package's test
names.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma sipsma changed the title Sort with a merge sort instead of an insertion sort Speed up package discovery in large workspaces Oct 21, 2026
Finding a module's main and go:generate packages ran the same marker
search three times, once each for package main clauses, ignore build tags
and go:generate commands, and read every result's path and line each
time. The search is now read once per module and its lines narrowed to
each kind in process.

Signed-off-by: Erik Sipsma <[email protected]>
@grouville
grouville self-requested a review October 21, 2026 19:03
@sipsma
sipsma merged commit e69fd7e into main Oct 21, 2026
52 checks passed
sipsma added a commit to sipsma/dagger that referenced this pull request Oct 21, 2026
…retries

The golang workspace module (generate and test checks) still used
dagger/go's pull/45 head, which is frozen at its September merge. Pin it
to the dagger/go main merge of dagger/go#71, which also contains
dagger/go#72:

- dagger/go#71 sets GOPROXY to proxy.golang.org|proxy.golang.org|direct
  in withGoCaches, so a module zip download that the proxy resets
  mid-stream is retried once against the proxy and, on another proxy
  failure, attempted directly from the origin. The check can still fail
  if that fallback fails.
- dagger/go#72 speeds up package discovery.

The source names a commit rather than main because dagger.lock pins are
not applied in Cloud's remote-workspace sessions, so main would float in
CI. The dagger/go main entry in dagger.lock is refreshed to the same
commit with dagger lock update.

dagger/go now organizes checks and generators by package, so their names
change: golang/generate-all/stale becomes golang/packages/generate/stale,
golang/test-all becomes golang/packages/tests/test, and the
golang/generate-all generator becomes golang/packages/generate. Update
the release instructions' generate step to the new generator name.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to sipsma/dagger that referenced this pull request Oct 21, 2026
…retries

The golang workspace module (generate and test checks) still used
dagger/go's pull/45 head, which is frozen at its September merge. Pin it
to the dagger/go main merge of dagger/go#73, which also contains
dagger/go#71 and dagger/go#72:

- dagger/go#71 sets GOPROXY to proxy.golang.org|proxy.golang.org|direct
  in withGoCaches, so a module zip download that the proxy resets
  mid-stream is retried once against the proxy and, on another proxy
  failure, attempted directly from the origin. The check can still fail
  if that fallback fails.
- dagger/go#72 and dagger/go#73 speed up and fix package discovery.

The source names a commit rather than main because dagger.lock pins are
not applied in Cloud's remote-workspace sessions, so main would float in
CI. The dagger/go main entry in dagger.lock is refreshed to the same
commit with dagger lock update.

dagger/go now organizes checks and generators by package, so their names
change: golang/generate-all/stale becomes golang/packages/generate/stale,
golang/test-all becomes golang/packages/tests/test, and the
golang/generate-all generator becomes golang/packages/generate. Update
the release instructions' generate step to the new generator name.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to sipsma/dagger that referenced this pull request Oct 21, 2026
…retries

The golang workspace module (generate and test checks) still used
dagger/go's pull/45 head, which is frozen at its September merge. Pin it
to the dagger/go main merge of dagger/go#73, which also contains
dagger/go#71 and dagger/go#72:

- dagger/go#71 sets GOPROXY to proxy.golang.org|proxy.golang.org|direct
  in withGoCaches, so a module zip download that the proxy resets
  mid-stream is retried once against the proxy and, on another proxy
  failure, attempted directly from the origin. The check can still fail
  if that fallback fails.
- dagger/go#72 and dagger/go#73 speed up and fix package discovery.

The source names a commit rather than main because dagger.lock pins are
not applied in Cloud's remote-workspace sessions, so main would float in
CI. The dagger/go main entry in dagger.lock is refreshed to the same
commit with dagger lock update.

dagger/go now organizes checks and generators by package, so their names
change: golang/generate-all/stale becomes golang/packages/generate/stale,
golang/test-all becomes golang/packages/tests/test, and the
golang/generate-all generator becomes golang/packages/generate. Update
the release instructions' generate step to the new generator name.

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