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 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-27 01:11:03 +05:30
1 parent 769fe52887
commit 96dfd9bb89
2 files changed
+121 -7

No files matched your search

+75 -1
View File
@@ -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<Mutex<Spans>>,
/// 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<bool> = 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);