Skip to content

dagql: keep repeat cache hits under the egraph read lock - #14921

Closed
sipsma wants to merge 6 commits into
mainfrom
sipsma/dagql-hit-read-lock
Closed

sipsma wants to merge 6 commits into
mainfrom
sipsma/dagql-hit-read-lock

Conversation

@sipsma

@sipsma sipsma commented Oct 28, 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 inside the module was 0.14 s faster and an edit of the CLI's own package 0.20 s faster. The walk over cached packages before the first real compile was 0.136 s shorter. A first build and an unchanged rebuild didn't change.

These were measured on an engine built from main as of 27 Oct with #14866 and #14872 applied, not on this branch's base. The lock code this change touches is the same on both.

Why

In an edit build, nearly every request is a cache hit. #14892 let most hits share the cache's global lock, egraphMu, but some hits still took it for writing, and so did usesPartAcquisition for every result that hosts parts. Writers make every reader wait, so the request goroutines queued behind them.

What changes

A hit now takes the write lock only when it actually changes something:

  • Identity teaching. A hit outside the plain recipe-digest fast path (extra digests, TTL, persistable, or a structural route) used to repeat its lookup under the write lock, to teach the e-graph its identity. It now first checks, under the read lock, whether teaching would change anything. On a repeat hit it wouldn't: the request's digests are already in the result's single output class, the result is already associated with the request's term, every term with that digest already points at that class, the result is already posted under its recipe and extra digests, and the expiry is unchanged. Then the hit is answered and claimed under the read lock. Otherwise it repeats the lookup under the write lock, as before.
  • Persisted edges. A persistable hit's persisted-edge upsert takes the write lock only when the edge is missing or its expiry would change.
  • Part acquisition. usesPartAcquisition answers under the read lock whenever its answer needs no write; only starting to manage a part gate takes the write lock.

The read-only checks find equivalence-class roots without path compression, which is a write, and answer "changes something" whenever a step would write. Each check's read point is where the hit is ordered: a write that lands after it orders after the hit, which was already a legal order.

Measurements

dagger's CLI with dagger.io/go's per-package build, on the earlier engine described above, 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
Edit inside the module 10.67 s 10.53 s −0.14 s, every run faster
Edit of the CLI package 9.19 s 8.99 s −0.20 s, every run faster
That edit's walk over cached packages, to the first real compile 0.700 s 0.564 s −0.136 s, every run faster
Mean time each cached package holds its build slot 5.73 ms 4.60 ms −20%, every run faster
First build 30.31 s 30.31 s no change
Unchanged rebuild 2.22 s 2.22 s no change

Lock waits per edit, summed over request goroutines, from separate runs with Go's block profile:

Without With
On the read lock 1.39–1.40 s 0.23–0.56 s
On the write lock 0.67–0.76 s below the profile's display threshold

Testing

  • The dagql race suite passes on this branch: engine-dev test --pkg ./dagql --race, 1,234 tests. So do the core package tests, 2,569 of them.
  • New tests in TestCacheReadPathsShareEgraphLock and TestCachePartAcquisitionAnswersUnderReadLock. Each runs the path while another goroutine holds the read lock, so a path that takes the write lock fails:
    • a repeat persistable hit;
    • usesPartAcquisition with no part state, with an unmanaged gate, and with a managed gate.
  • A reworked test: TestSnapshotOwnerWaitsForWholeContainer guards an earlier deadlock fix, and it built that deadlock by waiting for usesPartAcquisition to queue for the write lock. This change takes that call off the write lock. The test now queues a real graph writer behind the diagnostic's read lock and waits for the evaluation's read lock to queue behind that writer. A queued writer can always do this on a busy engine. With the deadlock fix reverted, the test still fails.

The TLA+ model

  • What changed in the model: PersistHit now needs the write lock only when it changes the result, i.e. when the result is registered and not yet persisted. Identity teaching and part gates aren't modeled, so those two changes need no model update.
  • Which configurations it can affect: only those in the "split" sections mode with persistable requests. In the "atomic" mode, write sections are never blocked, so every other configuration is unchanged.
  • Results for those configurations:
    • shared_claims_prune passes, at 309,115,892 distinct states, the same count as before the model change;
    • shared_claims_resources passes, at 52,014,149;
    • the mutation shared_claims_prune_stale_edge still violates OwnershipExact, as it must.
  • Not rerun: the other configurations.

@sipsma
sipsma force-pushed the sipsma/dagql-hit-read-lock branch 4 times, most recently from 3ea308c to 6f68e1a Compare October 30, 2026 19:01
A cache hit still took egraphMu for writing on two paths that, for a
repeat hit, change nothing: a hit that might teach the e-graph its
identity (extra digests, TTL, persistable, or a structural route)
repeated its lookup under the write lock, and a persistable hit upserted
its persisted edge under the write lock. usesPartAcquisition took the
write lock on every call for results that host parts. In an edit build,
where nearly every request is a cache hit, those writers queued the
request goroutines' read locks behind them.

- A hit whose identity teaching would change nothing (the request's
  digests already in the result's single output class, the result
  already associated with the request's term, every term with that
  digest already pointing at that class, the result already posted under
  its recipe and extra digests, the expiry unchanged) is now answered and
  claimed under the read lock. Anything else still repeats the lookup
  under the write lock.
- A persistable hit's persisted-edge upsert takes the write lock only
  when the edge is missing or its expiry would change.
- usesPartAcquisition answers under the read lock when the result has no
  part state at all or its gate is already managed; only starting to
  manage a gate takes the write lock.

The read-only checks find roots without path compression and answer
"changes something" whenever a step would write.

Signed-off-by: Erik Sipsma <[email protected]>
PersistHit held egraphMu for writing in every case. The code now checks
under the read lock whether the result is collected or already persisted,
and then changes nothing, so only an upsert that changes the edge waits
for open read sections. In the "atomic" sections mode WriteLockAvailable
is always true, so those configurations are unchanged.

Signed-off-by: Erik Sipsma <[email protected]>
usesPartAcquisition's read-locked fast path covered a row with no gate and
no part state, and a managed gate. A row whose gate exists but is not
managed, with no part state or with a group still evaluating, still took
the write lock only to answer false. The fast path now follows the write
path's order exactly, so only starting to manage a gate takes the write
lock.

Signed-off-by: Erik Sipsma <[email protected]>
It makes one read-only check for each write the hit's write path could
make, which takes it past gocyclo's limit of 30, as other long sections
under the graph lock are.

Signed-off-by: Erik Sipsma <[email protected]>
TestSnapshotOwnerWaitsForWholeContainer built the cycle between a
diagnostic holding the graph read lock, a snapshot owner reader and a
Container's whole body by waiting for the body's dependency evaluation
to queue for the graph write lock in usesPartAcquisition. That call now
answers under the read lock, which another reader doesn't block, so the
wait never came. A queued writer still holds off new readers, as one
always can on a busy engine, so the test now queues a real writer
behind the diagnostic and waits for the body's evaluation to queue for
the read lock behind it.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma

sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #15007, which carries these commits unchanged.

@sipsma sipsma closed this Oct 30, 2026
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.

1 participant