Skip to content

dagql, core: memoize the Module results a call frame references - #14920

Merged
sipsma merged 3 commits into
mainfrom
sipsma/dagql-module-ids-memo
Oct 28, 2026
Merged

sipsma merged 3 commits into
mainfrom
sipsma/dagql-module-ids-memo

Conversation

@sipsma

@sipsma sipsma commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Every load of an object by its ID (node(id:)) worked out which modules the object depends on by walking its whole call graph. A Go build with one exec per package makes thousands of these loads over deep graphs, and that walk was most of each load's cost. This change memoizes the answer on each call frame, so repeated loads reuse it.

What changes for users

  • Incremental builds that make thousands of cache-hit requests get through them faster: about 0.13 s off each edit's pass over its cached packages (−20%).
    • Setup: an edit of dagger's own CLI package, rebuilt with one exec per Go package. Almost all of the edit's ~3,500 requests are cache hits before the changed package compiles. 5 builds against 5, interleaved on one AWS c8gd.4xlarge (16 cores, arm64), each starting from a fresh engine and a first build. Both engines also carried other pending engine changes, among them cheaper locking for cache hits, so the numbers are this change's effect on top of those.
    • What's measured: the pass over cached packages, from the build's first request to its first real compile, read from the client's own timing of each request. That pass is bound by the build's 12 concurrent package slots, so it shrinks with the time each package holds a slot.
Pass over cached packages (s) Median Mean slot hold per package
Without this change 0.521, 0.673, 0.658, 0.695, 0.615 0.658 5.26 ms
This change 0.524, 0.482, 0.531, 0.525, 0.505 0.524 4.30 ms (−18%)
  • An edit of a package deeper in the tree shows the same: −24% slot hold per cached package, about −0.18 s for its pass.

  • The whole edit build is ~9–12 s, so ~0.13 s is within its run-to-run spread.

  • First builds don't change: 31.2 s at the median with the change, against 32.7 s without, 5 each. The engine's peak memory is unchanged: 1,153–1,256 MB with it, against 1,189–1,231 MB.

  • Nothing else changes for users. The same modules are found, in the same order, so the schema each load uses is unchanged.

How

  • dagql:
    • Cache.ModuleResultIDs returns the result IDs of the Module results a call frame's graph references, in the order WalkResultCall visits them.
    • It's memoized on the frame, built from the answers of the frames it references.
    • Each call computes a frame at most once, as WalkResultCall visits a frame once. It records every frame it computes, including those it can't memoize, and reuses them for the rest of the call.
    • A canceled request stops it, checked every 256 frames computed.
  • Why a memo can't go stale:
    • A stored frame isn't edited after it's stored; the cache replaces it.
    • A referenced result's frame can be replaced by an equivalent call that references different Module results, because equivalence goes by e-graph class, not result identity. So each memo holds only for the frame generation it was computed in, and replacing any frame starts a new generation.
    • Result IDs are never reused, so a memoized ID can't name a different result.
    • If a call graph ever had a cycle, nothing above it is memoized, since its answer would miss its ancestors' modules.
  • core.Query.ModDepsForCall uses it instead of walking. Loading the modules for the requesting session's schema still happens on every load, as before.

A stall found before this PR, and fixed

An earlier version of this change sometimes stalled a first build: one took 132 s instead of ~30, with the engine busy throughout.

  • The cause, found in the code and reproduced in a test (the stall never recurred under a profiler): that version computed a frame again whenever its memo wasn't usable, on every path that reached it. A package import graph shares much of itself, so that work grows far faster than the graph. A first build replaces frames thousands of times, so loads from different generations run at once. Each one overwrote the memos the others had just computed, and the others then recomputed those frames on their next path to them. The test's graph of 129 frames took 2,145 frame computations. Frames above a cycle, which are never memoized, would have been recomputed the same way, but no measured build had a cycle.
  • The fix: each call records every frame it computes and never computes a frame twice, whatever happens to the memos.
  • Since the fix: no stall in 10 watched first builds (a watchdog recorded any stall over 60 s).

Tests

dagql/result_call_module_ids_test.go:

  • Equivalence with the walk: the answer matches WalkResultCall's, in the same order, including inline and zero-ID references, and on 200 random shared graphs memoized out of order.
  • Replacement: replacing a referenced frame invalidates memos, including one computed before the replacement and stored after it. A missing frame is an error, as in the walk.
  • Cycles: a cycle stores no partial memo.
  • Each frame computed once per call: under a cycle; while calls of other generations overwrite the memos; and when one such overwrite lands between two paths to the same frame, which is forced deterministically.
  • Cancellation: a canceled call stops with the context's error.
  • Concurrency: lookups stay correct while frames are replaced concurrently.

They run under -race with dagql's other tests.

@sipsma
sipsma force-pushed the sipsma/dagql-module-ids-memo branch from 3f5941a to e033e8e Compare October 28, 2026 22:04
core.Query.ModDepsForCall walked the receiver's whole call graph on every
handle load (node(id:)) to find the Module results it references. For a
package exec in a per-package Go build, that graph reaches into every
imported package's graph, and each frame visited took the cache's read lock.

The cache now answers which Module results a frame references
(Cache.ModuleResultIDs), memoized on the frame and built from the answers of
the frames it references. A stored frame's own fields don't change, but a
referenced result's frame can be replaced by an equivalent one that
references different Module results, so each memo holds for the frame
generation it was computed in; replacing a frame starts a new generation.

Each call records every frame it computes and reuses that for the rest of
the call, so it computes each frame at most once, as WalkResultCall visits
each frame once. Without that, frames above a cycle (kept out of the memo,
since their answer would miss their ancestors' modules) and frames whose
memo a call of another generation overwrote would be recomputed on every
further path to them, exponentially in how much of the graph is shared. The
call also stops with the request's error once the request is canceled.

Signed-off-by: Erik Sipsma <[email protected]>
ModuleResultIDs must return what WalkResultCall finds, in the same order,
including inline and zero-ID references and on shared graphs memoized out of
order. A referenced frame's replacement must invalidate memos, including one
computed before the replacement and stored after it; a cycle must store no
partial memo; and lookups must stay correct while frames are replaced.

A call must compute each frame once: under a cycle, while calls of other
generations overwrite the memos, and when one such overwrite lands between
two paths to the same frame. A canceled call must stop.

Signed-off-by: Erik Sipsma <[email protected]>
moduleResultIDs carried the walk over a frame's references as closures,
which put it over gocyclo's limit. The walk of one frame's references now
lives on moduleIDsFrameWalk; what it computes and in what order are
unchanged.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/dagql-module-ids-memo branch from e033e8e to 1d40e9c Compare October 28, 2026 22:30
@sipsma
sipsma merged commit 87447c3 into main Oct 28, 2026
93 checks passed
@sipsma
sipsma deleted the sipsma/dagql-module-ids-memo branch October 28, 2026 23:00
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