|
|
@@ -26,6 +26,9 @@ pub(crate) struct HostHeap {
|
|
|
sampler: bool,
|
|
|
/// The record (view index and generation) each slot was last made from.
|
|
|
made: Mutex<Vec<u64>>,
|
|
|
+ /// The same for a slot whose descriptor is made and stays (else 0): what
|
|
|
+ /// [`HostHeap::mirror`] asks first, without the lock. Changed under it.
|
|
|
+ quick: Vec<std::sync::atomic::AtomicU64>,
|
|
|
/// The records of the title's heap as the pass over records copied in
|
|
|
/// as bytes last saw them ([`HostHeap::copied_changes`]). Made on first
|
|
|
/// use.
|
|
|
@@ -1070,6 +1073,7 @@ impl HostHeap {
|
|
|
dynamic: extra,
|
|
|
sampler,
|
|
|
made: Mutex::new(vec![0; count as usize]),
|
|
|
+ quick: (0..count).map(|_| std::sync::atomic::AtomicU64::new(0)).collect(),
|
|
|
copied: Mutex::new(Vec::new()),
|
|
|
waiting: Mutex::default(),
|
|
|
away: Mutex::default(),
|
|
|
@@ -1606,6 +1610,7 @@ impl HostHeap {
|
|
|
/// Makes host slot `index` null again, as it is before its view is
|
|
|
/// made: a null view of the kind it held.
|
|
|
fn unmake(&self, made: &mut [u64], index: u32) {
|
|
|
+ self.quick_is(index, 0);
|
|
|
let Some(key) = made.get_mut(index as usize).map(std::mem::take) else {
|
|
|
return;
|
|
|
};
|
|
|
@@ -2019,15 +2024,7 @@ impl HostHeap {
|
|
|
return;
|
|
|
};
|
|
|
let records = &records[..last.len()];
|
|
|
- if last == records {
|
|
|
- return;
|
|
|
- }
|
|
|
- for (n, (old, new)) in last.chunks_exact_mut(step).zip(records.chunks_exact(step)).enumerate() {
|
|
|
- if old != new {
|
|
|
- old.copy_from_slice(new);
|
|
|
- out.push(first + n as u32);
|
|
|
- }
|
|
|
- }
|
|
|
+ changed_records(last, records, step, first, out);
|
|
|
}
|
|
|
|
|
|
/// Whether new records of draws' tables are left to the draws
|
|
|
@@ -2099,12 +2096,24 @@ impl HostHeap {
|
|
|
let [tag, i, generation] = unsafe { (handle as *const [u32; 3]).read_unaligned() };
|
|
|
// Generations start at 1, so a made slot's key is never 0.
|
|
|
let key = u64::from(generation) << 32 | u64::from(i);
|
|
|
+ // Made from this record, and nothing has changed it: most of the
|
|
|
+ // asks, from five recording threads, behind one lock while another
|
|
|
+ // held it to make a descriptor (15 % of a recording thread's time in
|
|
|
+ // the opening, profile cpu-1).
|
|
|
+ if tag == VIEW_TAG
|
|
|
+ && quick_mirror()
|
|
|
+ && self.quick.get(index as usize).is_some_and(|k| k.load(std::sync::atomic::Ordering::Acquire) == key)
|
|
|
+ && let Some(view) = view_at(i, generation)
|
|
|
+ {
|
|
|
+ return Some(view);
|
|
|
+ }
|
|
|
let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
let view = (tag == VIEW_TAG).then(|| view_at(i, generation)).flatten();
|
|
|
let Some(view) = view else {
|
|
|
// Not a view of ours (never written, or of a resource the
|
|
|
// title released): nothing to read.
|
|
|
if made[index as usize] != 0 {
|
|
|
+ self.quick_is(index, 0);
|
|
|
made[index as usize] = 0;
|
|
|
self.make_null(index);
|
|
|
let _ = self.note_buffer(index, None, false);
|
|
|
@@ -2147,6 +2156,7 @@ impl HostHeap {
|
|
|
KEPT_VIEWS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
|
|
return Some(view);
|
|
|
}
|
|
|
+ self.quick_is(index, 0);
|
|
|
made[index as usize] = if later { 0 } else { key };
|
|
|
let kept = if later {
|
|
|
None
|
|
|
@@ -2157,10 +2167,19 @@ impl HostHeap {
|
|
|
null_for(&view, self.cpu(index));
|
|
|
}
|
|
|
self.keep(index, kept.flatten());
|
|
|
+ self.quick_is(index, made[index as usize]);
|
|
|
}
|
|
|
Some(view)
|
|
|
}
|
|
|
|
|
|
+ /// Slot `index` is made from record `key` and stays so (0: not). Under
|
|
|
+ /// the lock of `made`.
|
|
|
+ fn quick_is(&self, index: u32, key: u64) {
|
|
|
+ if let Some(quick) = self.quick.get(index as usize) {
|
|
|
+ quick.store(key, std::sync::atomic::Ordering::Release);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/// Whether slot `index` is backed by a made host descriptor (tests).
|
|
|
#[cfg(test)]
|
|
|
pub(crate) fn made(&self, index: u32) -> bool {
|
|
|
@@ -2987,6 +3006,65 @@ fn keep_views() -> bool {
|
|
|
*ON.get_or_init(|| std::env::var_os("VICTORY_KEEP_VIEWS").is_none_or(|v| v != "0"))
|
|
|
}
|
|
|
|
|
|
+/// Whether a slot already made from its record is told so without the lock
|
|
|
+/// (`VICTORY_QUICK_MIRROR=0`: with it, as before).
|
|
|
+fn quick_mirror() -> bool {
|
|
|
+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
|
|
+ *ON.get_or_init(|| std::env::var_os("VICTORY_QUICK_MIRROR").is_none_or(|v| v != "0"))
|
|
|
+}
|
|
|
+
|
|
|
+/// The records (`step` bytes each, the first is slot `first`) that differ
|
|
|
+/// between `last` and `records` (of one length) go into `out` and `last`.
|
|
|
+/// Most of a changed page is as it was: it is looked at sixteen records at a
|
|
|
+/// time.
|
|
|
+fn changed_records(last: &mut [u8], records: &[u8], step: usize, first: u32, out: &mut Vec<u32>) {
|
|
|
+ const AT_A_TIME: usize = 16;
|
|
|
+ let block = AT_A_TIME * step;
|
|
|
+ for (b, (old, new)) in last.chunks_mut(block).zip(records.chunks(block)).enumerate() {
|
|
|
+ if old == new {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (n, (old, new)) in old.chunks_exact_mut(step).zip(new.chunks_exact(step)).enumerate() {
|
|
|
+ if old != new {
|
|
|
+ old.copy_from_slice(new);
|
|
|
+ out.push(first + (b * AT_A_TIME + n) as u32);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(test)]
|
|
|
+mod changed_records_tests {
|
|
|
+ use super::changed_records;
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn changed_records_are_those_that_differ_one_by_one() {
|
|
|
+ for (step, count) in [(32usize, 128usize), (32, 2048), (16, 77), (32, 5), (32, 17)] {
|
|
|
+ let mut seed = 7u64;
|
|
|
+ let mut random = move || {
|
|
|
+ seed = seed.wrapping_mul(0x5851_F42D_4C95_7F2D).wrapping_add(0x1405_7B7E_F767_814F);
|
|
|
+ (seed >> 33) as usize
|
|
|
+ };
|
|
|
+ let mut last: Vec<u8> = (0..step * count).map(|_| random() as u8).collect();
|
|
|
+ let mut now = last.clone();
|
|
|
+ let mut differ = Vec::new();
|
|
|
+ for slot in 0..count {
|
|
|
+ if random() % 9 == 0 {
|
|
|
+ now[slot * step + random() % step] ^= 1 + (random() % 255) as u8;
|
|
|
+ differ.push(1000 + slot as u32);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let mut out = Vec::new();
|
|
|
+ changed_records(&mut last, &now, step, 1000, &mut out);
|
|
|
+ assert_eq!(out, differ);
|
|
|
+ assert_eq!(last, now);
|
|
|
+ out.clear();
|
|
|
+ changed_records(&mut last, &now, step, 1000, &mut out);
|
|
|
+ assert!(out.is_empty());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/// Whether both are shader views of one texture the title placed in
|
|
|
/// memory: the same address and the same resource description, whatever
|
|
|
/// part of it each shows.
|