Region epochs are per 64 KiB block, not per 64 MiB (about 2,600 to 400 block walks a world frame) #179

Open
github-bot wants to merge 3 commits from github-bot/gh-179 into main AGit
Owner

@⁠SPFCChris on GitHub · 2026-11-25 19:33 UTC · original
SPFCChris:perf/block-sized-region-epochs → main · +119 −18 · 2 files · 3 commits · open

Read "Measured" before merging: this cuts the work it aims at more than six-fold, but I could not show a frame-time gain beyond run-to-run noise. It is offered as a small step that removes lock traffic, not as a speed-up I can prove.

Second edit: as first posted this change made stale host copies more frequent (stretched triangles for a few seconds in one run). The third commit fixes the hole behind it, which is in main too, and adds a switch that checks the epochs against the walk: see the comment below for the figures. Do not merge the first two commits without the third.

Edited after posting: the first version quoted the copies per frame figures as logged (12,300 and 2,550 watches a frame). Those are about five times too high, see the note under "Measured"; the figures below are corrected. The ratio and everything else stand.

Problem

Shadow::update_free keeps the ranges it found clean, keyed by watch::region_epoch, and skips writes_guarded (the walk of the range's blocks under GUARDS) while the epoch stands. A region was 64 MiB. The title writes somewhere in every 64 MiB of its buffers between two checks of the same range, so the fast path missed most of the time: in the world, about 2,600 watches a frame over ranges of which 95 % had not changed, the same big buffers checked several times a frame.

Change

A region is one guarded block (64 KiB): REGION_SHIFT 26 becomes 16 by default, and the table grows from 1,024 to 2^20 counters (8 MB of zeroed statics; two regions share a counter only 64 GiB apart, which can only cost a needless walk). VICTORY_EPOCH_SHIFT=26 gives the old size back.

Why it is still sound: every bump already passes the addresses it concerns (open_here, the pinned block in guard, title_memory_remapped, title_memory_written, try_open_guarded), and a block lies in exactly one region, so the regions of a written address are those of its whole block. A range's epoch therefore stands exactly as long as none of its blocks was opened, remapped or rewritten, which is what the walk would find. A write through another view of a page opens the guarded view's block by its own address (open), and bumps there. The shift is clamped to at least 16 for that reason.

One file, crates/victory-gpu/src/watch.rs.

Measured

Radeon 8060S (Ryzen AI Max+ 395), diag build, Series X profile, opening cutscene, 220 s in the world per run. Same binary, VICTORY_EPOCH_SHIFT=26 against the default, runs alternated. Built on a59f3e5 plus #148 (this GPU needs it to draw the world) plus #178.

Counters of copies per frame, per frame, medians over the intervals of 2,500 draws a frame or more, two runs each:

watches their time, all threads ranges checked page faults
64 MiB regions 2,561 and 2,604 36.7 and 40.3 ms 1.6 and 1.7 GB 8,833 and 8,247
64 KiB regions 397 and 370 27.6 and 24.4 ms 1.8 and 1.5 GB 8,353 and 8,668

A note on those counters, which is a small bug of its own: Meter::frame calls copystat::log(frames) once every 5 s with the frames of the last second only, while the counters run since the last line, so the line prints them about five times too high (it showed 12,300 and 2,550 watches a frame here). The table recounts them over the frames of the whole interval, from a per-frame time log. I can send the one-line fix separately if wanted.

Median frame time of the frames in a band of draws per frame:

3,000-4,000 draws 4,000-6,000 draws all frames
64 MiB regions, run 1 200 ms 238 ms 178 ms
64 MiB regions, run 2 216 ms 244 ms 196 ms
64 KiB regions, run 1 208 ms 234 ms 186 ms
64 KiB regions, run 2 183 ms 201 ms 175 ms

One of the two runs is faster than anything before it and the other is not: with single runs differing by this much, that is no proof. The time in watches fell by a third, not by the six-fold of their number (it includes waiting for GUARDS), so the walks this removes were the cheap ones.

Tested

  • cargo test -p victory-gpu --lib --locked: 114 passed, 0 failed, 4 ignored. One is new, a_write_changes_the_epoch_of_its_block_alone: after a write to one block of a guarded view, the ranges holding that block have a new epoch and the block beside it has not (with 64 MiB regions it fails).
  • cargo clippy -p victory-gpu --all-targets --locked: no warning in the changed code.
  • In the title: three runs of about four minutes in the world, no device removal; the frames I looked at (street, car interior, apartment, the couple by the car park) are as before.

Not tested

  • Vehicles in motion, the case the guard over all views of a page was written for (parts left behind and stretched): the cutscene these runs cover has none on screen for long. If a stale copy shows anywhere, VICTORY_EPOCH_SHIFT=26 is the first thing to try.
  • Any other GPU; a build without diagnostics; anything past the opening cutscene.

AI-assisted (Claude Code); no game data.

🤖 Generated with Claude Code

**@⁠SPFCChris** on GitHub · 2026-11-25 19:33 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/179) `SPFCChris:perf/block-sized-region-epochs` → `main` · +119 −18 · 2 files · 3 commits · open **Read "Measured" before merging: this cuts the work it aims at more than six-fold, but I could not show a frame-time gain beyond run-to-run noise.** It is offered as a small step that removes lock traffic, not as a speed-up I can prove. **Second edit: as first posted this change made stale host copies more frequent (stretched triangles for a few seconds in one run). The third commit fixes the hole behind it, which is in `main` too, and adds a switch that checks the epochs against the walk: see the comment below for the figures. Do not merge the first two commits without the third.** *Edited after posting: the first version quoted the `copies per frame` figures as logged (12,300 and 2,550 watches a frame). Those are about five times too high, see the note under "Measured"; the figures below are corrected. The ratio and everything else stand.* ## Problem `Shadow::update_free` keeps the ranges it found clean, keyed by `watch::region_epoch`, and skips `writes_guarded` (the walk of the range's blocks under `GUARDS`) while the epoch stands. A region was 64 MiB. The title writes somewhere in every 64 MiB of its buffers between two checks of the same range, so the fast path missed most of the time: in the world, about **2,600 watches a frame over ranges of which 95 % had not changed**, the same big buffers checked several times a frame. ## Change A region is one guarded block (64 KiB): `REGION_SHIFT` 26 becomes 16 by default, and the table grows from 1,024 to 2^20 counters (8 MB of zeroed statics; two regions share a counter only 64 GiB apart, which can only cost a needless walk). `VICTORY_EPOCH_SHIFT=26` gives the old size back. Why it is still sound: every bump already passes the addresses it concerns (`open_here`, the pinned block in `guard`, `title_memory_remapped`, `title_memory_written`, `try_open_guarded`), and a block lies in exactly one region, so the regions of a written address are those of its whole block. A range's epoch therefore stands exactly as long as none of its blocks was opened, remapped or rewritten, which is what the walk would find. A write through another view of a page opens the guarded view's block by its own address (`open`), and bumps there. The shift is clamped to at least 16 for that reason. One file, `crates/victory-gpu/src/watch.rs`. ## Measured Radeon 8060S (Ryzen AI Max+ 395), diag build, Series X profile, opening cutscene, 220 s in the world per run. Same binary, `VICTORY_EPOCH_SHIFT=26` against the default, runs alternated. Built on `a59f3e5` plus #148 (this GPU needs it to draw the world) plus #178. Counters of `copies per frame`, per frame, medians over the intervals of 2,500 draws a frame or more, two runs each: | | watches | their time, all threads | ranges checked | page faults | |---|---|---|---|---| | 64 MiB regions | 2,561 and 2,604 | 36.7 and 40.3 ms | 1.6 and 1.7 GB | 8,833 and 8,247 | | 64 KiB regions | 397 and 370 | 27.6 and 24.4 ms | 1.8 and 1.5 GB | 8,353 and 8,668 | A note on those counters, which is a small bug of its own: `Meter::frame` calls `copystat::log(frames)` once every 5 s with the frames of the last second only, while the counters run since the last line, so the line prints them about five times too high (it showed 12,300 and 2,550 watches a frame here). The table recounts them over the frames of the whole interval, from a per-frame time log. I can send the one-line fix separately if wanted. Median frame time of the frames in a band of draws per frame: | | 3,000-4,000 draws | 4,000-6,000 draws | all frames | |---|---|---|---| | 64 MiB regions, run 1 | 200 ms | 238 ms | 178 ms | | 64 MiB regions, run 2 | 216 ms | 244 ms | 196 ms | | 64 KiB regions, run 1 | 208 ms | 234 ms | 186 ms | | 64 KiB regions, run 2 | 183 ms | 201 ms | 175 ms | One of the two runs is faster than anything before it and the other is not: with single runs differing by this much, that is no proof. The time in watches fell by a third, not by the six-fold of their number (it includes waiting for `GUARDS`), so the walks this removes were the cheap ones. ## Tested - `cargo test -p victory-gpu --lib --locked`: 114 passed, 0 failed, 4 ignored. One is new, `a_write_changes_the_epoch_of_its_block_alone`: after a write to one block of a guarded view, the ranges holding that block have a new epoch and the block beside it has not (with 64 MiB regions it fails). - `cargo clippy -p victory-gpu --all-targets --locked`: no warning in the changed code. - In the title: three runs of about four minutes in the world, no device removal; the frames I looked at (street, car interior, apartment, the couple by the car park) are as before. ## Not tested - **Vehicles in motion**, the case the guard over all views of a page was written for (parts left behind and stretched): the cutscene these runs cover has none on screen for long. If a stale copy shows anywhere, `VICTORY_EPOCH_SHIFT=26` is the first thing to try. - Any other GPU; a build without `diagnostics`; anything past the opening cutscene. AI-assisted (Claude Code); no game data. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Shadow::update_free keeps the ranges it found clean by region_epoch, and
skips the walk of their blocks while the epoch stands. A region was 64 MiB,
and the title writes somewhere in every 64 MiB of its buffers between two
checks of the same range, so most first checks of a range walked all of it
under the guards' lock: about 12,300 walks a world frame over ranges of
which 96 % had not changed.

A region is one guarded block now. Every bump already gives the addresses
it concerns, and a block lies in one region, so a range is still known
clean exactly as long as none of its blocks was opened, remapped or
rewritten. VICTORY_EPOCH_SHIFT=26 is the old size.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The `copies per frame` line divides five seconds' counters by the last
second's frames, so it printed them about five times too high; the comment
quoted it.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
`title_memory_remapped` bumped the region epochs only when it found guarded
blocks or noted holes in the range. A walk that met an unmapped block with
`check_each` (`guard`'s first-encounter path) leaves it unnoted, so memory
mapped there later was neither guarded nor a reason to bump: a range holding
it stayed in `clean_spans`, `Shadow::update_free` skipped the walk, and what
the title wrote into the new memory never reached the host copy.

With 64 MiB regions other writes in the region mostly bumped it anyway; with
block-sized regions nothing does. Counted with the new diagnostics switch
`VICTORY_EPOCH_CHECK=1`, which runs the walk although the epochs call the
range clean and reports blocks it finds written while the epoch stood, over
about four minutes in the world on a Radeon 8060S:

  64 MiB regions:           20 of   524,000 clean ranges were not
  64 KiB regions:          148 of 2,097,000
  64 KiB, with this change:  0 of 2,097,000

A run with 64 KiB regions before this change showed stretched triangles
across the screen for some seconds.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:21 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-25 20:04 UTC · original

Independent verification on PR head 01709d0 (Bazzite Linux 45 x86_64, Rust 1.98.1, Intel Core i9-11900K, GE-Proton 10-34 / Wine 10.0):

  • Non-diagnostics build (noted as untested in the PR): cargo test -p victory-gpu --lib --no-run --locked --offline --target x86_64-pc-windows-gnu and cargo clippy -p victory-gpu --all-targets --locked --offline --target x86_64-pc-windows-gnu passed cleanly with 0 new warnings in crates/victory-gpu/src/watch.rs.
  • Unit tests under Wine (--test-threads=1):
    • Default (VICTORY_EPOCH_SHIFT unset → 16 / 64 KiB regions): watch::tests 8 passed, 0 failed (including a_write_changes_the_epoch_of_its_block_alone).
    • VICTORY_EPOCH_SHIFT=26 (64 MiB regions): a_write_changes_the_epoch_of_its_block_alone fails as expected (the block beside it: left 2, right 0).
  • Code audit:
    • Changing REGION_EPOCHS initial value from 1 to 0 keeps the 8 MiB (1 << 20 × AtomicU64) static in .bss rather than bloating the binary, and Shadow::clean_spans (shadow.rs:720-775) checks HashMap::get(&(from, to)) == Some(&ge) with no zero sentinel, so 0 is safe.
    • Note on large spans: for a very large range (e.g. a 174 MiB buffer), region_epoch now scans ~2,784 Acquire loads across 64 KiB buckets (vs 3 at shift=26) — still far cheaper than taking GUARDS, which matches the measured reduction in walks.

(No in-game vehicle/world run performed here; this verifies the non-diagnostics build, clippy, Wine unit tests, and static/epoch review.)

**@⁠KernelSmoker** on GitHub · 2026-11-25 20:04 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/179#issuecomment-7173165489) Independent verification on PR head `01709d0` (Bazzite Linux 45 x86_64, Rust 1.98.1, Intel Core i9-11900K, GE-Proton 10-34 / Wine 10.0): - **Non-`diagnostics` build (noted as untested in the PR):** `cargo test -p victory-gpu --lib --no-run --locked --offline --target x86_64-pc-windows-gnu` and `cargo clippy -p victory-gpu --all-targets --locked --offline --target x86_64-pc-windows-gnu` passed cleanly with **0 new warnings** in `crates/victory-gpu/src/watch.rs`. - **Unit tests under Wine (`--test-threads=1`):** - Default (`VICTORY_EPOCH_SHIFT` unset → 16 / 64 KiB regions): `watch::tests` **8 passed, 0 failed** (including `a_write_changes_the_epoch_of_its_block_alone`). - `VICTORY_EPOCH_SHIFT=26` (64 MiB regions): `a_write_changes_the_epoch_of_its_block_alone` fails as expected (`the block beside it: left 2, right 0`). - **Code audit:** - Changing `REGION_EPOCHS` initial value from `1` to `0` keeps the 8 MiB (`1 << 20` × `AtomicU64`) static in `.bss` rather than bloating the binary, and `Shadow::clean_spans` (`shadow.rs:720-775`) checks `HashMap::get(&(from, to)) == Some(&ge)` with no zero sentinel, so `0` is safe. - Note on large spans: for a very large range (e.g. a 174 MiB buffer), `region_epoch` now scans ~2,784 `Acquire` loads across 64 KiB buckets (vs 3 at shift=26) — still far cheaper than taking `GUARDS`, which matches the measured reduction in walks. (No in-game vehicle/world run performed here; this verifies the non-`diagnostics` build, clippy, Wine unit tests, and static/epoch review.)
Author
Owner

@⁠SPFCChris on GitHub · 2026-11-25 22:49 UTC · original

Do not merge this as it stood before commit "Memory mapped anew bumps its regions' epochs…": by itself the change made stale host copies more frequent. I have pushed the fix and a way to check it.

What happened. A run with block-sized regions showed stretched triangles across the screen for a few seconds. To test the epochs directly I added a diagnostics switch, VICTORY_EPOCH_CHECK=1: where Shadow::update_free finds a range in clean_spans at the current epoch, it runs the walk all the same and reports ranges in which the walk finds a block written although the epoch stood. Over about four minutes in the world (Radeon 8060S, opening cutscene):

clean ranges checked of them not clean
64 MiB regions (VICTORY_EPOCH_SHIFT=26, i.e. main's behaviour) 524,000 20
64 KiB regions (this PR before the fix) 2,097,000 148
64 KiB regions with the fix 2,097,000 0

So the hole is in main too, and this PR made it about seven times more frequent, because with 64 MiB regions some other write in the region usually bumps the epoch by accident.

The hole. title_memory_remapped bumped the epochs only if changed, that is when it found guarded blocks or noted holes in the range. But guard's first-encounter path skips a block that is unmapped (check_each) without noting it as a hole. When the title maps memory there later, nothing is guarded and nothing is noted, so no bump: a range holding the block stays known clean, the walk is skipped, and what the title writes into the new memory (no fault, it was never made read-only) does not reach the host copy.

The fix bumps the epochs of a remapped range unconditionally (watch.rs, three lines). The check stays in as a diagnostics switch (shadow.rs); it costs nothing unless set.

Still true: I cannot show a frame-time gain for this PR beyond noise, so its case is no stronger than before. The three-line fix is worth having on its own, with or without the smaller regions; say if you would rather have it as a separate PR.

AI-assisted (Claude Code); no game data.

**@⁠SPFCChris** on GitHub · 2026-11-25 22:49 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/179#issuecomment-7176837488) **Do not merge this as it stood before commit "Memory mapped anew bumps its regions' epochs…": by itself the change made stale host copies more frequent.** I have pushed the fix and a way to check it. What happened. A run with block-sized regions showed stretched triangles across the screen for a few seconds. To test the epochs directly I added a diagnostics switch, `VICTORY_EPOCH_CHECK=1`: where `Shadow::update_free` finds a range in `clean_spans` at the current epoch, it runs the walk all the same and reports ranges in which the walk finds a block written although the epoch stood. Over about four minutes in the world (Radeon 8060S, opening cutscene): | | clean ranges checked | of them not clean | |---|---|---| | 64 MiB regions (`VICTORY_EPOCH_SHIFT=26`, i.e. `main`'s behaviour) | 524,000 | 20 | | 64 KiB regions (this PR before the fix) | 2,097,000 | 148 | | 64 KiB regions with the fix | 2,097,000 | 0 | So the hole is in `main` too, and this PR made it about seven times more frequent, because with 64 MiB regions some other write in the region usually bumps the epoch by accident. The hole. `title_memory_remapped` bumped the epochs only `if changed`, that is when it found guarded blocks or noted holes in the range. But `guard`'s first-encounter path skips a block that is unmapped (`check_each`) without noting it as a hole. When the title maps memory there later, nothing is guarded and nothing is noted, so no bump: a range holding the block stays known clean, the walk is skipped, and what the title writes into the new memory (no fault, it was never made read-only) does not reach the host copy. The fix bumps the epochs of a remapped range unconditionally (`watch.rs`, three lines). The check stays in as a diagnostics switch (`shadow.rs`); it costs nothing unless set. Still true: I cannot show a frame-time gain for this PR beyond noise, so its case is no stronger than before. The three-line fix is worth having on its own, with or without the smaller regions; say if you would rather have it as a separate PR. AI-assisted (Claude Code); no game data.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 19:40 UTC · original

Tested natively on Windows 11 26300 with an RX 7900 XT (RDNA3, driver 32.0.32029.2010), MSVC and Rust 1.98.1. Head is 847dcbb. I compared it with base 000ffdd and with main 4f62c2a; the PR merges into main cleanly. I could not run VICTORY_EPOCH_CHECK=1 in the game because the title is not on this machine. So I checked the hole that commit 3 fixes with a unit test instead.

Remap hole, local-only test (not in the PR):

  • The walk starts in an unmapped block, so guard takes the check_each path. The range is [unmapped][view][unmapped], so the third block is skipped and not noted as a hole.
  • Note region_epoch for the range. Map a view into the third block, call title_memory_view and then title_memory_remapped, and write into the block. There is no fault, because the block was never made read-only.
  • Check that region_epoch changed.

Results:

  • 000ffdd (same watch.rs/shadow.rs as main, 64 MiB regions): fails 5/5, the epoch stays the same.
  • 2569aee (first two commits only): fails 5/5. It also fails with VICTORY_EPOCH_SHIFT=26.
  • 847dcbb: passes 5/5. It also passes with VICTORY_EPOCH_SHIFT=26.

So the hole can be reproduced without the game, and the third commit closes it at both region sizes. Commit 3 has no test of its own, and the existing tests pass without it. I can share this test (about 50 lines, Windows-only like the rest of watch::tests) if you want it in the PR.

Other results:

  • a_write_changes_the_epoch_of_its_block_alone copied onto 000ffdd fails 3/3 ("the block beside it", left 3, right 1). It passes at head.
  • Full cargo test -p victory-gpu --lib, 70 interleaved runs each of base, head, main, and head merged on main:
    • Runs with a failing watch::tests test: 10, 13, 13 and 13 of 70.
    • The same four watch tests also fail on main.
    • The new test passed in all 137 runs that reached it. 3 runs (1 head, 2 merged) ended early without a summary, as 1 base run did.
    • Other jobs were running on the machine at the same time.
  • cargo clippy -p victory-gpu --all-targets --features diagnostics (MSVC): no warning in the changed code.

Not tested: frame time or watch counts in game, a release build, and other GPUs.

**@⁠adrianmjim** on GitHub · 2026-11-26 19:40 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/179#issuecomment-7201847585) Tested natively on Windows 11 26300 with an RX 7900 XT (RDNA3, driver 32.0.32029.2010), MSVC and Rust 1.98.1. Head is 847dcbb. I compared it with base 000ffdd and with main 4f62c2a; the PR merges into main cleanly. I could not run `VICTORY_EPOCH_CHECK=1` in the game because the title is not on this machine. So I checked the hole that commit 3 fixes with a unit test instead. Remap hole, local-only test (not in the PR): - The walk starts in an unmapped block, so `guard` takes the `check_each` path. The range is [unmapped][view][unmapped], so the third block is skipped and not noted as a hole. - Note `region_epoch` for the range. Map a view into the third block, call `title_memory_view` and then `title_memory_remapped`, and write into the block. There is no fault, because the block was never made read-only. - Check that `region_epoch` changed. Results: - 000ffdd (same `watch.rs`/`shadow.rs` as main, 64 MiB regions): fails 5/5, the epoch stays the same. - 2569aee (first two commits only): fails 5/5. It also fails with `VICTORY_EPOCH_SHIFT=26`. - 847dcbb: passes 5/5. It also passes with `VICTORY_EPOCH_SHIFT=26`. So the hole can be reproduced without the game, and the third commit closes it at both region sizes. Commit 3 has no test of its own, and the existing tests pass without it. I can share this test (about 50 lines, Windows-only like the rest of `watch::tests`) if you want it in the PR. Other results: - `a_write_changes_the_epoch_of_its_block_alone` copied onto 000ffdd fails 3/3 ("the block beside it", left 3, right 1). It passes at head. - Full `cargo test -p victory-gpu --lib`, 70 interleaved runs each of base, head, main, and head merged on main: - Runs with a failing `watch::tests` test: 10, 13, 13 and 13 of 70. - The same four watch tests also fail on main. - The new test passed in all 137 runs that reached it. 3 runs (1 head, 2 merged) ended early without a summary, as 1 base run did. - Other jobs were running on the machine at the same time. - `cargo clippy -p victory-gpu --all-targets --features diagnostics` (MSVC): no warning in the changed code. Not tested: frame time or watch counts in game, a release build, and other GPUs.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/179/head:github-bot/gh-179'
git switch 'github-bot/gh-179'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-179'
git switch 'github-bot/gh-179'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-179'
git switch 'github-bot/gh-179'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-179'
git switch 'main'
git merge --squash 'github-bot/gh-179'
git switch 'main'
git merge --ff-only 'github-bot/gh-179'
git switch 'main'
git merge 'github-bot/gh-179'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.