From 8c1fa5d578b859f8399da3963a2de64b980f8f98 Mon Sep 17 00:00:00 2001 From: readmet3xt-lab <238408149+readmet3xt-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:12:20 +0530 Subject: [PATCH] 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 --- crates/victory-gpu/src/shadow.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 5ba6ddd..cde6ec0 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -1597,6 +1597,12 @@ fn wide(jobs: Vec) { } 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) { })); } } - 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 {