Skip to content

dagql: share the egraph lock on cache-hit lookups, session tracking and ID loads - #14892

Merged
sipsma merged 4 commits into
mainfrom
sipsma/dagql-egraph-shared-lock
Oct 28, 2026
Merged

sipsma merged 4 commits into
mainfrom
sipsma/dagql-egraph-shared-lock

Conversation

@sipsma

@sipsma sipsma commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

What this changes for users

Rebuilding after an edit is faster. Building dagger's CLI with a per-package Go build, an edit rebuild was about 0.4–0.5 s faster (4–5%). A first build didn't change measurably, and an unchanged rebuild was 20 ms slower (0.6%).

These were measured on an engine built from main as of 27 Oct with #14866 and #14872 applied, not on this branch's base. Since then, #14806 stopped caching reflected primitive getters, which changes how often this lock is taken. So the effect on current main hasn't been measured.

Why

Almost every dagql call takes the cache's global lock, egraphMu, exclusively, several times: cache lookup, session result tracking, publication, handoff release, dependency edges and ID loads. The work done under the lock is small. But with hundreds of concurrent calls, goroutines queue on it, and handing the lock from one to the next costs more than the work.

What changes

The per-call paths that only read the e-graph now hold egraphMu for reading:

  • Cache lookups: a miss, or a recipe-digest hit that teaches nothing, is answered under the read lock, and the hit is claimed for the session in the same section. A hit that must teach the e-graph its identity (extra digests, TTL, persistable, or a structural route) changes nothing under the read lock and repeats the lookup under the write lock, as before.
  • Session result tracking (trackSessionResult) holds the read lock.
  • ID loads (sharedResultByResultID), including the canonical-equivalent search, hold the read lock.

What those sections write has its own synchronization:

  • the session edge is recorded under sessionMu;
  • last-used stamps are written under the result's payloadMu;
  • the incoming ownership count is now atomic.

Every decrement, and every check that drives collection, still takes the write lock, so no claim can run concurrently with them. The read paths find e-graph roots with the existing non-mutating lookup, because the usual find compresses paths, which is a write.

Measurements

dagger's CLI with dagger.io/go's per-package build, on the earlier engine described above (main as of 27 Oct with #14866 and #14872), with and without this change. One 16-core AWS machine (c8gd.4xlarge), a fresh engine per run, 3 interleaved runs per arm, medians:

Build Without With Difference
First build 33.59 s 34.05 s +0.46 s, runs overlap
Edit inside the module 11.32 s 10.94 s −0.38 s, every run faster
Edit of the CLI package 9.89 s 9.38 s −0.51 s, every run faster
Unchanged rebuild 3.44 s 3.46 s +0.02 s, every run slower
A second binary afterwards 54.22 s 54.10 s −0.12 s, noise
  • The edit gains are consistent but not proven. With 3 runs per arm, every run of one arm beating every run of the other means p ≈ 0.1.
  • The first build hasn't moved. In separate profiled runs it leaned the other way: 32.7–32.9 s with the change, against 33.3–33.5 s without.

Mutex delay, summed over the engine's goroutines, from separate runs with Go's mutex profiling (2 per arm):

Without With
First build, all mutexes 63–68 s 48–50 s
First build, held by ID loads and session tracking 18–23 s under 1 s
Each edit, all mutexes 29–38 s about 8 s

Testing

  • The dagql race suite passed with the same change code on that earlier engine: engine-dev test --pkg ./dagql --race, 1,198 tests. CI runs it on this branch.
  • New tests:
    • TestCacheReadPathsShareEgraphLock fails without the change;
    • TestCacheConcurrentSharedClaimsKeepOwnershipExact checks that concurrent claims under the read lock, interleaved with releases, keep ownership counts exact.

The TLA+ model

The cache model in dagql/tla treated every critical section as one action. With read sections now able to overlap, that's sound only if they commute, so the model now checks it:

  • The new constant SharedSections:
    • "atomic" keeps every existing configuration's state space exactly as before;
    • "split" models the lookup hit and the ID-load claim each as a selection step and a claim step, with other read sections, release marking and lock-free steps free to run between them;
    • "split_stale_edge" is a deliberate mutation that must violate OwnershipExact.
  • All four shared_claims configurations are new with this PR:
    • shared_claims passes at its full bounds (two sessions, three invocations, release): about 205 million distinct states.
    • shared_claims_prune adds prune and persistable requests. With both sessions able to release, it never finished: after 100 minutes at ten workers it was at 270 million distinct states, with the queue still growing. It now lets only one session release, and passes with about 309 million.
      • What that gives up: the second session's release interleavings with prune.
      • Where they're covered: shared_claims covers both sessions releasing, without prune.
    • shared_claims_resources covers the ID-load claim's split on an imported graph.
  • The mutation runs at each configuration's exact bounds. shared_claims_stale_edge and shared_claims_prune_stale_edge both violate OwnershipExact within seconds. Each counterexample has two lookup hits selected and unclaimed at once, which shows those bounds let two hits overlap.
  • The existing configurations use "atomic", so their state spaces are unchanged. They weren't all rerun for this PR.

@grouville
grouville self-requested a review October 28, 2026 02:34
…nd ID loads

Every dagql call took the cache's global egraphMu exclusively, several
times: cache lookup, session result tracking, publication, handoff
release, dependency edges and ID loads. The work under the lock is small,
but with hundreds of concurrent calls the lock becomes a hand-over queue.
Building dagger's CLI with a per-package Go build, the engine's goroutines
waited 63-68 s in total on mutexes during a first build, and 29-38 s
during an edit rebuild, most of it on this lock.

The per-call paths that only read the e-graph now hold it for reading:

- lookupCacheForRequest answers a miss, or a recipe-digest hit that
  teaches nothing, under the read lock, and claims the hit for the
  session in the same section. A hit that must teach the e-graph its
  identity (extra digests, TTL, persistable, or a structural route)
  changes nothing under the read lock and repeats the lookup under the
  write lock, as before.
- trackSessionResult and sharedResultByResultID (ID loads, including the
  canonical-equivalent search) hold the read lock.

What those sections write has its own synchronization: the session edge
is recorded under sessionMu, last-used stamps under the result's
payloadMu, and the incoming ownership count is now atomic. Every
decrement, and every check that drives collection, still holds the write
lock, so no claim can run concurrently with them. The read paths use the
existing non-mutating eq-class root lookup instead of the path-compressing
find.

Signed-off-by: Erik Sipsma <[email protected]>
The cache model treated every critical section as one action. With the
lookup hit, session tracking and ID-load claims now holding egraphMu for
reading, read sections can overlap each other. Modeling each as one
action is sound only if they commute; this makes the model check that.

A new constant SharedSections selects how they are modeled. "atomic"
keeps every existing configuration exactly as before. "split" models the
lookup hit and the ID-load claim each as a selection step and a claim
step, with other read sections, release marking and lock-free steps free
to run between them and every write-locked step excluded until the claim
(WriteLockAvailable). "split_stale_edge" is a deliberate mutation, the
claim trusting the session edge it read at selection, which must violate
OwnershipExact.

New configurations: shared_claims (two sessions' overlapping hits and a
release), shared_claims_prune (two sessions' overlapping hits with prune
and persistable requests), shared_claims_resources (resources_latedep_recheck split, with
ID loads on an imported graph) and the mutation shared_claims_stale_edge.

Signed-off-by: Erik Sipsma <[email protected]>
Release marking and its sessionMu snapshot do not take egraphMu, so they
can run while a read section is open; only release cleanup holds the
write lock. The claim's tombstone check orders the claim against them.

Signed-off-by: Erik Sipsma <[email protected]>
…ounds

shared_claims_prune had not finished after 100 minutes at ten TLC
workers (270 million distinct states, the queue still growing). It now
lets only s1 release, with call-only symmetry, and passes with about 309
million. It keeps three invocations, so two lookup hits still overlap:
s1's release marking and snapshot can interleave with s2's open read
section, while cleanup and prune wait until every read section has
closed. s2's release interleavings with prune are no longer explored
there; shared_claims covers both sessions releasing, without prune.

The stale-edge mutation is what shows a configuration can observe two
overlapping read sections, so it has to run at that configuration's exact
bounds. shared_claims_stale_edge had nested calls on while shared_claims
has them off; it now matches shared_claims exactly. The new
shared_claims_prune_stale_edge does the same for shared_claims_prune.
Both violate OwnershipExact within seconds, with two lookup hits
selected and unclaimed at once.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/dagql-egraph-shared-lock branch from 8322a88 to a963b62 Compare October 28, 2026 02:38
@sipsma
sipsma merged commit 9c3b527 into main Oct 28, 2026
92 checks passed
@sipsma
sipsma deleted the sipsma/dagql-egraph-shared-lock branch October 28, 2026 02:54
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