From 5fff0f1fb75d87597e646cec8e8371ed84198780 Mon Sep 17 00:00:00 2001 From: theme613 <66501763+theme613@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:13:48 +0800 Subject: [PATCH] gpu: restore 1-by-1 wide_each stealing for balanced dirty-buffer refresh load --- crates/victory-gpu/src/host.rs | 47 ++++++++++---------------------- crates/victory-gpu/src/shadow.rs | 34 ----------------------- 2 files changed, 15 insertions(+), 66 deletions(-) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 17fd313..c115790 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -9052,28 +9052,18 @@ impl Batch { crate::watch::ask_ahead(&ranges); } FLUSHES[3].fetch_add(spans.len() as u64, Ordering::Relaxed); - const SPAN_CHUNK: usize = 32; - let span_chunks = spans.len().div_ceil(SPAN_CHUNK); - let due_len = due.len(); - crate::shadow::wide_each(due_len + span_chunks, move |i| { - // The textures first (1 by 1): they are the longer pieces. - let Some(chunk_idx) = i.checked_sub(due_len) else { + crate::shadow::wide_each(spans.len() + due.len(), move |i| { + // The textures first: they are the longer pieces. + let Some(i) = i.checked_sub(due.len()) else { due[i].refresh(); return; }; - let start = chunk_idx * SPAN_CHUNK; - let end = (start + SPAN_CHUNK).min(spans.len()); - let mut skipped = 0u64; - for (shadow, from, to) in &spans[start..end] { - if many && shadow.nothing_new(*from, *to - *from) { - skipped += 1; - continue; - } - Self::update(shadow, *from, *to); - } - if skipped > 0 { - FLUSHES[2].fetch_add(skipped, Ordering::Relaxed); + let (shadow, from, to) = &spans[i]; + if many && shadow.nothing_new(*from, *to - *from) { + FLUSHES[2].fetch_add(1, Ordering::Relaxed); + return; } + Self::update(shadow, *from, *to); }); if ahead { crate::watch::ask_ahead_done(); @@ -9225,22 +9215,15 @@ impl Batch { let before = PREPARING[0].load(Ordering::Relaxed); FLUSHES[3].fetch_add(self.refreshes.len() as u64, Ordering::Relaxed); if light { - // Chunked work-stealing avoids per-item atomic contention. + // A number to a free thread, not a job to each copy. let refreshes = std::mem::take(&mut self.refreshes); - let many = refreshes.len() > FEW; - let chunk = if many { 32 } else { 1 }; - crate::shadow::wide_each_chunked(refreshes.len(), chunk, move |range| { - let mut skipped = 0u64; - for (shadow, from, to) in &refreshes[range] { - if many && shadow.nothing_new(*from, *to - *from) { - skipped += 1; - continue; - } - Self::update(shadow, *from, *to); - } - if skipped > 0 { - FLUSHES[2].fetch_add(skipped, Ordering::Relaxed); + crate::shadow::wide_each(refreshes.len(), move |i| { + let (shadow, from, to) = &refreshes[i]; + if refreshes.len() > FEW && shadow.nothing_new(*from, *to - *from) { + FLUSHES[2].fetch_add(1, Ordering::Relaxed); + return; } + Self::update(shadow, *from, *to); }); } else { let jobs: Vec> = self diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 555d508..5ccb5ae 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -1843,40 +1843,6 @@ pub(crate) fn wide_each(n: usize, work: impl Fn(usize) + Send + Sync + 'static) wide(jobs); } -/// As [`wide_each`], but hands out contiguous slices of up to `chunk` indices -/// per atomic claim to eliminate cross-core cache-line bouncing on thousands -/// of lightweight items (e.g. unchanged copy checks). -pub(crate) fn wide_each_chunked( - n: usize, - chunk: usize, - work: impl Fn(std::ops::Range) + Send + Sync + 'static, -) { - let chunk = chunk.max(1); - let chunks = n.div_ceil(chunk); - if chunks == 0 { - return; - } - let next = Arc::new(std::sync::atomic::AtomicUsize::new(0)); - let work = Arc::new(work); - let threads = (wide_threads() + 1).min(chunks); - let jobs = (0..threads) - .map(|_| { - let (next, work) = (next.clone(), work.clone()); - Box::new(move || { - loop { - let c = next.fetch_add(1, Ordering::Relaxed); - let start = c.saturating_mul(chunk); - if start >= n { - break; - } - work(start..(start + chunk).min(n)); - } - }) as Job - }) - .collect(); - wide(jobs); -} - /// About this many bytes to a piece of work for [`wide`]. const WIDE_JOB: usize = 1 << 20;