Skip to content

perf(dagql): share result lists when restoring the cache - #14730

Merged
grouville merged 2 commits into
dagger:mainfrom
grouville:pr/go-collections-restored-index
Oct 27, 2026
Merged

grouville merged 2 commits into
dagger:mainfrom
grouville:pr/go-collections-restored-index

Conversation

@grouville

@grouville grouville commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

While profiling Go collection listing on dagger/dagger, I found that restoring the cache builds a separate result-index tree for every key, even when many keys point to the same results.

An earlier snapshot from the listing experiments had 2,606 keys pointing to 2,605 GomodGoMod results. Restoring that group alone created 6.8 million index entries. The cache accumulated during these experiments; it is not a claim about a typical user's cache.

Share each imported result list between its keys instead. Track which shared lists contain a result so cleanup can remove it from each list once. Later additions stay local to their key, and merging groups preserves what existing keys can find. All access uses the existing cache lock; the disk format stays the same.

Memory on the retained engine cache

I checked this change alone on engine main 14c1942e, using the same retained cache volume. Each snapshot follows a successful small Go collection listing and session cleanup, with no active clients. Telemetry and ordinary GC remain enabled; there is no cache reset or forced GC.

Idle snapshot Main This PR Back on main
Go heap (HeapAlloc) 9.20 GB 0.82 GB 9.17 GB
Heap objects 179.3 million 8.63 million 179.2 million

These are individual memory snapshots in decimal GB, not peak memory or command timings. Switching back to main brings the larger footprint back. Unforced heap profiles attribute several GB to the result-index trees allocated by importPersistedState; that allocation largely disappears with this change. Profile samples can lag GC, so I am not treating their exact byte counts as current heap usage.

The cache contains heavily shared result lists. Smaller caches, or caches whose keys have separate results, will save less. The extra membership tracking also has a cost when there is little to share.

Allocation controls

Earlier component checks on main 3eb7f1ea used identical benchmark code and cache fixtures on both versions:

Cache operation Main This PR
Restore shared lists, 2,049 results total 227.81 MB 25.90 MB
Remove 2,049 restored roots through metadata cleanup 3.951 MB 3.952 MB
Restore 2,048 separate one-result lists 23.686 MB 23.750 MB
Add 2,048 fresh results 15.425 MB 15.358 MB

These measure total allocation for each operation, in decimal MB, from two alternating pairs. The separate-list control costs about 64 kB more. Fresh insertion and cleanup allocations are effectively unchanged in this sample. The host was busy during those checks, so their elapsed times are not latency evidence.

Validation and remaining work

The patch is now rebased onto main 930485687df5c9a7ed521f5585a17b2a36936b17 (beta21), where the full DagQL race suite passes. The tests cover additions to individual keys, group merges, removal from overlapping imported groups, and retaining later additions when an imported group becomes empty. The measurements above remain tied to their recorded earlier commits.

go test -race -count=1 ./dagql

This remains draft. The memory reduction is established for the retained cache shown above, which has many keys sharing result lists. A complete warm Go listing speedup from this change alone on the current Go module is not established.

@grouville grouville changed the title perf(dagql): avoid quadratic restored-cache indexing perf(dagql): avoid duplicating restored cache entries Oct 19, 2026
@grouville grouville changed the title perf(dagql): avoid duplicating restored cache entries perf(dagql): share result lists when restoring the cache Oct 19, 2026
@grouville
grouville force-pushed the pr/go-collections-restored-index branch 2 times, most recently from 5a66952 to 576077e Compare October 19, 2026 19:30
@grouville
grouville force-pushed the pr/go-collections-restored-index branch from 576077e to 009630f Compare October 21, 2026 22:20
@grouville
grouville force-pushed the pr/go-collections-restored-index branch from 009630f to ba2776d Compare October 22, 2026 04:08
@grouville
grouville force-pushed the pr/go-collections-restored-index branch from ba2776d to 9253cb9 Compare October 23, 2026 11:30
@grouville
grouville marked this pull request as ready for review October 27, 2026 17:11
While profiling Go collection listing, I found that restoring the cache copies a result list for every key referring to it. An earlier cache had 2,606 keys pointing to 2,605 module results, which created 6.8 million index entries for that group alone.

Keep one shared list for each imported group and track its members for cleanup. Later additions stay local to their key. Merging groups and removing results preserve what each key can find. Access uses the existing lock and the disk format stays the same.

On main 3eb7f1e, the 2,049-result fixture allocated 227.81 MB before and 25.90 MB after. The control with 2,048 separate lists added about 64 kB. Those are earlier allocation measurements; a current full-listing speedup has not been established.

The full DagQL race suite passes on the beta21-main rebase. Tests cover additions, merges, overlapping imported groups and cleanup that retains later additions.

Signed-off-by: Guillaume de Rouville <[email protected]>
@grouville
grouville force-pushed the pr/go-collections-restored-index branch from 9253cb9 to 59d21e8 Compare October 27, 2026 17:53

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just make sure lint is green before merging.

@grouville
grouville merged commit 4ca3208 into dagger:main Oct 27, 2026
92 checks passed
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.

3 participants