watch: extend WRITTEN_EPOCHS for lock-free journal reads and whole-page notes #3756

Closed
ScymicX wants to merge 3 commits from upstream-pr/3756 into main
ScymicX commented 2026-11-30 06:04:33 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by ScymicX on 2026-11-30T06:04:33Z.


What it changes

main already maintains a 3-level lock-free WRITTEN_EPOCHS (PageEpochs) radix table for
othing_new_with/
othing_new_single, while writes, writes_since, sked, parts_written,
othing_new_all, and landed_beside still read and write Journal::written: BTreeMap<usize, u64> under the global JOURNAL mutex. Across 1,500+ watch::writes calls/frame on worker threads, JOURNAL lock contention and BTreeMap traversal/updates add substantial CPU overhead.

This PR extends the existing WRITTEN_EPOCHS table instead of duplicating a second radix structure (enabled by default; VICTORY_JOURNAL_LOCKFREE=0 falls back to Journal::written under JOURNAL):

  • Extend EpochLeaf with present and has_parts 64-bit bitmasks (lloc_zeroed on the heap + CAS publication), and add
    ote_whole ( etch_max + present bit), set_has_parts / clear_has_parts / ny_has_parts, plus a shared scan_present traversal backing collect_range, ny_since (preserving #2795's zero-allocation writes_since path for texture refreshes), and ll_le.
  • Bypass JOURNAL & BTreeMap on hot paths:
    • writes / writes_since: owned, guarded && published, empty-result, and whole-page note paths read/update WRITTEN_EPOCHS lock-free (only taking journal_mut() if a newly whole-written page previously had has_parts set).
    • sked: reads WRITTEN_EPOCHS.collect_range without holding JOURNAL across the range scan.
    • parts_written: fast-paths to ec![WHOLE; pages.len()] without locking JOURNAL when !parts_on() or !WRITTEN_EPOCHS.any_has_parts(pages).
    • landed_beside: writes whole-page landing updates to WRITTEN_EPOCHS without locking JOURNAL unless has_parts needs clearing.
    • Journal::note /
      ew_in: update/query WRITTEN_EPOCHS directly when sync_epochs is true, avoiding BTreeMap churn even on sk_ahead/sk_once paths.
  • Adds unit test watch::parts_tests::page_epochs_collect_any_since_and_parts_flags and updates the two journal().written test assertions in watch.rs to check WRITTEN_EPOCHS when lock-free mode is active.

How it was tested

  • GPU and profile: NVIDIA GeForce RTX 5070 Ti, Xbox Series X, 1440p, DLSS Balanced, frame generation off ( 2026.11.28.01 baseline with this optimization).
  • Before: Summed across worker threads in watch::writes: journal_mut / publish 6.70 ms/frame, head / published 1.58 ms/frame.
  • After: journal_mut / publish dropped to 1.21 ms/frame, head / published dropped to 0.89 ms/frame (and eliminated BTreeMap updates under JOURNAL).
  • cargo test passed for the crates it touches: yes
    • cargo test --locked -p victory-gpu --lib watch::: 66 passed, 0 failed (default lock-free ON).
    • VICTORY_JOURNAL_LOCKFREE=0 cargo test --locked -p victory-gpu --lib watch:: -- --test-threads=1: 66 passed, 0 failed.
    • cargo clippy -p victory-gpu --no-deps --all-targets --locked and --all-features: 0 warnings in touched/new code.
    • cargo xtask policy --staged: passed.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #1556, #2504, #2795 (already merged zero-alloc writes_since, preserved via ny_since), #3108, #3502. Complementary to our separate PR for unlocked VirtualProtect + batched Bits/publish in watch.rs (#3753 is our separate shadow.rs landing bitmap PR).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5559977028 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756). Originally posted by [ScymicX](https://github.com/ScymicX) on 2026-11-30T06:04:33Z. --- ## What it changes main already maintains a 3-level lock-free WRITTEN_EPOCHS (PageEpochs) radix table for othing_new_with/ othing_new_single, while writes, writes_since, sked, parts_written, othing_new_all, and landed_beside still read and write Journal::written: BTreeMap<usize, u64> under the global JOURNAL mutex. Across 1,500+ watch::writes calls/frame on worker threads, JOURNAL lock contention and BTreeMap traversal/updates add substantial CPU overhead. This PR extends the existing WRITTEN_EPOCHS table instead of duplicating a second radix structure (enabled by default; VICTORY_JOURNAL_LOCKFREE=0 falls back to Journal::written under JOURNAL): - **Extend EpochLeaf** with present and has_parts 64-bit bitmasks (lloc_zeroed on the heap + CAS publication), and add ote_whole ( etch_max + present bit), set_has_parts / clear_has_parts / ny_has_parts, plus a shared scan_present traversal backing collect_range, ny_since (preserving #2795's zero-allocation writes_since path for texture refreshes), and ll_le. - **Bypass JOURNAL & BTreeMap on hot paths**: - writes / writes_since: owned, guarded && published, empty-result, and whole-page note paths read/update WRITTEN_EPOCHS lock-free (only taking journal_mut() if a newly whole-written page previously had has_parts set). - sked: reads WRITTEN_EPOCHS.collect_range without holding JOURNAL across the range scan. - parts_written: fast-paths to ec![WHOLE; pages.len()] without locking JOURNAL when !parts_on() or !WRITTEN_EPOCHS.any_has_parts(pages). - landed_beside: writes whole-page landing updates to WRITTEN_EPOCHS without locking JOURNAL unless has_parts needs clearing. - Journal::note / ew_in: update/query WRITTEN_EPOCHS directly when sync_epochs is true, avoiding BTreeMap churn even on sk_ahead/sk_once paths. - Adds unit test watch::parts_tests::page_epochs_collect_any_since_and_parts_flags and updates the two journal().written test assertions in watch.rs to check WRITTEN_EPOCHS when lock-free mode is active. ## How it was tested - **GPU and profile**: NVIDIA GeForce RTX 5070 Ti, Xbox Series X, 1440p, DLSS Balanced, frame generation off ( 2026.11.28.01 baseline with this optimization). - **Before**: Summed across worker threads in watch::writes: journal_mut / publish **6.70 ms/frame**, head / published **1.58 ms/frame**. - **After**: journal_mut / publish dropped to **1.21 ms/frame**, head / published dropped to **0.89 ms/frame** (and eliminated BTreeMap updates under JOURNAL). - cargo test passed for the crates it touches: **yes** - cargo test --locked -p victory-gpu --lib watch::: **66 passed, 0 failed** (default lock-free ON). - VICTORY_JOURNAL_LOCKFREE=0 cargo test --locked -p victory-gpu --lib watch:: -- --test-threads=1: **66 passed, 0 failed**. - cargo clippy -p victory-gpu --no-deps --all-targets --locked and --all-features: 0 warnings in touched/new code. - cargo xtask policy --staged: passed. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #1556, #2504, #2795 (already merged zero-alloc writes_since, preserved via ny_since), #3108, #3502. Complementary to our separate PR for unlocked VirtualProtect + batched Bits/publish in watch.rs (#3753 is our separate shadow.rs landing bitmap PR).
Dukestar69 commented 2026-11-30 06:08:22 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-30T06:08:22Z.

Review: APPROVED


Tested locally merged onto main (41adb551).

  • Merge: Clean merge in crates/victory-gpu/src/watch.rs.
  • Tests: cargo test -p victory-gpu --lib watch -- --test-threads=1 passed (67 passed; 0 failed, including parts_tests::page_epochs_checks_single_and_multi_page_ranges and page_epochs_collect_any_since_and_parts_flags) with 0 new warnings.

Solid lock-free WRITTEN_EPOCHS extension for journal reads and whole-page notes. LGTM.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6107589965 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#pullrequestreview-6107589965). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-30T06:08:22Z. **Review:** APPROVED --- Tested locally merged onto `main` (`41adb551`). - **Merge**: Clean merge in `crates/victory-gpu/src/watch.rs`. - **Tests**: `cargo test -p victory-gpu --lib watch -- --test-threads=1` passed (`67 passed; 0 failed`, including `parts_tests::page_epochs_checks_single_and_multi_page_ranges` and `page_epochs_collect_any_since_and_parts_flags`) with 0 new warnings. Solid lock-free `WRITTEN_EPOCHS` extension for journal reads and whole-page notes. LGTM. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
ColorC commented 2026-11-30 08:58:51 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by ColorC on 2026-11-30T08:58:51Z.


AI Disclosure: This hardware benchmark, log analysis, and report were automated and posted by an AI assistant on behalf of the hardware owner (Ryzen 7 9800X3D, 32 GB DDR5, RTX 5070 Ti 16 GB, Windows 11 26200). No game screenshots or title assets are included.

Cross-validation on RTX 5070 Ti + Ryzen 7 9800X3D (Series X 1440p, DLSS Quality + 3x MFG), testing #3756 (VICTORY_JOURNAL_LOCKFREE) together with #3760 (VICTORY_WATCH_UNLOCKED_PROTECT) and VICTORY_FAULT_UNLOCKED via same-binary toggle A/B at ~10.3k draws/frame:

Variant (same binary, same save) Draws Avg Native FPS Avg / Med ms CPU Prep Submitting Thread refreshes Errors
Lock-free watch/journal OFF (=0) 10,412 25.82 38.7 / 38.9 ms 29.0 ms 31.5 ms 16.2 ms (28 flushes of 6,052) 0
Lock-free watch/journal ON (=1) 10,266 28.34 (+2.52) 35.3 / 35.6 ms 24.3 ms (-4.7) 26.9 ms (-4.6) 9.6 ms (-6.6 ms!) (27 flushes of 6,030) 0

Notes

  • Combined lock-free watch/journal path cut refreshes by -6.6 ms (16.2 -> 9.6 ms) and gained +2.52 FPS with zero stability regressions over 8+ minutes of open-world walking (both raster and DXR 1.1 HW RT).
  • Merge note: #3756 and #3760 compose cleanly except for a trivial 1-hunk adjacency in watch.rs where journal_lockfree() and unlocked_protect() sit right after parts_on().

Test Setup

  • Hardware: AMD Ryzen 7 9800X3D (Zen 5, 8C/16T), 32 GB DDR5, NVIDIA RTX 5070 Ti 16 GB (ReBAR ON), Windows 11 26200
  • Profile: Series X, 2560x1440, DLSS Quality, 3x MFG (--res 1440 --dlss quality --framegen 3 --fps 1000)
  • Build: Release ThinLTO (-C target-cpu=znver5) rebased on main (41adb551) with a curated local PR stack; all A/B numbers below are same-binary toggle ablations from the exact same restored save (~10.0k–10.4k draws/frame standing scene) after official 1-time warmup.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7273584570 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#issuecomment-7273584570). Originally posted by [ColorC](https://github.com/ColorC) on 2026-11-30T08:58:51Z. --- > **AI Disclosure:** This hardware benchmark, log analysis, and report were automated and posted by an AI assistant on behalf of the hardware owner (Ryzen 7 9800X3D, 32 GB DDR5, RTX 5070 Ti 16 GB, Windows 11 26200). No game screenshots or title assets are included. Cross-validation on **RTX 5070 Ti + Ryzen 7 9800X3D** (Series X 1440p, DLSS Quality + 3x MFG), testing `#3756` (`VICTORY_JOURNAL_LOCKFREE`) together with `#3760` (`VICTORY_WATCH_UNLOCKED_PROTECT`) and `VICTORY_FAULT_UNLOCKED` via same-binary toggle A/B at ~10.3k draws/frame: | Variant (same binary, same save) | Draws | Avg Native FPS | Avg / Med ms | CPU Prep | Submitting Thread | `refreshes` | Errors | |---|---:|---:|---:|---:|---:|---:|---:| | Lock-free watch/journal OFF (`=0`) | 10,412 | 25.82 | 38.7 / 38.9 ms | 29.0 ms | 31.5 ms | 16.2 ms (28 flushes of 6,052) | 0 | | **Lock-free watch/journal ON (`=1`)** | **10,266** | **28.34 (+2.52)** | **35.3 / 35.6 ms** | **24.3 ms (-4.7)** | **26.9 ms (-4.6)** | **9.6 ms (-6.6 ms!)** (27 flushes of 6,030) | **0** | ### Notes - Combined lock-free watch/journal path cut `refreshes` by **`-6.6 ms`** (`16.2 -> 9.6 ms`) and gained **`+2.52 FPS`** with zero stability regressions over 8+ minutes of open-world walking (both raster and DXR 1.1 HW RT). - Merge note: `#3756` and `#3760` compose cleanly except for a trivial 1-hunk adjacency in `watch.rs` where `journal_lockfree()` and `unlocked_protect()` sit right after `parts_on()`. ### Test Setup - **Hardware:** AMD Ryzen 7 9800X3D (Zen 5, 8C/16T), 32 GB DDR5, NVIDIA RTX 5070 Ti 16 GB (ReBAR ON), Windows 11 26200 - **Profile:** Series X, 2560x1440, DLSS Quality, 3x MFG (`--res 1440 --dlss quality --framegen 3 --fps 1000`) - **Build:** Release ThinLTO (`-C target-cpu=znver5`) rebased on `main` (`41adb551`) with a curated local PR stack; all A/B numbers below are **same-binary toggle ablations** from the exact same restored save (~10.0k–10.4k draws/frame standing scene) after official 1-time warmup.
Yoruiopz commented 2026-11-30 09:49:16 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Yoruiopz on 2026-11-30T09:49:16Z.


I reproduced a lost-watermark race on exact head 88a0f30d. A publisher can reserve epoch W and still be writing radix stamps. A guarded or already-covered query can take a radix snapshot that misses a pending stamp, then return watermark N greater than W. A subsequent writes_since ignores W, even after that stamp becomes visible. A landing can leave PUBLISHED true, so the reader can take that fast path during publication.

The deterministic native Windows regression fails on the exact PR logic with “write epoch 2 was hidden by watermark 3.” It passes with this synchronized radix variant. The correction keeps the journal writer guard from ticket reservation through stamp and partial-mask publication, and keeps the read projection and returned watermark in one journal snapshot. The radix index remains available as a process-start opt-in. These are two commits so the regression can be reviewed separately.

On Windows, all 70 watch tests passed in separate runs with VICTORY_JOURNAL_LOCKFREE=0 and =1 after the correction. Gameplay and performance of that corrected variant are unmeasured.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7274730388 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#issuecomment-7274730388). Originally posted by [Yoruiopz](https://github.com/Yoruiopz) on 2026-11-30T09:49:16Z. --- I reproduced a lost-watermark race on exact head 88a0f30d. A publisher can reserve epoch W and still be writing radix stamps. A guarded or already-covered query can take a radix snapshot that misses a pending stamp, then return watermark N greater than W. A subsequent writes_since ignores W, even after that stamp becomes visible. A landing can leave PUBLISHED true, so the reader can take that fast path during publication. The deterministic native Windows regression fails on the exact PR logic with “write epoch 2 was hidden by watermark 3.” It passes with [this synchronized radix variant](https://github.com/Yoruiopz/VIctory-NVIDIA/compare/88a0f30db299dad99a621e917730f6ca0eaa5b76...31bf8345a826a6c2b3eb1c17b1a6217aacf9c487). The correction keeps the journal writer guard from ticket reservation through stamp and partial-mask publication, and keeps the read projection and returned watermark in one journal snapshot. The radix index remains available as a process-start opt-in. These are two commits so the regression can be reviewed separately. On Windows, all 70 watch tests passed in separate runs with VICTORY_JOURNAL_LOCKFREE=0 and =1 after the correction. Gameplay and performance of that corrected variant are unmeasured.
ScymicX commented 2026-11-30 14:36:25 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by ScymicX on 2026-11-30T14:36:25Z.


Follow-up commit 2dd01d0 addresses the lost-watermark race caught by @Yoruiopz (and thanks @ColorC for the A/B numbers!):

  • Close lost-watermark race (EPOCH_GATE): Added EPOCH_GATE: RwLock<()> (with strict lock order JOURNAL -> EPOCH_GATE) so publication paths (owner_wrote, ask_ahead, ask_rest, ask_once, wrote, writes_with, landed_beside) serialize next_asking() against lock-free WRITTEN_EPOCHS readers (asked, writes_with). A reader can no longer advance its watermark past an in-flight write epoch before the page's WRITTEN_EPOCHS store becomes visible.
  • Process-latched toggle: Switched journal_lockfree() from crate::toggled! to OnceLock<bool> latched at process start so VICTORY_AB cannot flip between lock-free and locked mid-run.
  • Shared helper & regression test: Unified page-part checks on PageEpochs::any_has_parts_iter across parts_written, writes_with, and landed_beside, and added watch::parts_tests::epoch_gate_prevents_lost_watermark_during_publication.

Verified on 2dd01d0:

  • cargo test --locked -p victory-gpu --lib watch:: -- --test-threads=1: 67 passed, 0 failed (default VICTORY_JOURNAL_LOCKFREE ON)
  • VICTORY_JOURNAL_LOCKFREE=0 (-- --test-threads=1): 67 passed, 0 failed
  • watch::parts_tests::epoch_gate_prevents_lost_watermark_during_publication (10x loop): 10/10 passed
  • cargo clippy -p victory-gpu --no-deps --all-targets --locked (+ --all-features): 0 new warnings in watch.rs
  • cargo xtask policy --staged: 1 files ok
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7281640677 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#issuecomment-7281640677). Originally posted by [ScymicX](https://github.com/ScymicX) on 2026-11-30T14:36:25Z. --- Follow-up commit `2dd01d0` addresses the lost-watermark race caught by @Yoruiopz (and thanks @ColorC for the A/B numbers!): - **Close lost-watermark race (`EPOCH_GATE`)**: Added `EPOCH_GATE: RwLock<()>` (with strict lock order `JOURNAL -> EPOCH_GATE`) so publication paths (`owner_wrote`, `ask_ahead`, `ask_rest`, `ask_once`, `wrote`, `writes_with`, `landed_beside`) serialize `next_asking()` against lock-free `WRITTEN_EPOCHS` readers (`asked`, `writes_with`). A reader can no longer advance its watermark past an in-flight write epoch before the page's `WRITTEN_EPOCHS` store becomes visible. - **Process-latched toggle**: Switched `journal_lockfree()` from `crate::toggled!` to `OnceLock<bool>` latched at process start so `VICTORY_AB` cannot flip between lock-free and locked mid-run. - **Shared helper & regression test**: Unified page-part checks on `PageEpochs::any_has_parts_iter` across `parts_written`, `writes_with`, and `landed_beside`, and added `watch::parts_tests::epoch_gate_prevents_lost_watermark_during_publication`. Verified on `2dd01d0`: - `cargo test --locked -p victory-gpu --lib watch:: -- --test-threads=1`: **67 passed, 0 failed** (default `VICTORY_JOURNAL_LOCKFREE` ON) - `VICTORY_JOURNAL_LOCKFREE=0` (`-- --test-threads=1`): **67 passed, 0 failed** - `watch::parts_tests::epoch_gate_prevents_lost_watermark_during_publication` (10x loop): **10/10 passed** - `cargo clippy -p victory-gpu --no-deps --all-targets --locked` (+ `--all-features`): 0 new warnings in `watch.rs` - `cargo xtask policy --staged`: `1 files ok`
mstrmoos commented 2026-11-30 19:22:33 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mstrmoos on 2026-11-30T19:22:33Z.


Picked onto our branch with #3753, #3756, #3760, #3881, #3832, #3858 and four that have since merged (#3818, #3850, #3874, #3871). #3760 needed a hand merge on top of the others.

All of victory-gpu's and victory-shader's tests passed. On the RTX 5080 16 GB, Ryzen 7 9800X3D, Windows 11, Series X profile, same save and 90 s bench route each run, the frame median went from 40.4 to 39.1 ms for the whole set. I didn't measure the PRs one by one, so this is evidence of no regression rather than this PR's own gain. Screenshots looked clean.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7289289390 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#issuecomment-7289289390). Originally posted by [mstrmoos](https://github.com/mstrmoos) on 2026-11-30T19:22:33Z. --- Picked onto our branch with #3753, #3756, #3760, #3881, #3832, #3858 and four that have since merged (#3818, #3850, #3874, #3871). #3760 needed a hand merge on top of the others. All of victory-gpu's and victory-shader's tests passed. On the RTX 5080 16 GB, Ryzen 7 9800X3D, Windows 11, Series X profile, same save and 90 s bench route each run, the frame median went from 40.4 to 39.1 ms for the whole set. I didn't measure the PRs one by one, so this is evidence of no regression rather than this PR's own gain. Screenshots looked clean.
nicolasabosch commented 2026-11-30 21:52:18 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nicolasabosch on 2026-11-30T21:52:18Z.


Tested on an RTX 4070 Laptop 8 GB: no measurable fps difference in this scene (2 runs).

Tested on main e1251c31 with this PR (30d4fe95) merged on top and nothing else changed. Release build, same profile as main.

Setup

  • PC: RTX 4070 Laptop 8 GB, i9-14900HX, 32 GB RAM, Windows 11.
  • Command: victory play --series-s --dlss performance --framegen 2, with no fps cap and the window minimized.
  • Route: scripted Enter presses into the world from a save, then the character idles there for 300 s.
  • fps is the mean of the last 40 host: reports, about 200 s. These are real frames, not generated ones.
  • The submit-thread figures come from the "preparing per frame, the submitting thread" line, also over the last 40 reports.
  • The second run starts from a fixed copy of the save. In the first run the save had moved on, so the scene was a little different.
run fps ms/frame submit thread ms refreshes ms
main, 7 runs 15.96 – 18.76 53.4 – 62.7 35.0 – 41.4 13.3 – 15.4
main, fixed save only (4 runs) 16.21 – 18.18 (median 16.37) 55.1 – 61.8 35.0 – 40.5 13.3 – 15.2
this PR, run 1 16.69 59.9 39.9 14.7
this PR, run 2 (fixed save) 15.85 63.1 37.6 14.0

On this laptop, identical main runs differ by up to about 2–2.5 fps, so only an effect of roughly 2–3 fps or more would be visible in this scene. The submit thread's time asking the write watch was 4.4 and 3.8 ms with this PR, against 3.6–4.3 ms on main, so the lock-free journal did not show up in that line here. The fixed-save run (15.85) is just under main's lowest and the other run is inside main's range, so neither stands out from the noise.

Not tested: driving, camera turns, interiors, an image comparison, other GPUs.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7293022682 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#issuecomment-7293022682). Originally posted by [nicolasabosch](https://github.com/nicolasabosch) on 2026-11-30T21:52:18Z. --- **Tested on an RTX 4070 Laptop 8 GB: no measurable fps difference in this scene (2 runs).** Tested on main `e1251c31` with this PR (`30d4fe95`) merged on top and nothing else changed. Release build, same profile as main. **Setup** - PC: RTX 4070 Laptop 8 GB, i9-14900HX, 32 GB RAM, Windows 11. - Command: `victory play --series-s --dlss performance --framegen 2`, with no fps cap and the window minimized. - Route: scripted Enter presses into the world from a save, then the character idles there for 300 s. - fps is the mean of the last 40 `host:` reports, about 200 s. These are real frames, not generated ones. - The submit-thread figures come from the "preparing per frame, the submitting thread" line, also over the last 40 reports. - The second run starts from a fixed copy of the save. In the first run the save had moved on, so the scene was a little different. | run | fps | ms/frame | submit thread ms | refreshes ms | |---|---|---|---|---| | main, 7 runs | 15.96 – 18.76 | 53.4 – 62.7 | 35.0 – 41.4 | 13.3 – 15.4 | | main, fixed save only (4 runs) | 16.21 – 18.18 (median 16.37) | 55.1 – 61.8 | 35.0 – 40.5 | 13.3 – 15.2 | | this PR, run 1 | 16.69 | 59.9 | 39.9 | 14.7 | | this PR, run 2 (fixed save) | 15.85 | 63.1 | 37.6 | 14.0 | On this laptop, identical main runs differ by up to about 2–2.5 fps, so only an effect of roughly 2–3 fps or more would be visible in this scene. The submit thread's time asking the write watch was 4.4 and 3.8 ms with this PR, against 3.6–4.3 ms on main, so the lock-free journal did not show up in that line here. The fixed-save run (15.85) is just under main's lowest and the other run is inside main's range, so neither stands out from the noise. **Not tested:** driving, camera turns, interiors, an image comparison, other GPUs.
upipibi commented 2026-12-01 00:04:51 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by upipibi on 2026-12-01T00:04:51Z.


Race review of #3756 at 30d4fe95, after 2dd01d01: EPOCH_GATE closes the lost-watermark race @Yoruiopz found, on the real reader paths. One gap is in the regression test, not the code: epoch_gate_prevents_lost_watermark_during_publication takes epoch_read() itself and never calls writes_with or asked. It still passes with the gate removed from writes_with, shown below.

Audit (watch.rs line numbers on the head):

  • Every next_asking() whose number is stamped runs under epoch_write() until the stamps are in: owner_wrote :229, ask_ahead :1055, ask_once :1280, ask_rest :1461, wrote :1502, writes_with :1786 and :1794, landed_beside :3356 and :3370. All of them take JOURNAL before the gate where they take both, so the lock order holds.
  • Every lock-free reader takes its snapshot and its watermark under epoch_read(): asked :1614 and :1621, writes_with :1690, :1703, :1718 and :1774.
  • The ahead() path returns ahead_asked, read under JOURNAL before epoch_read(). That's fine, because ahead_asked is only assigned under epoch_write(), after every earlier number's stamps.
  • The other lock-free readers (nothing_new_with, nothing_new_single, new_in) compare stamps with numbers the caller already holds and return no new watermark.

Test: Yoruiopz's regression (cc9613b9) with its writer changed to follow this PR's protocol (JOURNAL, then EPOCH_GATE written, then its number), run through the real writes_with owned path:

the test (added to parts_tests)
#[test]
fn review_gated_writer_is_not_hidden_by_a_reader_watermark() {
    if !journal_lockfree() || !owned_on() {
        return;
    }
    let room = vec![0u8; 2 * PAGE];
    let start = (room.as_ptr() as usize).next_multiple_of(PAGE);
    assert!(own(start, PAGE));
    let before = next_asking();
    let pending = std::cell::RefCell::new(None);
    let (watermark, changed) = writes_with(
        start,
        start + PAGE,
        false,
        |mut pages| pages.any(|(_, &at)| at > before),
        |first, last| {
            let changed = WRITTEN_EPOCHS.any_since(first, last, before);
            // A publisher that gets in while the reader is between its
            // snapshot and its watermark.
            if let Ok(j) = JOURNAL.try_write()
                && let Ok(gate) = EPOCH_GATE.try_write()
            {
                let now = next_asking();
                pending.replace(Some((j, gate, now)));
            }
            changed
        },
    )
    .unwrap();
    let got_in = pending.borrow().is_some();
    let (mut j, gate, write) = pending.into_inner().unwrap_or_else(|| {
        let j = journal_mut();
        let gate = epoch_write();
        (j, gate, next_asking())
    });
    j.note(start / PAGE, WHOLE, write);
    drop(gate);
    drop(j);
    let seen = writes_since(start, start + PAGE, false, watermark).unwrap().1;
    disown(start);
    assert!(!changed);
    assert!(seen, "write epoch {write} was hidden by watermark {watermark} (publisher got in: {got_in})");
}

Results, with this PR merged onto main 7d93ee07 (clean merge) and #4619 applied so a known test-isolation flake in watch:: stays out:

run result
the test above, 5 runs 5 passed
epoch_gate_prevents_lost_watermark_during_publication, 3 runs 3 passed
watch:: in parallel, default (lock-free on), 20 runs 20 × 72 passed, 0 failed
watch:: with VICTORY_JOURNAL_LOCKFREE=0, 5 runs 5 × 72 passed, 0 failed
negative control: let _ep = epoch_read(); removed from the owned path of writes_with (:1703); the test above, 3 runs 3 failed: write epoch 2 was hidden by watermark 3 (publisher got in: true)
same negative control; epoch_gate_prevents_lost_watermark_during_publication, 3 runs 3 passed

So the gate is what stops this, and only the test above notices when a reader path loses it. Adding it, or one like it for asked, would keep a later change from dropping a reader's epoch_read() unnoticed. Setup: Ryzen 9 5950X, Windows 11, CPU-only tests. The head alone doesn't build the lib tests (E0061 at host.rs:20302, which isn't in this PR's diff), so all runs above are merged onto main.

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

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7295901265 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#issuecomment-7295901265). Originally posted by [upipibi](https://github.com/upipibi) on 2026-12-01T00:04:51Z. --- Race review of #3756 at 30d4fe95, after 2dd01d01: **`EPOCH_GATE` closes the lost-watermark race @Yoruiopz found, on the real reader paths.** One gap is in the regression test, not the code: `epoch_gate_prevents_lost_watermark_during_publication` takes `epoch_read()` itself and never calls `writes_with` or `asked`. It still passes with the gate removed from `writes_with`, shown below. **Audit** (watch.rs line numbers on the head): - Every `next_asking()` whose number is stamped runs under `epoch_write()` until the stamps are in: `owner_wrote` :229, `ask_ahead` :1055, `ask_once` :1280, `ask_rest` :1461, `wrote` :1502, `writes_with` :1786 and :1794, `landed_beside` :3356 and :3370. All of them take `JOURNAL` before the gate where they take both, so the lock order holds. - Every lock-free reader takes its snapshot and its watermark under `epoch_read()`: `asked` :1614 and :1621, `writes_with` :1690, :1703, :1718 and :1774. - The `ahead()` path returns `ahead_asked`, read under `JOURNAL` before `epoch_read()`. That's fine, because `ahead_asked` is only assigned under `epoch_write()`, after every earlier number's stamps. - The other lock-free readers (`nothing_new_with`, `nothing_new_single`, `new_in`) compare stamps with numbers the caller already holds and return no new watermark. **Test:** Yoruiopz's regression (cc9613b9) with its writer changed to follow this PR's protocol (`JOURNAL`, then `EPOCH_GATE` written, then its number), run through the real `writes_with` owned path: <details><summary>the test (added to <code>parts_tests</code>)</summary> ```rust #[test] fn review_gated_writer_is_not_hidden_by_a_reader_watermark() { if !journal_lockfree() || !owned_on() { return; } let room = vec![0u8; 2 * PAGE]; let start = (room.as_ptr() as usize).next_multiple_of(PAGE); assert!(own(start, PAGE)); let before = next_asking(); let pending = std::cell::RefCell::new(None); let (watermark, changed) = writes_with( start, start + PAGE, false, |mut pages| pages.any(|(_, &at)| at > before), |first, last| { let changed = WRITTEN_EPOCHS.any_since(first, last, before); // A publisher that gets in while the reader is between its // snapshot and its watermark. if let Ok(j) = JOURNAL.try_write() && let Ok(gate) = EPOCH_GATE.try_write() { let now = next_asking(); pending.replace(Some((j, gate, now))); } changed }, ) .unwrap(); let got_in = pending.borrow().is_some(); let (mut j, gate, write) = pending.into_inner().unwrap_or_else(|| { let j = journal_mut(); let gate = epoch_write(); (j, gate, next_asking()) }); j.note(start / PAGE, WHOLE, write); drop(gate); drop(j); let seen = writes_since(start, start + PAGE, false, watermark).unwrap().1; disown(start); assert!(!changed); assert!(seen, "write epoch {write} was hidden by watermark {watermark} (publisher got in: {got_in})"); } ``` </details> Results, with this PR merged onto main 7d93ee07 (clean merge) and #4619 applied so a known test-isolation flake in `watch::` stays out: | run | result | |---|---| | the test above, 5 runs | 5 passed | | `epoch_gate_prevents_lost_watermark_during_publication`, 3 runs | 3 passed | | `watch::` in parallel, default (lock-free on), 20 runs | 20 × 72 passed, 0 failed | | `watch::` with `VICTORY_JOURNAL_LOCKFREE=0`, 5 runs | 5 × 72 passed, 0 failed | | **negative control:** `let _ep = epoch_read();` removed from the owned path of `writes_with` (:1703); the test above, 3 runs | 3 failed: `write epoch 2 was hidden by watermark 3 (publisher got in: true)` | | same negative control; `epoch_gate_prevents_lost_watermark_during_publication`, 3 runs | 3 passed | So the gate is what stops this, and only the test above notices when a reader path loses it. Adding it, or one like it for `asked`, would keep a later change from dropping a reader's `epoch_read()` unnoticed. Setup: Ryzen 9 5950X, Windows 11, CPU-only tests. The head alone doesn't build the lib tests (E0061 at host.rs:20302, which isn't in this PR's diff), so all runs above are merged onto main. *Tested with the help of Claude Code (Claude Opus 5.5).*
sklzygit commented 2026-12-01 15:14:01 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by sklzygit on 2026-12-01T15:14:01Z.


I tested head 30d4fe95f0f036aa76a5eaa994c79a0c7190dfa7 and found a separate partial-history race in the whole-note fast paths of writes_with and landed_beside.

The any_has_parts check happens before acquiring the exclusive EPOCH_GATE. Another writer can install a partial note between that check and the lock. The fast path then calls note_whole without clearing the newly installed Journal::parts history. A later parts_written query can report only the earlier partial write, omitting the remaining bytes of the whole-page write.

I reproduced this with authored metadata only: own one 64 KiB page, make an initial whole note and record its watermark, then inject a 4 KiB-part note at the actual landed_beside fast-path gap using a test-only hook. Let landed_beside record all sixteen kernel pages, then query parts_written from the saved watermark. Before the fix, the result is [8]; the whole write requires [65535].

The local fix is to acquire the exclusive epoch gate before checking for partial history, and retain that guard through whole-note publication. If partial history exists, drop the guard before entering the existing JOURNAL fallback, preserving the documented JOURNAL -> EPOCH_GATE lock order. Apply this to both whole-note fast paths. The deterministic regression and the existing whole/partial-write parity fixture pass after this change.

This reports a forced legal concurrency interleaving, not proof that this race caused any particular gameplay artifact. I have not measured an in-game FPS gain from this PR.

Test rig: Ryzen 7 9800X3D, RTX 5080 16 GB, 32 GB RAM, Windows 11, NVIDIA 621.40. The regression is CPU-only and contains no title data. Normal gameplay profile is Series X, 2560x1440 output, 1080p scene, DLSS Quality, 30 FPS base cap, FG/RT off; no game was launched for these tests.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7315567725 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#issuecomment-7315567725). Originally posted by [sklzygit](https://github.com/sklzygit) on 2026-12-01T15:14:01Z. --- I tested head `30d4fe95f0f036aa76a5eaa994c79a0c7190dfa7` and found a separate partial-history race in the whole-note fast paths of `writes_with` and `landed_beside`. The `any_has_parts` check happens before acquiring the exclusive `EPOCH_GATE`. Another writer can install a partial note between that check and the lock. The fast path then calls `note_whole` without clearing the newly installed `Journal::parts` history. A later `parts_written` query can report only the earlier partial write, omitting the remaining bytes of the whole-page write. I reproduced this with authored metadata only: own one 64 KiB page, make an initial whole note and record its watermark, then inject a 4 KiB-part note at the actual `landed_beside` fast-path gap using a test-only hook. Let `landed_beside` record all sixteen kernel pages, then query `parts_written` from the saved watermark. Before the fix, the result is `[8]`; the whole write requires `[65535]`. The local fix is to acquire the exclusive epoch gate **before** checking for partial history, and retain that guard through whole-note publication. If partial history exists, drop the guard before entering the existing JOURNAL fallback, preserving the documented `JOURNAL -> EPOCH_GATE` lock order. Apply this to both whole-note fast paths. The deterministic regression and the existing whole/partial-write parity fixture pass after this change. This reports a forced legal concurrency interleaving, not proof that this race caused any particular gameplay artifact. I have not measured an in-game FPS gain from this PR. Test rig: Ryzen 7 9800X3D, RTX 5080 16 GB, 32 GB RAM, Windows 11, NVIDIA 621.40. The regression is CPU-only and contains no title data. Normal gameplay profile is Series X, 2560x1440 output, 1080p scene, DLSS Quality, 30 FPS base cap, FG/RT off; no game was launched for these tests.
trustno1-coder commented 2026-12-01 18:49:00 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:49:00Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7321601062 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3756#issuecomment-7321601062). Originally posted by [trustno1-coder](https://github.com/trustno1-coder) on 2026-12-01T18:49:00Z. --- 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

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!3756
No description provided.