[pr-performance][memory] watch,inplace,xmem: batch view guards, shard LAZY map, and pre-arm overflow aliases asynchronously #219

Open
hingeowl wants to merge 2 commits from hingeowl/Darner:pr/batched-view-guards into main
Contributor

Re-submitted from the previous CodeFloe repository (original commit 5315f0a8d09a0f71cd1ff1b638981eef2525aad7, preserving original authorship by Ethan Anderson) so it is not lost after the forge move.

What it changes

  • Keeps soft copy views (map_lazy) from unguarding live aliased views via title_memory_soft_view, unguarding physical aliases only when a soft view materializes (title_memory_made_real).
  • Shards xmem's LAZY map 64 ways with cache-line-aligned occupancy bits.
  • Caches uncommitted placeholder holes in protect_here and coalesces contiguous VirtualProtect calls in unguard.
  • Adds coalesced watch::guard_ahead + Journal::guard_ahead + batched cover_many before wide_each_scoped in Batch::refresh_front_spans_with, using inplace::view_span instead of repeated VirtualQuery loops.
  • Budgets synchronous alias VirtualProtect calls per guard pass (default 24 via DARNER_GUARD_ASYNC_BUDGET) and pre-arms overflow aliases on a dedicated darner-guard-bg thread paused during ask_once and wide_each_scoped.

Notes

  • Rebased cleanly onto main (f3840d7).
  • Submitted primarily to preserve the patch on GTA Hub for review/testing; standalone A/B on current main has not been run yet.
Re-submitted from the previous CodeFloe repository (original commit `5315f0a8d09a0f71cd1ff1b638981eef2525aad7`, preserving original authorship by Ethan Anderson) so it is not lost after the forge move. ### What it changes - Keeps soft copy views (`map_lazy`) from unguarding live aliased views via `title_memory_soft_view`, unguarding physical aliases only when a soft view materializes (`title_memory_made_real`). - Shards `xmem`'s `LAZY` map 64 ways with cache-line-aligned occupancy bits. - Caches uncommitted placeholder holes in `protect_here` and coalesces contiguous `VirtualProtect` calls in `unguard`. - Adds coalesced `watch::guard_ahead` + `Journal::guard_ahead` + batched `cover_many` before `wide_each_scoped` in `Batch::refresh_front_spans_with`, using `inplace::view_span` instead of repeated `VirtualQuery` loops. - Budgets synchronous alias `VirtualProtect` calls per guard pass (default 24 via `DARNER_GUARD_ASYNC_BUDGET`) and pre-arms overflow aliases on a dedicated `darner-guard-bg` thread paused during `ask_once` and `wide_each_scoped`. ### Notes - Rebased cleanly onto `main` (`f3840d7`). - Submitted primarily to preserve the patch on GTA Hub for review/testing; standalone A/B on current `main` has not been run yet.
Cutscene camera cuts and streaming map/unmap bursts caused multi-thread
view-guard lock convoys and synchronous VirtualProtect TLB shootdown
storms on aliased physical pages.

- Keep soft copy views (map_lazy) from unguarding live aliased views via
  title_memory_soft_view, unguarding physical aliases only when a soft
  view actually materializes (title_memory_made_real).
- Shard xmem's LAZY map 64 ways with cache-line-aligned occupancy bits.
- Cache uncommitted placeholder holes in protect_here and coalesce
  contiguous VirtualProtect calls in unguard.
- Add coalesced watch::guard_ahead + Journal::guard_ahead + batched
  cover_many before wide_each_scoped in Batch::refresh_front_spans_with,
  using inplace::view_span instead of repeated VirtualQuery loops.
- Budget synchronous alias VirtualProtect calls per guard pass (default
  24 via DARNER_GUARD_ASYNC_BUDGET) and pre-arm overflow aliases on a
  dedicated darner-guard-bg thread paused during ask_once and
  wide_each_scoped (pause_async_guard). Primary blocks with deferred
  aliases stay strictly unclean/unpublished until aliases are armed.

(cherry picked from commit 5315f0a8d09a0f71cd1ff1b638981eef2525aad7)
First-time contributor

CHANGES NEEDED

Review of d8325feab.

async_guard_worker can run before ASYNC_GUARD.get_or_init publishes its value: the initializer spawns the thread at crates/darner-gpu/src/watch.rs:4635, while the worker returns immediately when ASYNC_GUARD.get() is still None at lines 4657-4661. If scheduled in that window, the worker exits permanently, so queued overflow aliases are never pre-armed. Pass the initialized state to the worker or start it only after publication, and cover the startup ordering with a test.

Windows validation: G1 PASS; G2 PASS; cargo xtask policy PASS (555 files ok); inplace tests — 15 passed, 0 failed; xmem tests — 53 passed, 0 failed, 1 ignored. The shadow::tests selector matched 0 tests.

Not run in-game.

AI-assisted: yes (GitHub Copilot, Auto mode).

**CHANGES NEEDED** Review of `d8325feab`. `async_guard_worker` can run before `ASYNC_GUARD.get_or_init` publishes its value: the initializer spawns the thread at `crates/darner-gpu/src/watch.rs:4635`, while the worker returns immediately when `ASYNC_GUARD.get()` is still `None` at lines 4657-4661. If scheduled in that window, the worker exits permanently, so queued overflow aliases are never pre-armed. Pass the initialized state to the worker or start it only after publication, and cover the startup ordering with a test. Windows validation: G1 PASS; G2 PASS; `cargo xtask policy` PASS (`555 files ok`); `inplace` tests — 15 passed, 0 failed; `xmem` tests — 53 passed, 0 failed, 1 ignored. The `shadow::tests` selector matched 0 tests. Not run in-game. AI-assisted: yes (GitHub Copilot, Auto mode).
First-time contributor

Replay A/B on RTX 5070 Ti: no measurable difference

Measured d8325feab6 merged on top of release v2026.12.03.02, against the plain release, on a ~6.5 minute replay route (same save, recorded controller input) through a cutscene, a scene cut and a stretch of free play.

Build fps (range) CPU prep ms median ms p95 ms p99 ms frames over 100 ms per run
v2026.12.03.02 (3 runs) 38.30 (38.17-38.46) 9.57 26.8 29.8 37.8 20 / 11 / 8
+ PR 219 (2 runs) 38.33 (37.79-38.87) 9.84 27.0 29.9 36.6 10 / 7

Crashes: none in any run (no device loss, no host GPU failure). Fps means are within the release's own run-to-run spread, so: no measurable difference on this route. With only 3 release runs, that spread is not well known yet; differences under ~2-3% can't be resolved here.

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB RAM, Windows 11; scripts\play-nvidia.ps1 -Build release (Series X, 1440p native, DLSS Quality, no frame generation, fullscreen, no fps cap). Each build had one unmeasured warm-up run first. The release base excludes its first measured run, which was still compiling shaders (about 3,300 compiles during the route).

Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.

### Replay A/B on RTX 5070 Ti: no measurable difference Measured `d8325feab6` merged on top of release v2026.12.03.02, against the plain release, on a ~6.5 minute replay route (same save, recorded controller input) through a cutscene, a scene cut and a stretch of free play. | Build | fps (range) | CPU prep ms | median ms | p95 ms | p99 ms | frames over 100 ms per run | |---|---:|---:|---:|---:|---:|---:| | v2026.12.03.02 (3 runs) | 38.30 (38.17-38.46) | 9.57 | 26.8 | 29.8 | 37.8 | 20 / 11 / 8 | | + PR 219 (2 runs) | 38.33 (37.79-38.87) | 9.84 | 27.0 | 29.9 | 36.6 | 10 / 7 | Crashes: none in any run (no device loss, no host GPU failure). Fps means are within the release's own run-to-run spread, so: **no measurable difference** on this route. With only 3 release runs, that spread is not well known yet; differences under ~2-3% can't be resolved here. Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB RAM, Windows 11; `scripts\play-nvidia.ps1 -Build release` (Series X, 1440p native, DLSS Quality, no frame generation, fullscreen, no fps cap). Each build had one unmeasured warm-up run first. The release base excludes its first measured run, which was still compiling shaders (about 3,300 compiles during the route). Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
Author
Contributor

Fixed in b44d45c: enqueue_async_aliases now publishes ASYNC_GUARD via get_or_init first, then spawns darner-guard-bg via ASYNC_GUARD_STARTED: Once and passes state: &'static AsyncGuardState directly into async_guard_worker(state) so the worker cannot observe None and exit on startup. Added unit test async_guard_worker_starts_after_state_publication_and_drains_queue.

Validation on b44d45c:

  • cargo test -p darner-gpu --lib --locked -- async_guard_worker_starts_after_state_publication_and_drains_queue — 1 passed, 0 failed.
  • cargo clippy -p darner-gpu --all-targets --all-features --locked -- -D warnings — passed.

Ready for re-review.

Fixed in `b44d45c`: `enqueue_async_aliases` now publishes `ASYNC_GUARD` via `get_or_init` first, then spawns `darner-guard-bg` via `ASYNC_GUARD_STARTED: Once` and passes `state: &'static AsyncGuardState` directly into `async_guard_worker(state)` so the worker cannot observe `None` and exit on startup. Added unit test `async_guard_worker_starts_after_state_publication_and_drains_queue`. Validation on `b44d45c`: - `cargo test -p darner-gpu --lib --locked -- async_guard_worker_starts_after_state_publication_and_drains_queue` — 1 passed, 0 failed. - `cargo clippy -p darner-gpu --all-targets --all-features --locked -- -D warnings` — passed. Ready for re-review.
Author
Contributor

cam-city ABBA Benchmark on RTX 4090 + 7950X3D: No Improvement (Flat on Clean Run, Regression on B2)

Tested b44d45c against main (f3840d7) in a full ABBA sequence (A1 -> B1 -> B2 -> A2) on the full 172 s cam-city route using bench-kit (with the foot.py pinsfix update), with warm pipelines.

Per-Run Results (all tagged rows, ~176 s)

Run Build Overall FPS Median FPS Median ms p95 ms p99 ms >100 ms prep_ms (med) card_idle_ms (med) card_lists_ms (med) pipe_made
A1 main (f3840d7) 10.44 10.85 92.13 132.17 171.49 40.1% 80.83 59.95 29.70 1
B1 PR #219 (b44d45c) 10.60 11.39 87.77 134.62 166.77 36.2% 77.20 57.17 29.21 0
B2 PR #219 (b44d45c) 9.10 9.77 102.36 145.10 201.88 52.5% 88.05 66.19 30.10 2
A2 main (f3840d7) 10.47 11.45 87.34 138.05 166.42 37.3% 78.98 58.48 28.73 1
Mean A main 10.46 11.15 89.73 135.11 168.95 38.7% 79.91 59.21 29.22 —
Mean B PR #219 9.85 (-5.8%) 10.58 (-5.1%) 95.06 (+5.9%) 139.86 (+3.5%) 184.32 (+9.1%) 44.4% 82.62 (+3.4%) 61.68 (+4.2%) 29.66 (+1.5%) —

Segment Breakdown (Median Frame Time)

  • Segment 1 (fast): A = 110.73, 113.98 ms (mean 112.36 ms) → B = 109.99, 117.50 ms (mean 113.75 ms, +1.2%)
  • Segment 2 (slow): A = 82.33, 81.53 ms (mean 81.93 ms) → B = 81.49, 88.84 ms (mean 85.17 ms, +3.9%)

Summary

  • No measurable gain on cam-city: B1 (pipe_made=0, pipe_wait_ms=0) landed inside main's A1–A2 spread (87.77 ms median / 10.60 overall FPS vs main 87.34–92.13 ms / 10.44–10.47 FPS), while B2 regressed to 102.36 ms median / 9.10 overall FPS (pipe_made=2, 52.2 s cumulative pipe_wait_ms, plus slower prep_ms across both segments).
  • Across the full ABBA, PR #219 averages +5.9% median frame time (-5.8% overall FPS) with notably higher run-to-run variance than main.
  • All 4 runs passed cam-bench.py clean checks (RESULT: camera route flown, clean) with zero crashes or device losses.

Environment

  • Hardware / OS: NVIDIA GeForce RTX 4090 (24 GB, driver 621.14), AMD Ryzen 9 7950X3D, Windows 11 Pro (build 26300).
  • Build: cargo build --release --locked -p darner --features bench-remote.
  • Settings: bench-kit (pinsfix foot.py), cam-city (172 s), Series X profile, HW RT enabled (DARNER_HW_RT=1, DARNER_BVH_BUILDER=1), play.rs DEFAULT_ENV tuning, no frame generation.
### `cam-city` ABBA Benchmark on RTX 4090 + 7950X3D: No Improvement (Flat on Clean Run, Regression on B2) Tested **`b44d45c`** against **`main` (`f3840d7`)** in a full ABBA sequence (`A1 -> B1 -> B2 -> A2`) on the full 172 s `cam-city` route using `bench-kit` (with the `foot.py` pinsfix update), with warm pipelines. #### Per-Run Results (`all tagged rows`, ~176 s) | Run | Build | Overall FPS | Median FPS | Median ms | p95 ms | p99 ms | >100 ms | `prep_ms` (med) | `card_idle_ms` (med) | `card_lists_ms` (med) | `pipe_made` | |---|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:| | A1 | `main` (`f3840d7`) | 10.44 | 10.85 | 92.13 | 132.17 | 171.49 | 40.1% | 80.83 | 59.95 | 29.70 | 1 | | B1 | PR #219 (`b44d45c`) | 10.60 | 11.39 | 87.77 | 134.62 | 166.77 | 36.2% | 77.20 | 57.17 | 29.21 | 0 | | B2 | PR #219 (`b44d45c`) | 9.10 | 9.77 | 102.36 | 145.10 | 201.88 | 52.5% | 88.05 | 66.19 | 30.10 | 2 | | A2 | `main` (`f3840d7`) | 10.47 | 11.45 | 87.34 | 138.05 | 166.42 | 37.3% | 78.98 | 58.48 | 28.73 | 1 | | **Mean A** | **`main`** | **10.46** | **11.15** | **89.73** | **135.11** | **168.95** | **38.7%** | **79.91** | **59.21** | **29.22** | — | | **Mean B** | **PR #219** | **9.85 (-5.8%)** | **10.58 (-5.1%)** | **95.06 (+5.9%)** | **139.86 (+3.5%)** | **184.32 (+9.1%)** | **44.4%** | **82.62 (+3.4%)** | **61.68 (+4.2%)** | **29.66 (+1.5%)** | — | #### Segment Breakdown (Median Frame Time) - **Segment 1 (fast):** A = `110.73, 113.98 ms` (mean `112.36 ms`) → B = `109.99, 117.50 ms` (mean `113.75 ms`, **+1.2%**) - **Segment 2 (slow):** A = `82.33, 81.53 ms` (mean `81.93 ms`) → B = `81.49, 88.84 ms` (mean `85.17 ms`, **+3.9%**) #### Summary - **No measurable gain on `cam-city`:** `B1` (`pipe_made=0`, `pipe_wait_ms=0`) landed inside `main`'s A1–A2 spread (`87.77 ms` median / `10.60` overall FPS vs `main` `87.34–92.13 ms` / `10.44–10.47` FPS), while `B2` regressed to `102.36 ms` median / `9.10` overall FPS (`pipe_made=2`, `52.2 s` cumulative `pipe_wait_ms`, plus slower `prep_ms` across both segments). - Across the full ABBA, PR #219 averages **+5.9% median frame time** (**-5.8% overall FPS**) with notably higher run-to-run variance than `main`. - All 4 runs passed `cam-bench.py` clean checks (`RESULT: camera route flown, clean`) with zero crashes or device losses. #### Environment - **Hardware / OS:** NVIDIA GeForce RTX 4090 (24 GB, driver `621.14`), AMD Ryzen 9 7950X3D, Windows 11 Pro (build `26300`). - **Build:** `cargo build --release --locked -p darner --features bench-remote`. - **Settings:** `bench-kit` (`pinsfix` `foot.py`), `cam-city` (172 s), Series X profile, HW RT enabled (`DARNER_HW_RT=1`, `DARNER_BVH_BUILDER=1`), `play.rs` `DEFAULT_ENV` tuning, no frame generation.
First-time contributor

Re-measured b44d45c on RTX 5070 Ti: no regression on this route

Follow-up to the measurement above (which was on d8325feab6, before the darner-guard-bg start fix). A second card and route next to the 4090 ABBA above: here neither b44d45c run is slower than the release.

b44d45c merged on top of release v2026.12.03.02 (f3840d73), against the plain release, same ~6.5 minute replay route (same save, recorded controller input). The release baseline now has 11 runs from the same day.

Build runs fps (range) CPU prep ms median ms p95 ms p99 ms frames over 100 ms per run
v2026.12.03.02 11 37.99 (37.70-38.26) 9.86 26.9 29.6 35.6 6-11
+ PR 219 b44d45c 2 38.41 (38.35-38.48) 9.24 26.9 28.7 38.0 11 / 8
  • Mean fps +1.1 %, under the ~2-3 % this bench can resolve, so: no measurable difference, and no regression. Both PR runs sit above every release run, and CPU prep is about 0.6 ms lower, which points the same way but needs more runs to confirm.
  • Crashes: none in any run.

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB RAM, Windows 11; scripts\play-nvidia.ps1 -Build release (Series X, 1440p native, DLSS Quality, no frame generation, fullscreen, no fps cap). One unmeasured warm-up run first.

Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.

### Re-measured `b44d45c` on RTX 5070 Ti: no regression on this route Follow-up to the measurement above (which was on `d8325feab6`, before the `darner-guard-bg` start fix). A second card and route next to the 4090 ABBA above: here neither `b44d45c` run is slower than the release. `b44d45c` merged on top of release v2026.12.03.02 (`f3840d73`), against the plain release, same ~6.5 minute replay route (same save, recorded controller input). The release baseline now has 11 runs from the same day. | Build | runs | fps (range) | CPU prep ms | median ms | p95 ms | p99 ms | frames over 100 ms per run | |---|---:|---:|---:|---:|---:|---:|---| | v2026.12.03.02 | 11 | 37.99 (37.70-38.26) | 9.86 | 26.9 | 29.6 | 35.6 | 6-11 | | + PR 219 `b44d45c` | 2 | 38.41 (38.35-38.48) | 9.24 | 26.9 | 28.7 | 38.0 | 11 / 8 | - Mean fps +1.1 %, under the ~2-3 % this bench can resolve, so: **no measurable difference**, and no regression. Both PR runs sit above every release run, and CPU prep is about 0.6 ms lower, which points the same way but needs more runs to confirm. - Crashes: none in any run. Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB RAM, Windows 11; `scripts\play-nvidia.ps1 -Build release` (Series X, 1440p native, DLSS Quality, no frame generation, fullscreen, no fps cap). One unmeasured warm-up run first. Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
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 'pr/batched-view-guards:hingeowl-pr/batched-view-guards'
git switch 'hingeowl-pr/batched-view-guards'

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 'hingeowl-pr/batched-view-guards'
git switch 'hingeowl-pr/batched-view-guards'
git rebase 'main'
git switch 'main'
git merge --ff-only 'hingeowl-pr/batched-view-guards'
git switch 'hingeowl-pr/batched-view-guards'
git rebase 'main'
git switch 'main'
git merge --no-ff 'hingeowl-pr/batched-view-guards'
git switch 'main'
git merge --squash 'hingeowl-pr/batched-view-guards'
git switch 'main'
git merge --ff-only 'hingeowl-pr/batched-view-guards'
git switch 'main'
git merge 'hingeowl-pr/batched-view-guards'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!219
No description provided.