From 8f662ea2b306f795b10a837b05cd6d381d0dc511 Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:42:55 -0500 Subject: [PATCH] Sparse windows: commits before the device are merged page ranges, not a list scanned per page Before the device exists, `sparse::committed` appended one entry per call to a list and `sparse::decommitted` scanned the whole list, so moving N pages away cost O(N^2). The list is now a BTreeMap of disjoint, merged page ranges: a commit or a decommit is O(log n). A range counts every page it touches, as `committed` / `decommitted` do once the state exists, and a decommit now takes out only its pages. The list dropped every entry a decommit overlapped, so after `commit_homes` (several pages in one call) and `move_home_away` (one of them), `init` left the still-committed pages as holes. Tests: random sequences against a page-set model and against the old list (one entry per page, the case it handled exactly), one page out of a multi-page range, and an ignored timing probe (`early_commit_timing`). Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/xcl/src/hle/d3d12x/sparse.rs | 184 ++++++++++++++++++++++++++-- 1 file changed, 177 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 3cf45e559..bcee2b4bb 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -16,7 +16,7 @@ //! Buffers the title places there get reserved resources over the same tiles, so PC-side //! copies, views and barriers see the same memory. -use std::collections::{HashMap, VecDeque}; +use std::collections::{BTreeMap, HashMap, VecDeque}; use std::ffi::c_void; use std::hash::{BuildHasherDefault, Hasher}; use std::sync::atomic::{AtomicU64, Ordering}; @@ -395,8 +395,60 @@ pub fn pool_bytes() -> (usize, usize) { let pools = s.pools.iter().filter(|p| p.is_some()).count() * POOL_TILES as usize * TILE; (pools, pools.saturating_sub(s.free.len() * TILE)) } -/// commits before the device existed -static EARLY: Mutex> = Mutex::new(Vec::new()); +/// commits before the device existed: the pages committed, as disjoint page ranges (start → +/// end) merged with their neighbours, so a commit or decommit costs O(log n) and not a scan of +/// every page committed so far. Like `committed` / `decommitted` with a state, a range counts +/// every page it touches; `init` commits them once the windows exist. +static EARLY: Mutex> = Mutex::new(BTreeMap::new()); + +/// The pages `[a, a + n)` touches, as `[start, end)`. +fn page_span(a: usize, n: usize) -> (usize, usize) { + (a & !(PAGE - 1), a.saturating_add(n).saturating_add(PAGE - 1) & !(PAGE - 1)) +} + +/// Add the pages `[a, b)` to `early`, merged with the ranges they overlap or touch. +fn early_add(early: &mut BTreeMap, mut a: usize, mut b: usize) { + if a >= b { + return; + } + if let Some((&s, &e)) = early.range(..=a).next_back() { + if e >= a { + a = s; + b = b.max(e); + early.remove(&s); + } + } + loop { + let Some((s, e)) = early.range(a..=b).next().map(|(&s, &e)| (s, e)) else { break }; + b = b.max(e); + early.remove(&s); + } + early.insert(a, b); +} + +/// Take the pages `[a, b)` out of `early`; a range that overlaps it keeps the rest. +fn early_remove(early: &mut BTreeMap, a: usize, b: usize) { + if a >= b { + return; + } + if let Some((&s, &e)) = early.range(..a).next_back() { + if e > a { + early.insert(s, a); + if e > b { + early.insert(b, e); + return; + } + } + } + loop { + let Some((s, e)) = early.range(a..b).next().map(|(&s, &e)| (s, e)) else { break }; + early.remove(&s); + if e > b { + early.insert(b, e); + break; + } + } +} /// generation counter incremented whenever the set of mapped tiles changes (for watch_ranges cache) static MAPPED_GEN: AtomicU64 = AtomicU64::new(0); @@ -494,8 +546,8 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> { Ok(s) => { crate::xlog_info!("D3D12.X: {} sparse window(s) for the title's own memory (tiled buffers on video memory)", out.len()); *STATE.lock().unwrap() = Some(s); - for (a, n) in std::mem::take(&mut *EARLY.lock().unwrap()) { - committed(a, n); + for (a, b) in std::mem::take(&mut *EARLY.lock().unwrap()) { + committed(a, b - a); } out } @@ -1192,7 +1244,8 @@ pub unsafe fn committed(a: usize, n: usize) { } let mut g = STATE.lock().unwrap(); let Some(s) = g.as_mut() else { - EARLY.lock().unwrap().push((a, n)); + let (start, end) = page_span(a, n); + early_add(&mut EARLY.lock().unwrap(), start, end); return; }; s.ensure_mapped(a, n, |_| 0xFFFF); @@ -1224,7 +1277,8 @@ pub unsafe fn decommitted(a: usize, n: usize) { } let mut g = STATE.lock().unwrap(); let Some(s) = g.as_mut() else { - EARLY.lock().unwrap().retain(|&(x, m)| x + m <= a || x >= a + n); + let (start, end) = page_span(a, n); + early_remove(&mut EARLY.lock().unwrap(), start, end); return; }; let tiles = decommit_tiles(&mut s.mapped, &s.pinned, &mut s.free, &mut s.to_zero, a, n); @@ -1699,4 +1753,120 @@ mod tests { assert_eq!(free, vec![(0, 13)]); assert_eq!(to_zero, vec![(t4, PAGE)], "unrelated mapped tile's to_zero must remain"); } + + /// The pages in `early`, checking that its ranges are page-aligned, not empty, disjoint and + /// merged (no two touch). + fn early_pages(early: &BTreeMap) -> std::collections::BTreeSet { + let mut last_end = None; + let mut pages = std::collections::BTreeSet::new(); + for (&a, &b) in early { + assert!(a % PAGE == 0 && b % PAGE == 0 && a < b, "range {a:#x}..{b:#x}"); + assert!(last_end.is_none_or(|e| e < a), "range {a:#x}..{b:#x} touches the one before"); + last_end = Some(b); + pages.extend((a..b).step_by(PAGE)); + } + pages + } + + /// xorshift64*: the same sequence on every run + fn next(x: &mut u64) -> u64 { + *x ^= *x >> 12; + *x ^= *x << 25; + *x ^= *x >> 27; + x.wrapping_mul(0x2545_F491_4F6C_DD1D) + } + + /// Before the device exists, commits and decommits leave the pages a plain page set would + /// hold, with the live path's rule (a range counts every page it touches): random ranges, + /// unaligned too, over 300 pages. + #[test] + fn early_commits_are_the_pages_a_page_set_holds() { + let base = 0x4_2000_0000usize; + for seed in 1..=8u64 { + let mut x = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1; + let (mut early, mut model) = (BTreeMap::new(), std::collections::BTreeSet::new()); + for _ in 0..4000 { + let a = base + (next(&mut x) % (300 * PAGE as u64)) as usize; + let n = match next(&mut x) % 4 { + 0 => PAGE, + 1 => 1 + (next(&mut x) % 64) as usize, + _ => 1 + (next(&mut x) % (24 * PAGE as u64)) as usize, + }; + let (start, end) = page_span(a, n); + let pages = (start..end).step_by(PAGE); + if next(&mut x) % 3 == 0 { + early_remove(&mut early, start, end); + pages.for_each(|p| { + model.remove(&p); + }); + } else { + early_add(&mut early, start, end); + model.extend(pages); + } + assert_eq!(early_pages(&early), model, "seed {seed}"); + } + } + } + + /// The same pages as the list this replaced, for what that list handled exactly: one + /// entry per committed page (`XMemMapPhysicalPagesScatter`, `home_again`) and page-aligned + /// decommits of any length. + #[test] + fn early_commits_match_the_old_list_for_page_entries() { + let base = 0x4_2000_0000usize; + let mut x = 0x1234_5678_9ABC_DEF1u64; + let (mut early, mut old): (BTreeMap, Vec<(usize, usize)>) = (BTreeMap::new(), Vec::new()); + for _ in 0..20000 { + let a = base + (next(&mut x) % 512) as usize * PAGE; + if next(&mut x) % 2 == 0 { + old.push((a, PAGE)); + early_add(&mut early, a, a + PAGE); + } else { + let n = (1 + next(&mut x) % 8) as usize * PAGE; + old.retain(|&(y, m)| y + m <= a || y >= a + n); + early_remove(&mut early, a, a + n); + } + } + let old_pages: std::collections::BTreeSet = old.iter().map(|&(y, _)| y).collect(); + assert_eq!(early_pages(&early), old_pages); + } + + /// A range committed in one call keeps its other pages when one page of it is decommitted + /// (`commit_homes` commits several pages, `move_home_away` gives one back). The list this + /// replaced dropped the whole entry, so `init` left those still-committed pages as holes. + #[test] + fn early_decommit_of_one_page_keeps_the_rest() { + let a = 0x4_2000_0000usize; + let mut early = BTreeMap::new(); + early_add(&mut early, a, a + 4 * TILE); + early_remove(&mut early, a + TILE, a + 2 * TILE); + assert_eq!(early.into_iter().collect::>(), vec![(a, a + TILE), (a + 2 * TILE, a + 4 * TILE)]); + } + + /// Time for n 64 KiB pages committed one at a time and decommitted one at a time in + /// reverse, before the device exists (`cargo test --release -p xcl --lib -- + /// --ignored --exact hle::d3d12x::sparse::tests::early_commit_timing --nocapture`, + /// `XCL_EARLY_PAGES=16384,32768`). Run it alone: a test that made the device keeps a state. + #[test] + #[ignore] + fn early_commit_timing() { + if STATE.lock().unwrap().is_some() { + return eprintln!("early_commit_timing: a sparse state exists – run it alone"); + } + let sizes: Vec = std::env::var("XCL_EARLY_PAGES").ok().map_or(vec![4096, 8192, 16384, 32768], |v| v.split(',').map(|s| s.trim().parse().unwrap()).collect()); + let base = 0x4_2000_0000usize; + for n in sizes { + let t = std::time::Instant::now(); + for i in 0..n { + unsafe { committed(base + i * TILE, TILE) }; + } + let commit = t.elapsed(); + let t = std::time::Instant::now(); + for i in (0..n).rev() { + unsafe { decommitted(base + i * TILE, TILE) }; + } + let decommit = t.elapsed(); + println!("early: {n} pages: commit {:.1} ms, decommit {:.1} ms", commit.as_secs_f64() * 1e3, decommit.as_secs_f64() * 1e3); + } + } }