Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 93 additions & 15 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize, std::time::Instant>,
/// the ranges `known` was made for
/// the ranges of the last look (a block new to the ranges starts hot: `new_blocks`)
ranges: Option<Arc<Vec<(usize, usize)>>>,
/// the blocks of those ranges (a block new to the ranges starts hot)
known: std::collections::HashSet<usize>,
}

/// 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<usize> {
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<Option<HotBlocks>> = Mutex::new(None);
Expand All @@ -1525,19 +1548,10 @@ fn hot_runs(ranges: &Arc<Vec<(usize, usize)>>, 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);
Expand Down Expand Up @@ -5636,6 +5650,70 @@ pub(super) mod tests {
assert_eq!(watch_ranges(&regions, 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<usize> {
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<usize> {
let known = blocks_of(old);
let mut v: Vec<usize> = 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<(usize, usize)>> = 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;
Expand Down
Loading