perf(watch): lock-free publish via CLEAN/HELD bits, batched cover_run, and zero-VirtualProtect view/page invalidations #3945

Closed
github-bot wants to merge 2 commits from github-bot/gh-3945 into main AGit
Owner

@⁠qlfu on GitHub · 2026-11-30 12:36 UTC · original
qlfu:pr/perf-watch-lockfree-publish-cover → main · +165 −63 · 1 files · 2 commits · closed without merging

Summary

Reduces GUARDS mutex contention and redundant VirtualProtect syscalls in crates/victory-gpu/src/watch.rs:

  1. Lock-free publish(): mirrors Guards::clean and Guards::held into atomic CLEAN / HELD Bits (all SeqCst) updated strictly after cover() completes and cleared ahead of PUBLISHED.clear. publish() uses a SeqCst Dekker recheck instead of acquiring GUARDS (~184 fewer GUARDS mutex acquisitions/frame).
  2. Zero-VirtualProtect invalidations on views_mapped() and title_memory_written(pages == true): clears clean and PUBLISHED on already-guarded blocks (and aliases) while keeping existing views PAGE_READONLY, avoiding RW/RO VirtualProtect churn while still forcing refresh and cover() on newly mapped views.
  3. Batched cover_run & single-lock guard(): coalesces contiguous g.protected && !g.clean blocks in guard() into one cover(from, to) (VIEWS.read()) call, avoids double-locking GUARDS on !PROTECTED entry, and skips g.alias() in protect_alias() when an unprotected alias is resting or left alone.
  4. Correctness fix in open_unlocked(): computes took = done && old != PAGE_READWRITE after restoring non-RW/RO protections (done = false), preventing failed VirtualProtect calls from clearing g.protected in settle().

Verification

  • Unit tests: cargo test -p victory-gpu --lib --locked -- --test-threads=1 watch:: (67 passed, 0 failed) + cargo check -p victory-gpu --all-targets --all-features --locked (exit 0).
  • In-game (90 s 1440p DLSS Balanced & FG 2x, RTX 4070 Ti + R7 5700X): refreshes 10.50 -> 9.45 ms med (11.13 -> 9.88 ms mean), prep down to 22.8 ms, copies down to 11.1 ms, 0 WARNs / 0 ERRORs.
**@⁠qlfu** on GitHub · 2026-11-30 12:36 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3945) `qlfu:pr/perf-watch-lockfree-publish-cover` → `main` · +165 −63 · 1 files · 2 commits · closed without merging ## Summary Reduces `GUARDS` mutex contention and redundant `VirtualProtect` syscalls in `crates/victory-gpu/src/watch.rs`: 1. **Lock-free `publish()`:** mirrors `Guards::clean` and `Guards::held` into atomic `CLEAN` / `HELD` `Bits` (all `SeqCst`) updated strictly after `cover()` completes and cleared ahead of `PUBLISHED.clear`. `publish()` uses a `SeqCst` Dekker recheck instead of acquiring `GUARDS` (~184 fewer `GUARDS` mutex acquisitions/frame). 2. **Zero-`VirtualProtect` invalidations on `views_mapped()` and `title_memory_written(pages == true)`:** clears `clean` and `PUBLISHED` on already-guarded blocks (and aliases) while keeping existing views `PAGE_READONLY`, avoiding RW/RO `VirtualProtect` churn while still forcing refresh and `cover()` on newly mapped views. 3. **Batched `cover_run` & single-lock `guard()`:** coalesces contiguous `g.protected && !g.clean` blocks in `guard()` into one `cover(from, to)` (`VIEWS.read()`) call, avoids double-locking `GUARDS` on `!PROTECTED` entry, and skips `g.alias()` in `protect_alias()` when an unprotected alias is resting or left alone. 4. **Correctness fix in `open_unlocked()`:** computes `took = done && old != PAGE_READWRITE` after restoring non-RW/RO protections (`done = false`), preventing failed `VirtualProtect` calls from clearing `g.protected` in `settle()`. ## Verification - Unit tests: `cargo test -p victory-gpu --lib --locked -- --test-threads=1 watch::` (67 passed, 0 failed) + `cargo check -p victory-gpu --all-targets --all-features --locked` (exit 0). - In-game (90 s 1440p DLSS Balanced & FG 2x, RTX 4070 Ti + R7 5700X): `refreshes` `10.50 -> 9.45 ms` med (`11.13 -> 9.88 ms` mean), `prep` down to `22.8 ms`, `copies` down to `11.1 ms`, `0` WARNs / `0` ERRORs.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 12:40:23 +00:00
Author
Owner

@⁠qlfu on GitHub · 2026-11-30 15:05 UTC · original · edited 2026-11-30 15:27 UTC

Added word-batched Bits::set_word for Guards::mark_clean so marking contiguous clean blocks emits one SeqCst fetch_or per 64-block word instead of per block. Verified: cargo check -p victory-gpu --all-targets --all-features --locked + watch:: unit tests passed + clean merge-tree onto 41adb551.

**@⁠qlfu** on GitHub · 2026-11-30 15:05 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3945#issuecomment-7282408363) · edited 2026-11-30 15:27 UTC Added word-batched `Bits::set_word` for `Guards::mark_clean` so marking contiguous clean blocks emits one `SeqCst fetch_or` per 64-block word instead of per block. Verified: `cargo check -p victory-gpu --all-targets --all-features --locked` + `watch::` unit tests passed + clean `merge-tree` onto `41adb551`.
Author
Owner

@⁠upipibi on GitHub · 2026-12-01 02:43 UTC · original

Race review of #3945 (head 701519a4): it holds. I found no lost write and no crash. Its open_unlocked change also fixes something main still has.

Reading.

  • publish() without GUARDS: it sets PUBLISHED, then checks CLEAN/HELD/BESIDE/unsettled again (all SeqCst). Every path that takes a block out of g.clean or g.held goes through unmark_clean/unmark_held, and where the block may have been published it clears PUBLISHED after the mirror. The exception is cover() (:3313), which un-cleans only blocks noted clean in the same locked pass, before mark_clean, as on main. So one of the two sides always sees the other. CLEAN is only set by mark_clean, which reads g.clean after cover() (protect), so it never runs ahead of the books.
  • took = done && old != PAGE_READWRITE in open_unlocked: on main, a VirtualProtect that fails leaves took = true. settle then takes a block that is still read-only out of g.protected. A write there finds nothing to open, and the handler gives up with 0xc0000005. That is the state I traced in #3760, by another way in. This PR closes that way.

Stress test. This is the test from my #3760 review: one 16 MiB view, four threads guarding it in a loop, writing() on another thread, 48 writer threads for 20 s. Debug test build, Ryzen 9 5950X, Windows 11, alternating runs. The PR was tested at its base, since it conflicts with main now.

tree runs crashed worst single title write
base 41adb551 6 0 35–99 ms
this PR 6 0 67–138 ms

watch:: on the PR, 3 runs: 65 passed, 0 failed twice. Once it was 64 passed, 1 failed: ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour, a test-isolation flake on main that #4620 fixes. The worst-wait ranges overlap, so I wouldn't read much into them.

It conflicts with main in watch.rs, so it needs a rebase before it can go in. The took line alone would also make a small fix by itself.

Tested with the help of Claude Code (Claude Opus 5.5).

**@⁠upipibi** on GitHub · 2026-12-01 02:43 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3945#issuecomment-7298923148) Race review of #3945 (head 701519a4): **it holds. I found no lost write and no crash. Its `open_unlocked` change also fixes something `main` still has.** **Reading.** - `publish()` without `GUARDS`: it sets `PUBLISHED`, then checks `CLEAN`/`HELD`/`BESIDE`/`unsettled` again (all `SeqCst`). Every path that takes a block out of `g.clean` or `g.held` goes through `unmark_clean`/`unmark_held`, and where the block may have been published it clears `PUBLISHED` after the mirror. The exception is `cover()` (:3313), which un-cleans only blocks noted clean in the same locked pass, before `mark_clean`, as on `main`. So one of the two sides always sees the other. `CLEAN` is only set by `mark_clean`, which reads `g.clean` after `cover()` (`protect`), so it never runs ahead of the books. - `took = done && old != PAGE_READWRITE` in `open_unlocked`: on `main`, a `VirtualProtect` that fails leaves `took = true`. `settle` then takes a block that is still read-only out of `g.protected`. A write there finds nothing to open, and the handler gives up with 0xc0000005. That is the state I traced in #3760, by another way in. This PR closes that way. **Stress test.** This is the test from my #3760 review: one 16 MiB view, four threads guarding it in a loop, `writing()` on another thread, 48 writer threads for 20 s. Debug test build, Ryzen 9 5950X, Windows 11, alternating runs. The PR was tested at its base, since it conflicts with `main` now. | tree | runs | crashed | worst single title write | |---|---|---|---| | base 41adb551 | 6 | 0 | 35–99 ms | | this PR | 6 | 0 | 67–138 ms | `watch::` on the PR, 3 runs: 65 passed, 0 failed twice. Once it was 64 passed, 1 failed: `ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour`, a test-isolation flake on `main` that #4620 fixes. The worst-wait ranges overlap, so I wouldn't read much into them. It conflicts with `main` in `watch.rs`, so it needs a rebase before it can go in. The `took` line alone would also make a small fix by itself. *Tested with the help of Claude Code (Claude Opus 5.5).*
github-bot closed this pull request 2026-12-01 18:50:57 +00:00
Author
Owner

@⁠trustno1-coder on GitHub · 2026-12-01 18:47 UTC · original

Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

**@⁠trustno1-coder** on GitHub · 2026-12-01 18:47 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3945#issuecomment-7321557711) Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

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