Skip to content

state_exec locks: L1's speed (deadlock search, uncontended path, restated gate) - #1175

Merged
transfix merged 7 commits into
masterfrom
perf/locks-speed
Nov 16, 2026
Merged

transfix merged 7 commits into
masterfrom
perf/locks-speed

Conversation

@transfix

@transfix transfix commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

This PR fixes the two with-lock bench findings open since L1 (#862). It needs no protocol
change. The plan and the full measurements are in the workstreams tracker:
state-exec-perf/locks-impl/L1-speed.md.

Finding 2: the deadlock search at 64 waiters (fixed)

block() ran protocol 4.6's P in reach(targets) as a depth-first search from every target. It
recomputed each process's successors (every overlapping waiter) as it went. With p waiters on
one key, every re-queue after a handoff searched the whole queue from every earlier waiter:
O(p^2) work, plus an allocation per node.

Every edge into P comes from a queued ticket that conflicts with a hold of P. P has no ticket in
block(), and its request would queue last (3.2).

  • With no hold, P can't be on a cycle, so the search is skipped. This is the lock.handoff
    case.
  • Otherwise the processes with a path to P are found first, backwards through
    predecessors(). The search runs only if a target is among them, and only over them. A
    process with no path to P reaches only processes with none, so the cycle found (or not) is
    the same one, and so is the DeadlockError's cycle.

Protocol 4.6 gains this as an implementation note. LD-11 checks a cycle found past earlier
targets that can't lead back, and 20 waiters that hold nothing queueing with no false deadlock.

lock.handoff (workers=host) master 2b2b976 this PR
p = 64, async, us/handoff 92.6 13.6
p = 64, sync 93.3 12.0
p = 8, async 9.1 8.3
p = 8, sync 8.2 6.7
steps per handoff (p = 64 / p = 8) 37.002 / 36.991 37.002 / 36.991

The times are medians of 6 runs, base and candidate interleaved, on a loaded box.

Finding 1: the uncontended lock (reduced; gate restated)

Cycle counters inside the lock path put an uncontended lock's cost at about 40% evaluator glue,
25% lock home and 30% manager. There is no single hot spot. This PR cuts what needs no
protocol change:

  • lock_home: scope_id remembers its last answer. A 16-slot memo, keyed by the address of
    the key's text, sits in front of the key cache. A memo entry counts only when it is live with
    the same scope and text.
  • lock_table: no seed copy while nothing lends, request text only for the self-check, and
    one record lookup per try_acquire. new_hold takes the record and returns the hold.
  • stackless_evaluator: the fast path derives the lock_ops once and copies no value_t.
    That is two derivations per lock, down from five.

Probe of the home and table (a temporary test, not committed): best of 25 loops of 100k,
pinned, three rounds interleaved against master, medians. The box was loaded.

ns master this PR
table try_acquire + release, single (depth 1 / 4) 180 / 182 154 / 164
the same, shared 232 / 235 204 / 209
scope + key-cache lookup 34 / 52 13 / 15
build_request 64 / 85 52 / 51
whole home path, single 297 / 308 249 / 257
whole home path, shared 363 / 356 332 / 292

The gate, restated (the owner's decision, 2026-11-12). Design 8.8's 25-50 ns estimate left
out the record each section makes and drops (4.1's ensure_rec and maybe_drop_rec). It also
left out two index updates, two pools, the operation framing and the evaluator's frame layout.
An uncontended lock touches about ten structures twice. The gate is now restated from that
measured architecture. A leaner fast path that changes protocol 4.1 was the other option, and
was not taken.

The restated gate: at most 450 ns over begin single and 550 ns shared (sync, flat
key, Release, quiet window, noise ≤ 10%). It was 60 and 120 ns. 0 steps and 0 allocations
stay the hard gates, run in CTest. The numbers come from three sources:

  • the probe's best runs: the manager and home together are 230-250 ns single and 270-290 ns
    shared;
  • the evaluator's share on top of that;
  • the lowest of the bench's own runs on this loaded box (sync, best of 100 short runs per side):
    230-305 ns single and 310-360 ns shared.

The first run in a quiet window checks them. Design 11.1-11.3 and STATE_EXEC_BENCH.md's gate
commands follow (faa024c). The sharding decision is unchanged: lock.cross_thread is measured
again in a quiet window first.

Counts and tests (Windows, MSVC 19.44 Release, local cvcpkg prefix)

  • state_exec_bench_gates, run by hand: every gate passes. That is 0 steps and 0 allocations
    per lock at every form=single point (both schedulers, both threading modes), and the pinned
    sched.single.* steps_total values.
  • state_exec_locks_test (-*Stress*, LocksThreads, LocksReview and LD-11 included): 211/211.
    LocksStress locally: 6/6 pass (LT-4 with 1000 fuzz seeds, LT-10 with 10^5 operations, and the four LK-18 cases at 10^5). locks-deep.yml is dispatched on this branch (run 43186202693). state_exec_locks_model_test (-*Stress*): 178/178.
    state_exec_priority_test (the PI cases): 40/40.
  • Scheduler 199, async 107, stackless evaluator 83, integration 104, coordinator 74.
    bindings/pycvc/test_pycvc_state_exec_locks.py: OK.
  • clang-format 18 on the changed lines: clean.

Left for a quiet window

The timing A/Bs: lock.uncontended, lock.cross_thread and the locks=none regression
families against master 2b2b976. On this box the uncontended medians moved ±30% between
interleaved rounds, so wall time gives no verdict here.

…requester

block() ran 4.6's "P in reach(targets)" as a depth-first search from every target, recomputing
each process's successors (every overlapping waiter) as it went. At p waiters on one key, each
re-queue after a handoff searched the whole queue from every earlier waiter: O(p^2) work and an
allocation per node, about 90 us per handoff at p = 64 on the bench box.

Every edge into P comes from a queued ticket that conflicts with a hold of P: P has no ticket
in block(), and its request would queue last (protocol 3.2). So a requester with no hold
cannot be on a cycle, and the search is skipped. Otherwise the processes with a path to P are
found first, backwards through predecessors(), and the search runs only when a target is among
them, confined to them. A process with no path to P reaches only processes with none, so the
confined search finds a cycle exactly when the full one does, and the same first path: the
DeadlockError's cycle is unchanged.

lock.handoff at p = 64: 92-93 us per handoff before, 12-14 us after (medians of 6 runs, base
and candidate interleaved, loaded box); steps per handoff unchanged (37.0). Protocol 4.6 gains
the implementation note; LD-11 checks a cycle found past targets that cannot lead back, and
twenty waiters that hold nothing queueing with no false deadlock.
… work

Cycle counters over lock.uncontended's loop put an uncontended lock's cost roughly 40% in the
evaluator's glue, 25% in the lock home and 30% in the manager, with no single hot spot. This
cuts what needs no protocol change:

- lock_home: scope_id remembers its last answer (a program locks under one chroot), and a
  16-slot memo keyed by the address of the key's text sits in front of the key cache's
  unordered_map. A memo slot is a hint: it is taken only when that entry is live and holds the
  same scope and text, and the live entries are exactly those in the index.
- lock_table: begin_op no longer copies the seeds while no home lends (only relevel_all's
  lending paths read them); the request's key text is built only for the self-check's
  cross-check; try_acquire looks the record up once, and new_hold takes the record and returns
  the hold, so neither is looked up again.
- stackless_evaluator: step_init hands the lock_ops it found to begin_lock_form, which hands it
  to lock_acquire, so the fast path derives it once with no value_t copy (two derivations per
  lock with the release, down from five). Restored frames still read it from the frame's slot.

Probe of the home and table (best of 25 loops of 100k, three interleaved rounds against master
2b2b976, pinned, loaded box): the scope and key-cache lookups 29-45 ns -> 11-13 ns, building
the request 56-72 -> 43-45 ns, the whole home path 242-307 -> 232-262 ns. Steps and
allocations per lock are unchanged (0 and 0); timing gates wait for a quiet window.
…s cost

Design 8.8's 25-50 ns (65-90 shared) estimate for an uncontended lock leaves out the record each
section makes and drops, two index updates, two pools, the operation framing and the
evaluator's frame layout; the manager alone takes 140-180 ns single and 190-235 ns shared on the
development laptop. 11.3 records the breakdown (evaluator glue ~40%, home ~25%, manager ~30%),
the 64-waiter handoff cost the deadlock search was (92-93 us, now 12-14 us), and what reaching
11.2's 60/120 ns would take (a leaner fast path that changes protocol 4.1, or a restated gate:
the owner's decision). The sharding decision is unchanged: lock.cross_thread is measured again
in a quiet window first.
…ecture

The owner's decision (2026-11-12), between a leaner fast path that changes protocol 4.1 and a
gate restated from what the design costs: the gate is restated. An uncontended with-lock may
cost at most 450 ns over begin in single mode and 550 ns in shared mode (sync, flat key,
Release, quiet window, noise at most 10%), against the 60 and 120 ns first set from design
8.8's estimate. 0 steps and 0 allocations per lock stay the hard gates, as CTest runs them.

The numbers come from three sources:
- the probe's best runs: the manager and home together are 230-250 ns single and 270-290 ns
  shared;
- the evaluator's share on top of that;
- the lowest of the bench's own runs on a loaded box (sync, best of 100 short runs per side):
  230-305 ns single and 310-360 ns shared.

The first run in a quiet window checks them. Design 11.1-11.3 and STATE_EXEC_BENCH.md's gate
commands follow; 11.3's L3 record keeps its numbers, marked as the gates of that time.
@transfix transfix changed the title state_exec locks: L1's speed (deadlock search, uncontended path) state_exec locks: L1's speed (deadlock search, uncontended path, restated gate) Nov 12, 2026
@transfix
transfix marked this pull request as ready for review November 14, 2026 21:17
A PR's Debug ctest gives every test without a TIMEOUT property 300 s (ci.yml, --timeout 300,
three tries). Three of ocean_ewave_test's long runs (NoFluxWallsAreLossless, AThinWallLeaksLittle,
AMovingSourceLeavesAKelvinWake) take 105-195 s each in Debug on an idle runner, so a busy one
misses 300 s on every try: on 2026-11-14 that made package-linux / libcvc-debug red on two
unrelated PRs (feat/ariadne-live-textures, p1/skeleton-surfaces). The suite's own 1200 s keeps a
real hang caught and stops the runner's load from failing PRs.

(cherry picked from commit 83e7536)
@transfix
transfix merged commit 62369e8 into master Nov 16, 2026
28 checks passed
@transfix
transfix deleted the perf/locks-speed branch November 16, 2026 08:00
transfix added a commit that referenced this pull request Nov 16, 2026
…#1307)

One conflict, in ariadne_runtime_test.cpp's ActionCapsAreTreeSettingsAndChargeAsyncCalls: this
branch cut its loop to 20 rounds so it fit the 0.1 s time cap on Linux debug CI, and master
(#1282) rewrote the test, raising the time cap to 30 s wherever the loop must finish.
Master's fix covers the same flake, so the test is master's as it stands, 200 rounds again.

tools/singleton_scan is master's (#1276); the auto-merged net, CI, test and doc files keep both
sides.
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