From 96dfd9bb89c00ff80420bf26f9587968625986eb Mon Sep 17 00:00:00 2001 From: readmet3xt-lab <238408149+readmet3xt-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:21:00 +0530 Subject: [PATCH] VICTORY_SPANS_QUICK=1, VICTORY_SYNC_FINE=1: the heaps' records' round spared the spans' lock, in finer jobs Ported from Belobug/VIctory-Belobug-AMD a284ca7 (its per-job timing and timeline phases are left out; both switches are on by default there and off here). VICTORY_SPANS_QUICK=1: a slot written with no placed texture view leaves the heap's spans of placed views (and their lock) alone when the slot is not in them (HostHeap::spanned, a bit per slot that may be there). Every record the round of the heaps' records mirrors went by that one lock, from its six threads, with the title's threads and the texture passes: there, 4 of the 16 ms a frame the submitting thread waited for the round were its threads waiting on that lock (run city4). What is done is the same: forgetting a slot that is not there does nothing. VICTORY_SYNC_FINE=1: the round gives its threads 2 pages and 64 records a job instead of 8 and 256: a round for a submission finds a dozen pages, which were two jobs on two of six threads while the submission waited. Neither changes what is mirrored. Off by default. The frame line "writes of view slots per frame (VICTORY_SPANS_QUICK=1)" counts writes that left the spans alone and those under their lock, and the job size; the heaps' records line's "ms waited for" shows the gain. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/descriptor.rs | 52 ++++++++++++++-- crates/victory-gpu/src/descriptor/host.rs | 76 ++++++++++++++++++++++- 2 files changed, 121 insertions(+), 7 deletions(-) diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 977903a..f1f265e 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -330,6 +330,7 @@ pub(crate) fn sync_heaps() { let mut short = 0u64; let mut wait: Vec = Vec::new(); let frame = crate::frame::current(); + let (pages_a_job, records_a_job) = sync_jobs(); let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner()); for &(start, end, at) in heaps.iter() { // SAFETY: registered heaps are live (removed when dropped). @@ -347,8 +348,8 @@ pub(crate) fn sync_heaps() { // since: a shader may pick them by index. let before = host.copied_before(frame); let of_tables = std::sync::atomic::AtomicU64::new(0); - crate::land::each_view(true, before.len().div_ceil(SYNC_AT_A_TIME), &|n| { - for &index in before.chunks(SYNC_AT_A_TIME).nth(n).unwrap_or_default() { + crate::land::each_view(true, before.len().div_ceil(records_a_job), &|n| { + for &index in before.chunks(records_a_job).nth(n).unwrap_or_default() { if host.copied_tabled(index, frame) { of_tables.fetch_add(1, Relaxed); } else { @@ -380,13 +381,13 @@ pub(crate) fn sync_heaps() { let copied = host.copied_begin(step as usize); let samplers = heap.samplers(); let in_place = sync_in_place() && step == 32 && crate::safecopy::on(); - let found: Vec> = parts.chunks(SYNC_PAGES).map(|_| Default::default()).collect(); + let found: Vec> = parts.chunks(pages_a_job).map(|_| Default::default()).collect(); crate::land::each_view(true, found.len(), &|n| { let mut out = SyncPart::default(); let mut page_bytes: Vec = Vec::new(); let mut changed: Vec = Vec::new(); let mut bits: Vec = Vec::new(); - for &(from, to) in parts.chunks(SYNC_PAGES).nth(n).unwrap_or_default() { + for &(from, to) in parts.chunks(pages_a_job).nth(n).unwrap_or_default() { let slots = changed_slots(from, to, start as usize, step, mirrored_count); // A record that straddles the range's ends is left // to the range that holds its first byte. @@ -500,8 +501,8 @@ pub(crate) fn sync_heaps() { } spent[1] += since(began); let began = std::time::Instant::now(); - crate::land::each_view(true, make.len().div_ceil(SYNC_AT_A_TIME), &|n| { - for &index in make.chunks(SYNC_AT_A_TIME).nth(n).unwrap_or_default() { + crate::land::each_view(true, make.len().div_ceil(records_a_job), &|n| { + for &index in make.chunks(records_a_job).nth(n).unwrap_or_default() { host.mirror(index, start + u64::from(index) * step, false); } }); @@ -582,6 +583,35 @@ const SYNC_PAGES: usize = 8; #[cfg(windows)] const SYNC_AT_A_TIME: usize = 256; +/// With `VICTORY_SYNC_FINE=1`: pages and records [`sync_heaps`] gives a thread +/// at a time. +#[cfg(windows)] +const SYNC_PAGES_FINE: usize = 2; +#[cfg(windows)] +const SYNC_AT_A_TIME_FINE: usize = 64; + +/// Pages and records [`sync_heaps`] gives a thread at a time +/// (`VICTORY_SYNC_FINE=1`: [`SYNC_PAGES_FINE`] and [`SYNC_AT_A_TIME_FINE`]; +/// off by default: [`SYNC_PAGES`] and [`SYNC_AT_A_TIME`], as before). +/// +/// Ported from Belobug/VIctory-Belobug-AMD a284ca7: a round for a submission +/// finds a dozen pages or so; by eight a job that was two jobs, on two of the +/// six threads, while the submission waited for the round (2 to 3.4 ms at a +/// time, their run city4). +#[cfg(windows)] +fn sync_jobs() -> (usize, usize) { + #[cfg(feature = "diagnostics")] + if let Some(fine) = crate::ab::pick("VICTORY_SYNC_FINE") { + return if fine { (SYNC_PAGES_FINE, SYNC_AT_A_TIME_FINE) } else { (SYNC_PAGES, SYNC_AT_A_TIME) }; + } + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + if *ON.get_or_init(|| std::env::var_os("VICTORY_SYNC_FINE").is_some_and(|v| v != "0")) { + (SYNC_PAGES_FINE, SYNC_AT_A_TIME_FINE) + } else { + (SYNC_PAGES, SYNC_AT_A_TIME) + } +} + /// Since the last frame line, of [`sync_heaps`]: changed pages of placed /// shader-visible heaps looked at, slots on them, slots whose record was /// not the one last seen and was mirrored, the microseconds it took; then @@ -614,6 +644,16 @@ pub(crate) fn sync_line(frames: f64) { let split = [0, 1, 2, 3, 4].map(|i| SYNC_SPLIT[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64); let kinds = [0, 1, 2, 3, 4].map(|i| SYNCED_KINDS[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames); let short = SYNC_SHORT.swap(0, std::sync::atomic::Ordering::Relaxed); + let quick = [0, 1].map(|i| host::SPANS_QUICK[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames); + if quick[0] > 0.0 || sync_jobs().0 == SYNC_PAGES_FINE { + tracing::info!( + "writes of view slots per frame (VICTORY_SPANS_QUICK=1): {:.0} leaving the placed views' spans alone, {:.0} under \ + their lock; the heaps' records in jobs of {} pages (VICTORY_SYNC_FINE)", + quick[0], + quick[1], + sync_jobs().0 + ); + } if pages == 0 { return; } diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 347a86e..56bf532 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -212,6 +212,35 @@ pub(crate) struct HostHeap { /// other memory there makes stale, and which to look at again when the /// runtime maps, unmaps or writes memory ([`remapped`]). placed: std::sync::Arc>, + /// The slots that may be in `placed` (a slot noted there is put here + /// under its lock, and taken out when it is forgotten there; a slot + /// another's view made stale stays, which says only "may"): a write of + /// any other view needs nothing of `placed` ([`spans_quick`]). + spanned: Slots, +} + +/// Since the last frame line: writes that left `placed` alone +/// ([`spans_quick`]), writes that took its lock. +pub(crate) static SPANS_QUICK: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2]; + +/// Whether a slot's write of no placed texture view leaves `placed` alone +/// when the slot is not in it ([`HostHeap::spanned`]; `VICTORY_SPANS_QUICK=1`; +/// off by default: its lock is taken for every write, as before). +/// +/// Ported from Belobug/VIctory-Belobug-AMD a284ca7 (on by default there). +/// Every record the pass over records copied as bytes mirrors went by that +/// one lock of the heap, from six threads, with the title's own threads and +/// the texture passes: 4 of the 16 ms a frame the submitting thread waited +/// for that pass were its threads waiting there (their run city4, timeline +/// 113). What is done is the same: forgetting a slot that is not there does +/// nothing. +fn spans_quick() -> bool { + #[cfg(feature = "diagnostics")] + if let Some(on) = crate::ab::pick("VICTORY_SPANS_QUICK") { + return on; + } + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("VICTORY_SPANS_QUICK").is_some_and(|v| v != "0")) } /// The spans of every view heap made ([`HostHeap::placed`]), for the @@ -1720,6 +1749,7 @@ impl HostHeap { } spans }, + spanned: Slots::new((count + extra) as usize), }; // A slot never written is undefined: shaders reading one (a slot // the title wrote in a way we do not know, or a table entry no @@ -2821,8 +2851,18 @@ impl HostHeap { } _ => None, }; - let stale = { + // (Under the slot's lock, as every write of it: whether it is in + // `spanned` changes only here.) + let in_spans = |index: u32| { + let bit = 1u64 << (index % 64); + self.spanned.0.get(index as usize / 64).is_none_or(|w| w.load(std::sync::atomic::Ordering::Acquire) & bit != 0) + }; + let stale = if placed_view.is_none() && !in_spans(index) && spans_quick() { + SPANS_QUICK[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed); + Vec::new() + } else { use std::sync::atomic::Ordering::Relaxed; + SPANS_QUICK[1].fetch_add(1, Relaxed); let mut placed = self.placed.lock().unwrap_or_else(|e| e.into_inner()); let span = placed_view.and_then(|(resource, start, first, held)| { Some((start, start.saturating_add(placed.bytes(resource)?), first, held)) @@ -2832,6 +2872,7 @@ impl HostHeap { SPANNED[0].fetch_min(start, Relaxed); SPANNED[1].fetch_max(end, Relaxed); placed.rule = stale_rule(); + self.spanned.put(index); let stale = placed.note(index, start, end, !seen); placed.shows(index, first, held); // (Under the lock of `placed`: a later write of one of @@ -2840,6 +2881,7 @@ impl HostHeap { } None => { placed.forget(index); + self.spanned.remove(index); Vec::new() } } @@ -5790,6 +5832,38 @@ mod tests { assert_eq!(slots(u64::MAX), [2]); } + /// A write of no placed texture view forgets the slot in the spans + /// whether it goes by their lock or not (`spans_quick`): slots that are + /// in them stay marked, also one another's view made stale. + #[test] + fn writes_of_other_views_leave_the_spans_as_under_their_lock() { + let Some(heap) = HostHeap::new(false, 8) else { + return; // no host GPU + }; + let marked = |slot: u32| heap.spanned.all(|_| true).contains(&slot); + let at = 0x7fed_0000_0000u64; + let view = |address: u64, width: u32| { + let mut desc = [0u32; 16]; + (desc[10], desc[11]) = (address as u32, (address >> 32) as u32); + View::Placed(false, [3, 0, 0, 0, width, 0, 64, 0x1_0001, 98, 1, 0, 0x105, 0, 0], desc) + }; + assert!(heap.note_buffer(1, Some(&view(at, 64)), false).is_empty()); + assert!(marked(1) && !marked(2)); + // Slot 1 made stale by slot 2: out of `by_slot`, still in `first`. + assert_eq!(heap.note_buffer(2, Some(&view(at, 128)), false), [(1, 0)]); + assert!(marked(1) && marked(2)); + assert!(heap.placed.lock().unwrap().first.contains_key(&1)); + // Written with no view: forgotten, under the lock, and unmarked. + assert!(heap.note_buffer(1, None, false).is_empty()); + assert!(!marked(1) && !heap.placed.lock().unwrap().first.contains_key(&1)); + // A slot never in the spans: nothing there to change. + assert!(heap.note_buffer(3, None, false).is_empty()); + assert!(heap.note_buffer(2, None, false).is_empty()); + let spans = heap.placed.lock().unwrap(); + assert!(spans.by_slot.is_empty() && spans.by_start.is_empty() && spans.first.is_empty()); + assert!(heap.spanned.all(|_| true).is_empty()); + } + #[test] fn a_set_of_slots_holds_what_was_put_in() { let set = Slots::new(200);