The sweep lets go placed host copies whose title memory is unmapped #1348

Open
HailLucifer666 wants to merge 6 commits from upstream-pr/1348 into main
HailLucifer666 commented 2026-11-28 17:42:45 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by HailLucifer666 on 2026-11-28T17:42:45Z.


The census counts a state nothing acts on: placed textures the title fills whose title memory is not mapped any more (resource.rs count[8], sampled every 16th because asking Windows about all of them takes too long). Such a copy's source is gone — it can never be refreshed from title memory — so past its last use it is pure VRAM waste, which the reports show the cost of: #453 (graphics memory full within a minute, 29→<10 fps over 30 min), #609 (3,072 MB kept for render targets never given back on an 8 GB card).

The mechanism

  • title_memory_unmapping records the range in O(1) (resource::unmapped); title_memory_remapped clears ranges it covers; the sweep drains the record and, for a placed texture overlapping it, applies the census's own idle window for that exact state (30 s / 60 frames — UNMAPPED_IDLE_MS/UNMAPPED_IDLE_FRAMES, one constant now shared by the census's count and the sweep's gate) instead of the VRAM-pressure line, which can be minutes.
  • Everything else is the sweep's existing gate, unchanged: no list holds it, no descriptor slot views it, the host copy is held alone. host_wrote textures keep their existing skip (conservative: fewer evictions, never more). The per-sweep byte budget still bounds work; unmapped copies compete under it and continue over successive looks.
  • The walk is the sweep's, never the unmapping thread's. Our first form of this idea on Marvisak/VIctory (#432) walked per-unmap and cost 975 ms of every second of a load — recorded in both codebases. That is also why this is not a hook inside title_memory_unmapping's walk and adds no per-texture Windows query to the sweep (the census samples every 16th for exactly that cost).

Tests and probes (resource::tests, 24 → part of victory-gpu's 458 passed / 0 failed / 36 ignored, clippy --all-targets --all-features -D warnings clean):

  • overlap_any_is_half_open_on_both_ends — half-open boundaries, containment, zero-length, and the top-of-space saturation (my own first assertion there was wrong; the arithmetic was right — the test was corrected, noted here).
  • the_unmapped_record_is_bounded_oldest_first — with the sweep off, the record ages out instead of growing without end.
  • the_idle_gate_borrows_the_census_window_only_for_an_unmapped_source — mutation probes run and recorded: the unmapped arm disabled → red; the overlap boundary flipped → red. Restored byte-identical.
  • the_unmapped_window_is_the_census_window — one constant, two readers, pinned.

Stacks on #1290/#1294/#1326 for CI. The behavior claim that remains unproven is the in-game one (no machine here runs the title): what this PR provably does is make the sweep act on a state the census already measures, under the census's own safety window, with the sweep's own liveness gates unchanged.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5542929912 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1348). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-28T17:42:45Z. --- The census counts a state nothing acts on: **placed textures the title fills whose title memory is not mapped any more** (`resource.rs` count[8], sampled every 16th because asking Windows about all of them takes too long). Such a copy's source is gone — it can never be refreshed from title memory — so past its last use it is pure VRAM waste, which the reports show the cost of: #453 (graphics memory full within a minute, 29→<10 fps over 30 min), #609 (3,072 MB kept for render targets never given back on an 8 GB card). **The mechanism** - `title_memory_unmapping` records the range in **O(1)** (`resource::unmapped`); `title_memory_remapped` clears ranges it covers; the sweep drains the record and, for a placed texture overlapping it, applies the **census's own idle window for that exact state** (30 s / 60 frames — `UNMAPPED_IDLE_MS`/`UNMAPPED_IDLE_FRAMES`, one constant now shared by the census's count and the sweep's gate) instead of the VRAM-pressure line, which can be minutes. - Everything else is the sweep's existing gate, unchanged: no list holds it, no descriptor slot views it, the host copy is held alone. `host_wrote` textures keep their existing skip (conservative: fewer evictions, never more). The per-sweep byte budget still bounds work; unmapped copies compete under it and continue over successive looks. - **The walk is the sweep's, never the unmapping thread's.** Our first form of this idea on Marvisak/VIctory (#432) walked per-unmap and cost 975 ms of every second of a load — recorded in both codebases. That is also why this is not a hook inside `title_memory_unmapping`'s walk and adds no per-texture Windows query to the sweep (the census samples every 16th for exactly that cost). **Tests and probes** (`resource::tests`, 24 → part of victory-gpu's **458 passed / 0 failed / 36 ignored**, clippy `--all-targets --all-features -D warnings` clean): - `overlap_any_is_half_open_on_both_ends` — half-open boundaries, containment, zero-length, and the top-of-space saturation (my own first assertion there was wrong; the arithmetic was right — the test was corrected, noted here). - `the_unmapped_record_is_bounded_oldest_first` — with the sweep off, the record ages out instead of growing without end. - `the_idle_gate_borrows_the_census_window_only_for_an_unmapped_source` — mutation probes **run and recorded**: the unmapped arm disabled → red; the overlap boundary flipped → red. Restored byte-identical. - `the_unmapped_window_is_the_census_window` — one constant, two readers, pinned. Stacks on #1290/#1294/#1326 for CI. The behavior claim that remains unproven is the in-game one (no machine here runs the title): what this PR provably does is make the sweep act on a state the census already measures, under the census's own safety window, with the sweep's own liveness gates unchanged.
gut0to commented 2026-12-01 14:43:49 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by gut0to on 2026-12-01T14:43:49Z.

Code review location: crates/victory-gpu/src/resource.rs; line 2344

@@ -2305,10 +2305,83 @@ pub(crate) fn card_accounting(room: bool, due: [u64; 5], never: Vec<(u64, u64)>,
 /// Every caller uses it at once, within the title's call.
 /// Called before a submission starts, by the thread that submits.
 /// `VICTORY_TEXTURE_SWEEP=0` lets nothing go.
+/// The sweep's own frame floor for a mapped source (was a local of
+/// `let_go_idle`; the gate below shares it).
+const IDLE_FRAMES: u64 = 4;
+
+/// The idle gate the census uses for a host copy whose title memory is
+/// unmapped: 30 s and 60 frames. The sweep's unmapped arm borrows exactly
+/// this window (one constant, two readers: the census counts the state, the
+/// sweep acts on it).
+const UNMAPPED_IDLE_MS: u64 = 30_000;
+const UNMAPPED_IDLE_FRAMES: u64 = 60;
+
+/// The ranges the title unmapped since the sweep last looked. Recorded on
+/// the unmapping thread in O(1); the sweep drains and walks them. Bounded:
+/// with the sweep off (`VICTORY_TEXTURE_SWEEP=0`) entries age out oldest
+/// first rather than growing without end.
+static UNMAPPED: std::sync::Mutex<Vec<(u64, u64)>> = std::sync::Mutex::new(Vec::new());
+const UNMAPPED_RANGES_CAP: usize = 4096;
+
+/// The title unmapped `[at, at + len)`: placed host copies whose bytes live
+/// in that range have lost their source. O(1) -- the walk is the sweep's.
+pub(crate) fn unmapped(at: u64, len: u64) {
+    if len == 0 {
+        return;
+    }
+    let mut ranges = UNMAPPED.lock().unwrap_or_else(|e| e.into_inner());
+    push_bounded(&mut ranges, (at, at.saturating_add(len)), UNMAPPED_RANGES_CAP);
+}
+
+/// The title mapped `[at, at + len)` back: recorded ranges under it say the
+/// opposite of what is now true, so they go.
+pub(crate) fn remapped(at: u64, len: u64) {
+    if len == 0 {
+        return;
+    }
+    let (start, end) = (at, at.saturating_add(len));
+    let mut ranges = UNMAPPED.lock().unwrap_or_else(|e| e.into_inner());
+    ranges.retain(|&(r, r_end)| !overlap_any(r, r_end - r, &[(start, end)]));

This drops an entire recorded interval on any overlap, but XMemMapPhysicalPages can remap only part of a previously unmapped range. For example, after unmapping [0, 64 KiB), remapping [16, 32 KiB) should leave the prefix and suffix recorded; removing the whole interval makes the next texture sweep miss those still-unmapped pages and retain their placed copies as if the source were mapped. Could you subtract the remapped interval and preserve the remaining pieces, with a middle-range remap test? AI-assisted review; I traced the map notification and sweep bookkeeping.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review-comment:4755858208 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1348#discussion_r4755858208). Originally posted by [gut0to](https://github.com/gut0to) on 2026-12-01T14:43:49Z. **Code review location:** `crates/victory-gpu/src/resource.rs`; line 2344 ~~~~diff @@ -2305,10 +2305,83 @@ pub(crate) fn card_accounting(room: bool, due: [u64; 5], never: Vec<(u64, u64)>, /// Every caller uses it at once, within the title's call. /// Called before a submission starts, by the thread that submits. /// `VICTORY_TEXTURE_SWEEP=0` lets nothing go. +/// The sweep's own frame floor for a mapped source (was a local of +/// `let_go_idle`; the gate below shares it). +const IDLE_FRAMES: u64 = 4; + +/// The idle gate the census uses for a host copy whose title memory is +/// unmapped: 30 s and 60 frames. The sweep's unmapped arm borrows exactly +/// this window (one constant, two readers: the census counts the state, the +/// sweep acts on it). +const UNMAPPED_IDLE_MS: u64 = 30_000; +const UNMAPPED_IDLE_FRAMES: u64 = 60; + +/// The ranges the title unmapped since the sweep last looked. Recorded on +/// the unmapping thread in O(1); the sweep drains and walks them. Bounded: +/// with the sweep off (`VICTORY_TEXTURE_SWEEP=0`) entries age out oldest +/// first rather than growing without end. +static UNMAPPED: std::sync::Mutex<Vec<(u64, u64)>> = std::sync::Mutex::new(Vec::new()); +const UNMAPPED_RANGES_CAP: usize = 4096; + +/// The title unmapped `[at, at + len)`: placed host copies whose bytes live +/// in that range have lost their source. O(1) -- the walk is the sweep's. +pub(crate) fn unmapped(at: u64, len: u64) { + if len == 0 { + return; + } + let mut ranges = UNMAPPED.lock().unwrap_or_else(|e| e.into_inner()); + push_bounded(&mut ranges, (at, at.saturating_add(len)), UNMAPPED_RANGES_CAP); +} + +/// The title mapped `[at, at + len)` back: recorded ranges under it say the +/// opposite of what is now true, so they go. +pub(crate) fn remapped(at: u64, len: u64) { + if len == 0 { + return; + } + let (start, end) = (at, at.saturating_add(len)); + let mut ranges = UNMAPPED.lock().unwrap_or_else(|e| e.into_inner()); + ranges.retain(|&(r, r_end)| !overlap_any(r, r_end - r, &[(start, end)])); ~~~~ --- This drops an entire recorded interval on any overlap, but `XMemMapPhysicalPages` can remap only part of a previously unmapped range. For example, after unmapping `[0, 64 KiB)`, remapping `[16, 32 KiB)` should leave the prefix and suffix recorded; removing the whole interval makes the next texture sweep miss those still-unmapped pages and retain their placed copies as if the source were mapped. Could you subtract the remapped interval and preserve the remaining pieces, with a middle-range remap test? AI-assisted review; I traced the map notification and sweep bookkeeping.
gut0to commented 2026-12-01 14:43:49 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by gut0to on 2026-12-01T14:43:49Z.

Review: COMMENTED


AI-assisted review; I used an AI assistant to inspect this diff.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6133567539 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1348#pullrequestreview-6133567539). Originally posted by [gut0to](https://github.com/gut0to) on 2026-12-01T14:43:49Z. **Review:** COMMENTED --- AI-assisted review; I used an AI assistant to inspect this diff.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1348:upstream-pr/1348'
git switch 'upstream-pr/1348'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1348'
git switch 'upstream-pr/1348'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1348'
git switch 'upstream-pr/1348'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1348'
git switch 'main'
git merge --squash 'upstream-pr/1348'
git switch 'main'
git merge --ff-only 'upstream-pr/1348'
git switch 'main'
git merge 'upstream-pr/1348'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
trustno1-coder/VIctory-NVIDIA!1348
No description provided.