Lighter refreshes of host copies; landing past the guards; the heap pass spread

VICTORY_LIGHT_REFRESH (on): view memory nothing wrote since the last asking
is known by a bit a block, copies nothing of which changed wake no thread,
the ends of a range are not copied at every refresh, long copies are split.
VICTORY_LAND_WINDOW=1: what shaders wrote goes into guarded title memory
through a view of ours of the section, the guard stays.
VICTORY_SEND_CHANGED=1: pages taken again go up to the card where they differ.
VICTORY_HEAP_SPREAD (on): a sixteenth of the heap's slots at every pass.
Three log lines split the preparing, the askings and the heap pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:03:45 -06:00
1 parent eb50022824
commit b8bf6d518c
9 files changed
+1549 -59

No files matched your search

+145 -10
View File
@@ -365,6 +365,126 @@ pub(crate) static HEAP_VIEWS_ELSEWHERE: AtomicU64 = AtomicU64::new(0);
/// index ([`State::heap_buffers`]), and the microseconds they took.
pub(crate) static HEAP_BUFFER_PASSES: AtomicU64 = AtomicU64::new(0);
pub(crate) static HEAP_BUFFER_MICROS: AtomicU64 = AtomicU64::new(0);
/// Of the passes, nanoseconds since the last frame line: looking at slots,
/// the spans and refreshes of their copies, the texture side.
#[cfg(feature = "diagnostics")]
pub(crate) static HEAP_SPLIT: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3];
/// Passes after which every buffer slot of the heap was looked at again.
const HEAP_ROUND: u64 = 16;
/// Whether the slots looked at again for no write are a part of them at
/// every pass, each slot at every sixteenth (`VICTORY_HEAP_SPREAD=0`: all of
/// them at every sixteenth pass, as before).
///
/// All at once was some 115 ms on a thread that records the frame, every
/// sixteenth frame (72,000 slots; run record-1: 11 ms a pass in the lines
/// without such a pass, 22 in those of ten frames with one). A slot is
/// looked at as often as before.
fn heap_spread() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_SPREAD").is_none_or(|v| v != "0"))
}
/// The host copy and range the buffer view of each of a heap's slots reads
/// ([`State::heap_buffers`]), by slot.
#[derive(Default)]
struct HeapRanges<T>(Vec<Option<(T, u64, u64)>>);
impl<T> HeapRanges<T> {
fn set(&mut self, slot: u32, range: Option<(T, u64, u64)>) {
let slot = slot as usize;
if range.is_none() && slot >= self.0.len() {
return;
}
if slot >= self.0.len() {
self.0.resize_with(slot + 1, || None);
}
self.0[slot] = range;
}
/// Forgets the slots of one of `parts` parts
/// (`HostHeap::buffer_slots_of`).
fn forget_part(&mut self, part: u32, parts: u32) {
self.0.iter_mut().skip(part as usize).step_by(parts as usize).for_each(|r| *r = None);
}
fn ranges(&self) -> impl Iterator<Item = &(T, u64, u64)> {
self.0.iter().flatten()
}
}
#[cfg(test)]
mod heap_range_tests {
use super::{HEAP_ROUND, HeapRanges};
/// Round by round every slot is looked at again once, whatever was
/// written between, and what a slot holds is what was found last.
#[test]
fn every_slot_is_looked_at_again_in_a_round() {
const SLOTS: u32 = 1000;
let mut ranges = HeapRanges(Vec::new());
ranges.set(7, None);
assert_eq!(ranges.ranges().count(), 0);
for slot in (0..SLOTS).step_by(3) {
ranges.set(slot, Some(((), 0, 1)));
}
let parts = HEAP_ROUND as u32;
for pass in 1..=HEAP_ROUND {
let part = (pass % HEAP_ROUND) as u32;
ranges.forget_part(part, parts);
for slot in (0..SLOTS).step_by(3).filter(|slot| slot % parts == part) {
assert!(ranges.0[slot as usize].is_none(), "slot {slot} not forgotten");
ranges.set(slot, Some(((), pass, pass)));
}
assert_eq!(ranges.ranges().count(), SLOTS.div_ceil(3) as usize);
}
assert!(ranges.ranges().all(|r| r.1 != 0), "a slot not looked at in a round");
ranges.set(3, None);
assert_eq!(ranges.ranges().count(), SLOTS.div_ceil(3) as usize - 1);
}
/// The spans of 4,400 copies from the ranges of 72,000 slots, as a pass
/// makes them, by slot and from the table they were in. Run with
/// `--ignored --nocapture`.
#[test]
#[ignore]
fn spans_of_a_pass() {
use std::collections::HashMap;
use std::sync::Arc;
let copies: Vec<Arc<u64>> = (0..4400).map(Arc::new).collect();
let (mut table, mut slots) = (HashMap::new(), HeapRanges(Vec::new()));
for slot in 0..72_000u32 {
let copy = &copies[slot as usize * 7919 % 4400];
table.insert(slot, (copy.clone(), u64::from(slot), u64::from(slot) + 64));
slots.set(slot, Some((copy.clone(), u64::from(slot), u64::from(slot) + 64)));
}
fn join<S: std::hash::BuildHasher + Default>(ranges: &mut dyn Iterator<Item = &(Arc<u64>, u64, u64)>) -> usize {
let mut spans: HashMap<usize, (Arc<u64>, u64, u64), S> = HashMap::default();
for (copy, from, to) in ranges {
let span = spans.entry(Arc::as_ptr(copy) as usize).or_insert_with(|| (copy.clone(), *from, *to));
span.1 = span.1.min(*from);
span.2 = span.2.max(*to);
}
spans.len()
}
let best = |f: &mut dyn FnMut() -> usize| {
(0..30)
.map(|_| {
let t = std::time::Instant::now();
assert_eq!(f(), 4400);
t.elapsed().as_secs_f64() * 1e3
})
.fold(f64::MAX, f64::min)
};
println!(
"{:.2} ms from the table, {:.2} ms by slot",
best(&mut || join::<std::hash::RandomState>(&mut table.values())),
best(&mut || join::<std::hash::BuildHasherDefault<crate::host::AddressHash>>(&mut slots.ranges())),
);
}
}
/// Textures brought up to date for shaders that pick them by index
/// ([`State::heap_textures`]), the bytes of their memory, and the slots
/// still waiting.
@@ -1368,18 +1488,25 @@ impl State {
// at best); all of them every 16 passes, because a view of a
// released resource goes stale without a write, and a slot written
// before its memory had a host copy is only made then.
type Ranges = std::collections::HashMap<u32, (std::sync::Arc<crate::shadow::Shadow>, u64, u64)>;
type Ranges = HeapRanges<std::sync::Arc<crate::shadow::Shadow>>;
static RANGES: std::sync::Mutex<Option<std::collections::HashMap<usize, (u64, Ranges)>>> =
std::sync::Mutex::new(None);
let mut kept = RANGES.lock().unwrap_or_else(|e| e.into_inner());
let (passes, ranges) = kept
.get_or_insert_with(Default::default)
.entry(std::ptr::from_ref(host) as usize)
.or_insert_with(|| (0, Ranges::new()));
.or_insert_with(|| (0, HeapRanges(Vec::new())));
let changed = host.buffer_changes();
let slots = if *passes % 16 == 0 {
ranges.clear();
let slots = if *passes == 0 || (*passes % HEAP_ROUND == 0 && !heap_spread()) {
ranges.0.clear();
host.buffer_slots()
} else if heap_spread() {
// The written ones, and this pass's part of the others.
let (part, parts) = ((*passes % HEAP_ROUND) as u32, HEAP_ROUND as u32);
ranges.forget_part(part, parts);
let mut slots = host.buffer_slots_of(part, parts);
slots.extend(changed);
slots
} else {
changed
};
@@ -1389,7 +1516,7 @@ impl State {
static GONE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
let mut alive = !GONE.load(Ordering::Relaxed) && crate::descriptor::host::device_removed().is_none();
for (n, &index) in slots.iter().enumerate() {
ranges.remove(&index);
ranges.set(index, None);
let Some(handle) = heap.handle(index) else {
continue;
};
@@ -1420,13 +1547,15 @@ impl State {
if host.shows(index, &shadow.buffer) == Some(false) {
HEAP_VIEWS_ELSEWHERE.fetch_add(1, Ordering::Relaxed);
}
ranges.insert(index, (shadow, address, address + len));
ranges.set(index, Some((shadow, address, address + len)));
}
let views = ranges.len() as u32;
#[cfg(feature = "diagnostics")]
let looked = std::time::Instant::now();
// Per host copy, the span its picked views cover.
let mut spans: std::collections::HashMap<usize, (std::sync::Arc<crate::shadow::Shadow>, u64, u64)> =
std::collections::HashMap::new();
for (shadow, from, to) in ranges.values() {
let mut spans: crate::host::ByCopy<(std::sync::Arc<crate::shadow::Shadow>, u64, u64)> = Default::default();
let mut views = 0u32;
for (shadow, from, to) in ranges.ranges() {
views += 1;
let key = std::sync::Arc::as_ptr(shadow) as usize;
let span = spans.entry(key).or_insert_with(|| (shadow.clone(), *from, *to));
span.1 = span.1.min(*from);
@@ -1466,7 +1595,13 @@ impl State {
}
shadow.refresh(list, *from, to - from);
}
#[cfg(feature = "diagnostics")]
let refreshed = std::time::Instant::now();
self.heap_textures(list, heap, host);
#[cfg(feature = "diagnostics")]
for (sum, time) in HEAP_SPLIT.iter().zip([looked - started, refreshed - looked, refreshed.elapsed()]) {
sum.fetch_add(time.as_nanos() as u64, Ordering::Relaxed);
}
HEAP_BUFFER_PASSES.fetch_add(1, Ordering::Relaxed);
HEAP_BUFFER_MICROS.fetch_add(started.elapsed().as_micros() as u64, Ordering::Relaxed);
static LOGGED: AtomicU64 = AtomicU64::new(0);