Skip to content

perf(cache): check cache entries off the scheduler thread - #982

Merged
wan9chi merged 2 commits into
trace-remote-cache-timingfrom
check-cache-entries-off-thread
Nov 9, 2026
Merged

wan9chi merged 2 commits into
trace-remote-cache-timingfrom
check-cache-entries-off-thread

Conversation

@wan9chi

@wan9chi wan9chi commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Motivation

The scheduler polls every task's future on one thread. The synchronous work in a task's cache lookup ran on that thread, so it held up every other task:

  • hashing the task's explicit inputs;
  • decoding its local entry, or the entries fetched from the remote cache;
  • validating them, which stats and hashes every inferred input.

Tasks that became ready together looked up one after another, and nothing else made progress in the meantime.

In a CI replay of a monorepo, 26 cached Vite builds become ready at once, all of them remote hits. Their starts were spread over about 1 s on Linux and up to 2.7 s on Windows. Per run, entry validation took up to 1.9 s (Linux) and 3.6 s (Windows) of scheduler-thread time when most tasks hit. When most missed, it took 3.7–4.8 s, because each miss checks up to 8 remote entries.

Changes

  • Where the work runs: a lookup hashes inputs and decodes and validates entries on rayon's thread pool, through a new run_off_thread. Hits, misses and miss reasons are unchanged. Cancelling the run still stops the lookup: work that hasn't started doesn't run, and work that's already running finishes in the background with its result dropped. try_hit returns None when a check is cancelled.
  • Why rayon's pool rather than tokio's blocking threads: validation already hashes inputs in parallel on it, and its size keeps the checks from crowding out downloads and archive extraction. In A/B runs on a 4-core Windows runner (same tree, alternating builds), running all 26 checks at once on blocking threads made those builds finish 0.7 s later than before. On rayon's pool they finished 1.35 s sooner, and on Linux 0.3 s sooner. On both, all 26 now start together. These runs also had remote fetches starting when the run starts, which isn't part of this stack.
  • Start order: tasks that become ready together now start, and print their commands, in the order their checks finish. The e2e harness runs every step with RAYON_NUM_THREADS=1, so the checks finish in the order they're submitted and snapshots keep scheduling order. With random 0–30 ms delays injected into the checks, the full suite passed 11 of 11 runs this way, while 8 threads failed 8 of 10.
  • Tests:
    • A new fixture, cache_check_concurrency, covers a local and a remote entry. It holds the reads of a task's inferred input while the entry is checked, and releases them from a task that the scheduler can only start once it sees another task finish. A check on the scheduler thread would never finish, and the step would time out.
    • vtt hold-reads holds the reads with a FIFO on Unix and with an exclusive oplock on Windows.
    • vtt barrier exposes its wait for reuse.

🤖 Generated with Claude Code


Note

Medium Risk
Touches core cache lookup, cancellation, and scheduling paths; behavior should match prior hit/miss semantics but concurrent validation and ordering when multiple tasks start together may surface edge cases.

Overview
Cache lookups no longer block the DAG scheduler. When multiple tasks become ready together, input hashing, local entry decode/validation, and remote entry resolution now run on rayon's pool via new run_off_thread, so other tasks can keep making progress instead of queuing one lookup after another on the single scheduler thread.

try_hit and lookup_cache were updated to use shared Validation state and an optional result layer: cancelled checks return Ok(None) / Report::Cancelled instead of blocking or mis-reporting. PathGlobSet::into_owned supports sending negative globs into that off-thread work.

E2E coverage adds cache_check_concurrency (blocked file reads during validation), plus vtt hold-reads (FIFO on Unix, oplock on Windows) and a reusable barrier::wait. The harness sets RAYON_NUM_THREADS=1 so snapshot task order stays stable.

Reviewed by Cursor Bugbot for commit ff367a1. Bugbot is set up for automated code reviews on this repo. Configure here.

The scheduler polls every task's future on one thread, so the synchronous
work in a task's cache lookup held up every other task: hashing its explicit
inputs, decoding its local entry or the entries fetched from the remote
cache, and validating them, which stats and hashes every inferred input.
Tasks that became ready together had their lookups run one after another. In
a CI run where 26 cached Vite builds became ready at once, all remote hits,
they started over about 1 second on Linux and up to 2.7 seconds on Windows
instead of together.

That work now runs on rayon's thread pool, in the task's span, so the lookups
of ready tasks proceed concurrently, with the same results and miss reasons.
The pool is rayon's rather than tokio's blocking threads: validation already
hashes inputs in parallel on it, and its size keeps the checks from crowding
out downloads and extraction. On a 4-core Windows runner, running all 26
checks at once on blocking threads made those builds finish later than
before. A lookup still stops when the run is cancelled: work that hasn't
started doesn't run, and work that's already running finishes in the
background with its result dropped.

Tasks that become ready together now start in the order their checks
finish. The e2e harness runs vp with RAYON_NUM_THREADS=1, so the checks
finish in the order they're submitted, and those tasks start in scheduling
order in snapshots.

The new e2e cases hold the reads of a task's inferred input while its local
or remote entry is checked, and release them from a task that the scheduler
can only start once it sees another task finish, so a check on the
scheduler's thread would never finish. vtt hold-reads holds the reads with a
FIFO on Unix and with an exclusive oplock on Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wan9chi
wan9chi added this pull request to stack #983 November 9, 2026 07:52
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

fspy benchmark

linux

dynamic/launch             change  -0.04%  [ -3.11% ..  +3.78%]  overhead   +57.73%
dynamic/access             change  +0.43%  [ -3.30% ..  +4.67%]  overhead   +20.49%
dynamic/access-relative    change  +0.00%  [ -2.89% ..  +2.75%]  overhead   +69.48%
dynamic/access-contended   change  -0.97%  [ -7.67% ..  +6.97%]  overhead   +48.37%
static/launch              change  -0.03%  [ -2.85% ..  +3.11%]  overhead  +197.01%
static/access              change  +0.19%  [ -0.69% ..  +0.72%]  overhead +1456.33%
static/access-relative     change  +0.68%  [ -0.12% ..  +1.36%]  overhead +1998.10%
static/access-contended    change  -0.13%  [ -1.22% ..  +0.39%]  overhead +1524.89%

macos

dynamic/launch             change  -0.45%  [ -4.26% ..  +3.67%]  overhead  +243.40%
dynamic/access             change  -0.33%  [ -5.28% ..  +4.17%]  overhead    +0.33%
dynamic/access-relative    change  +0.41%  [ -1.94% ..  +2.80%]  overhead  +247.77%
dynamic/access-contended   change  -0.53%  [ -9.33% ..  +9.39%]  overhead    +3.26%

windows

dynamic/launch             change  +0.00%  [ -1.91% ..  +1.51%]  overhead   +26.44%
dynamic/access             change  +0.19%  [ -0.76% ..  +0.77%]  overhead    +1.17%
dynamic/access-relative    change  +0.00%  [ -0.57% ..  +0.76%]  overhead    +1.33%
dynamic/access-contended   change  +0.18%  [ -0.18% ..  +0.73%]  overhead    +1.66%

…-thread

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wan9chi
wan9chi merged commit 0f75afe into main Nov 9, 2026
22 checks passed
@wan9chi
wan9chi deleted the check-cache-entries-off-thread branch November 9, 2026 09:11
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