From d8c75c5c135a368f4ea46efe026dad572d3abe74 Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Sun, 29 Nov 2026 03:51:23 +0100 Subject: [PATCH] D3D12.X: a look finds the blocks new to the watched ranges by their change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `hot_runs` makes a block new to the watched ranges hot. To find those it kept a set of all blocks (2 MiB) of the ranges of the last look and, each time the ranges changed, made the set again over every block of every range while holding `HOT` (the walk thread takes it too). The ranges are made again whenever a tile is mapped, a texture is placed, a GPU-view page is shown: in a retail title's world that is ~3.5 GB in 16 ranges, ~1,800 blocks, and the ranges change between many of the submits. Now `new_blocks` compares the ranges with the last look's (both sorted and apart, as `watch_ranges` makes them): a range found among the old ones as it is has no new block, and a block of a changed range is new when no old range meets it, which is what the set said. 16 ranges with one changed: 4 µs against 1.4 ms for a set of all their blocks (debug build, the test's own timing; not measured in a release build or a game). Test: new_blocks_are_those_no_old_range_meets (against a set of all the old blocks: ranges grown, shifted, merged, new, gone, in part of a block). Ported from amd-dev without its reasons for a hot block (`HOT_NEW`, `look_stats::HOT_WHY`), which main does not have: a new block is put in with its time only, as before. (cherry picked from commit 5b660277a9460246eca668b144872fe4c66c1594) (cherry picked from commit 5ff2b66338097c8d02657d79b524592c453a11a1) Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/title_textures.rs | 108 +++++++++++++++++--- 1 file changed, 93 insertions(+), 15 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index d809b1c2a2..30311bc238 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -1502,10 +1502,33 @@ fn hot_for() -> std::time::Duration { struct HotBlocks { /// block -> when a look or the walk last found a written page in it hot: BTreeMap, - /// the ranges `known` was made for + /// the ranges of the last look (a block new to the ranges starts hot: `new_blocks`) ranges: Option>>, - /// the blocks of those ranges (a block new to the ranges starts hot) - known: std::collections::HashSet, +} + +/// The `HOT_BLOCK`s of `ranges` that no range of `old` meets (both sorted and apart, as `watch_ranges` +/// makes them): a range found in `old` as it is has none, so the ranges changed by a mapping are +/// gone over and not all blocks of all of them. (Before, a set of all the blocks was made again +/// at each change of the ranges: ~1,800 blocks, 3.5 GB, in a retail title's world.) +fn new_blocks(old: &[(usize, usize)], ranges: &[(usize, usize)]) -> Vec { + let met = |lo: usize, hi: usize| { + let i = old.partition_point(|&(a, n)| a + n <= lo); + old.get(i).is_some_and(|&(a, _)| a < hi) + }; + let mut out = Vec::new(); + for &(base, size) in ranges { + if old.binary_search(&(base, size)).is_ok() { + continue; + } + let mut b = base & !(HOT_BLOCK - 1); + while b < base + size { + if !met(b, b + HOT_BLOCK) { + out.push(b); + } + b += HOT_BLOCK; + } + } + out } static HOT: Mutex> = Mutex::new(None); @@ -1525,19 +1548,10 @@ fn hot_runs(ranges: &Arc>, now: std::time::Instant) -> (Vec< let h = g.get_or_insert_with(HotBlocks::default); // a block new to the ranges starts hot: memory just mapped is often written and used at once if !h.ranges.as_ref().is_some_and(|r| Arc::ptr_eq(r, ranges)) { - let mut known = std::collections::HashSet::with_capacity(h.known.len()); - for &(base, size) in ranges.iter() { - let mut b = base & !(HOT_BLOCK - 1); - while b < base + size { - if !h.known.contains(&b) { - h.hot.insert(b, now); - } - known.insert(b); - b += HOT_BLOCK; - } + let old = h.ranges.replace(Arc::clone(ranges)); + for b in new_blocks(old.as_deref().map_or(&[], |v| &v[..]), ranges) { + h.hot.insert(b, now); } - h.known = known; - h.ranges = Some(Arc::clone(ranges)); } let keep = hot_for(); h.hot.retain(|_, t| now.saturating_duration_since(*t) < keep); @@ -5636,6 +5650,70 @@ pub(super) mod tests { assert_eq!(watch_ranges(®ions, vec![(0x1000_0000, 0x1000_1000), (0x2008_0000, 0x2009_0000)]), vec![(0x2008_0000, 0x1_0000)]); } + /// `new_blocks` finds the blocks a set of all the old ranges' blocks did not have (what + /// `hot_runs` made before), going over the changed ranges only. + #[test] + fn new_blocks_are_those_no_old_range_meets() { + let blocks_of = |ranges: &[(usize, usize)]| -> std::collections::BTreeSet { + let mut out = std::collections::BTreeSet::new(); + for &(base, size) in ranges { + let mut b = base & !(HOT_BLOCK - 1); + while b < base + size { + out.insert(b); + b += HOT_BLOCK; + } + } + out + }; + let reference = |old: &[(usize, usize)], new: &[(usize, usize)]| -> Vec { + let known = blocks_of(old); + let mut v: Vec = Vec::new(); + for &(base, size) in new { + let mut b = base & !(HOT_BLOCK - 1); + while b < base + size { + if !known.contains(&b) { + v.push(b); + } + b += HOT_BLOCK; + } + } + v + }; + let g = 0x4000_0000usize; + let m = 1usize << 20; + let old = vec![(g, 8 * m), (g + 64 * m, 3 * m), (g + 70 * m + 0x1000, 0x1000), (g + 200 * m, 64 * m)]; + let cases: Vec> = vec![ + old.clone(), + vec![], + // one grown at its end, one shifted inside its blocks, one new, one gone + vec![(g, 13 * m), (g + 64 * m + 0x1000, 3 * m), (g + 100 * m, 5 * m), (g + 200 * m, 64 * m)], + // a range now over what two were + vec![(g, 80 * m)], + // pieces of blocks the old ranges met only in part + vec![(g + 67 * m - 0x1000, 0x2000), (g + 71 * m, 0x1000), (g + 264 * m, 0x1000)], + ]; + for new in &cases { + assert_eq!(new_blocks(&old, new), reference(&old, new), "{new:x?}"); + assert_eq!(new_blocks(&[], new), reference(&[], new)); + } + assert!(new_blocks(&old, &old).is_empty()); + assert_eq!(new_blocks(&old, &cases[2]), vec![g + 8 * m, g + 10 * m, g + 12 * m, g + 100 * m, g + 102 * m, g + 104 * m]); + // (the measured case: ~1,800 blocks in 16 ranges, one of them changed) + let big: Vec<(usize, usize)> = (0..16).map(|k| (g + k * (256 * m), 224 * m)).collect(); + let mut changed = big.clone(); + changed[7].1 += 2 * m; + let t = std::time::Instant::now(); + for _ in 0..100 { + assert_eq!(new_blocks(&big, &changed), vec![changed[7].0 + 224 * m]); + } + let ours = t.elapsed(); + let t = std::time::Instant::now(); + for _ in 0..100 { + assert_eq!(reference(&big, &changed), vec![changed[7].0 + 224 * m]); + } + println!("new blocks of 16 ranges, one changed: {:?} a time, a set of all blocks {:?}", ours / 100, t.elapsed() / 100); + } + #[test] fn watch_ranges_merge_at_the_gap_limit_and_split_past_it() { let base = 0x1000_0000usize;