From be675f99a20df534bef15072b92054427717a06c Mon Sep 17 00:00:00 2001 From: stoltembergg Date: Wed, 25 Nov 2026 07:41:37 +0000 Subject: [PATCH 1/2] GPU: find shadow buffers past smaller placed entries Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-gpu/src/shadow.rs | 74 +++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 133e871a..0c6d0492 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -2762,16 +2762,23 @@ struct Entry { /// Title buffers by (start address, resource), with their copy once made. static BUFFERS: Mutex> = Mutex::new(BTreeMap::new()); +/// The longest title buffer registered so far (it stays after the buffer is +/// released): a buffer starting further below an address than this cannot +/// hold it. Changed under `BUFFERS`' lock. +static MAX_SPAN: AtomicU64 = AtomicU64::new(0); + fn buffers() -> std::sync::MutexGuard<'static, BTreeMap<(u64, usize), Entry>> { BUFFERS.lock().unwrap_or_else(|e| e.into_inner()) } /// A title buffer exists at `start` (`len` bytes). pub(crate) fn register(resource: usize, start: u64, len: u64) { - buffers().insert( + let mut map = buffers(); + MAX_SPAN.fetch_max(len, Ordering::Relaxed); + map.insert( (start, resource), Entry { - end: start + len, + end: start.saturating_add(len), shadow: None, }, ); @@ -2786,12 +2793,10 @@ pub(crate) fn unregister(resource: usize, start: u64) { /// its copy (diagnostics: which buffer a dropped program's root view names). #[cfg(feature = "diagnostics")] pub(crate) fn extent(address: u64) -> Option<(u64, u64)> { - buffers() - .range(..=(address, usize::MAX)) - .rev() - .take(64) - .find(|(_, e)| e.end > address) - .map(|(k, e)| (k.0, e.end - k.0)) + let map = buffers(); + let end = address.saturating_add(1); + enclosing(&map, MAX_SPAN.load(Ordering::Relaxed), address, end) + .and_then(|key| map.get(&key).map(|e| (key.0, e.end - key.0))) } /// Title memory `[at, at + len)` is about to show something else or be @@ -2836,6 +2841,27 @@ pub(crate) fn spans() -> Vec<(u64, u64, bool)> { spans } +/// The innermost entry holding `[address, end)`: the one starting closest +/// below `address` that reaches `end`, among those starting at most `span` +/// (the longest entry's length) below `end`. Big buffers hold many smaller +/// ones (a heap's placed buffers), so the one holding an address past +/// those can be any number of entries back. +fn enclosing( + map: &BTreeMap<(u64, usize), Entry>, + span: u64, + address: u64, + end: u64, +) -> Option<(u64, usize)> { + let lowest = end.saturating_sub(span); + if lowest > address { + return None; + } + map.range((lowest, 0)..=(address, usize::MAX)) + .rev() + .find(|(_, e)| e.end >= end) + .map(|(k, _)| *k) +} + /// The copy of the innermost title buffer holding `[address, address + /// len)`, made on first use. A `len` of 0 or 1 is an ask without a length /// (a root view: the caller reads on to the copy's end). @@ -2871,12 +2897,7 @@ pub(crate) fn find_any(address: u64, len: u64) -> Option> { fn lookup(address: u64, len: u64, any: bool) -> Option> { let end = address.checked_add(len.max(1))?; let mut map = buffers(); - let key = map - .range(..=(address, usize::MAX)) - .rev() - .take(64) - .find(|(_, e)| e.end >= end) - .map(|(k, _)| *k); + let key = enclosing(&map, MAX_SPAN.load(Ordering::Relaxed), address, end); let Some(key) = key else { drop(map); return loose(address, end, len <= 1, any); @@ -3156,7 +3177,30 @@ fn loose(address: u64, end: u64, unsized_ask: bool, any: bool) -> Option Date: Wed, 25 Nov 2026 07:44:04 +0000 Subject: [PATCH 2/2] GPU: test enclosing lookup past all small entries Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-gpu/src/shadow.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 0c6d0492..083e0794 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -3194,7 +3194,7 @@ mod window_tests { let at = base + i * 0x1_0000; map.insert((at, 100 + i as usize), entry(at, 0x1_0000)); } - let ask = base + 96 * 0x1_0000; + let ask = base + 128 * 0x1_0000; assert_eq!( enclosing(&map, 256 << 20, ask, ask + 4), Some((base, 1)),