Spread work wakes only as many kept threads as there are jobs for them

shadow::wide woke every kept thread (4 on 16 hardware threads) for each
call, however few its jobs: a submission's refresh_front with 2 copies to
refresh woke 4 threads, 3 of which found nothing and slept again. Waking
is a system call per thread on the calling thread
(NtAlertThreadByThreadIdEx, 2.4% of the submitting thread's samples in
profile-y-prof60, 1.3% under shadow::wide), and the threads woken for
nothing take processor time from the others on a machine where ready
threads already wait to run ("woken, not running yet" 1.4-6.1 ms of the
frame's chain in timeline tl.170).

Now one thread is woken for each job beyond the one the caller takes,
all of them only when there are jobs for all. A thread busy meanwhile
looks at the queue before it sleeps, and the caller takes every job
nobody took, as before, so no job waits for a thread.

Behaviour-neutral: the same jobs run, on the same threads. On by default,
no switch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-27 01:11:02 +05:30
1 parent 19720fee01
commit 8c1fa5d578
1 file changed
+11 -1
+11 -1
View File
@@ -1597,6 +1597,12 @@ fn wide(jobs: Vec<Job>) {
}
let workers = workers();
let _wide = crate::timeline::span("work spread over threads");
// Helpers woken: one for each job beyond the one this thread takes
// first, at most all of them. All were woken for every call (4 for 2
// jobs), and those that found nothing went back to sleep; a helper busy
// meanwhile looks at the queue before it sleeps again, and this thread
// takes what nobody took.
let wake = (jobs.len() - 1).min(wide_threads());
let left = Arc::new((Mutex::new(jobs.len()), std::sync::Condvar::new()));
{
let mut queue = workers.jobs.lock().unwrap_or_else(|e| e.into_inner());
@@ -1613,7 +1619,11 @@ fn wide(jobs: Vec<Job>) {
}));
}
}
workers.more.notify_all();
if wake >= wide_threads() {
workers.more.notify_all();
} else {
(0..wake).for_each(|_| workers.more.notify_one());
}
loop {
let job = workers.jobs.lock().unwrap_or_else(|e| e.into_inner()).pop_front();
match job {