Lock-free summary bitmap skips the JOURNAL lock for unchanged copies #472

Open
GValdezN wants to merge 1 commit from upstream-pr/472 into main
GValdezN commented 2026-12-02 15:05:20 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-02T15:05:20Z.


Lock-free summary bitmap skips the JOURNAL lock for unchanged copies

A static array of 16,384 AtomicU64 buckets (128 KiB, Fibonacci-hashed by page number) records the highest asking number that noted a write in each bucket. Shadow::nothing_new() reads the bucket for the shadow's page range with Relaxed loads before taking the JOURNAL RwLock; when the bitmap's maximum is at or below the shadow's last taken value, the lock is skipped entirely.

The JOURNAL's written_fast HashMap holds ~460 entries per frame (29 MB at 64 KiB pages). In a measured 26-minute run the submitting thread's flush_all iterates thousands of shadows per frame, and most return unchanged — the bitmap turns the common case into one atomic load instead of an RwLock read acquisition plus a HashMap::get. On the slow path (bitmap says "maybe dirty"), min_taken is refreshed for buffers over 64 pages so that subsequent frames benefit from the tighter bound.

This is the first level of a layered approach to JOURNAL contention (2–4 ms/frame in heavy scenes, SPLIT[6] in the frame-time log). A second level — 16-way RwLock sharding of the JOURNAL's written/written_fast/parts fields — is implemented in #478 as VICTORY_JOURNAL_SHARDS=1 and is strictly complementary: the bitmap eliminates ~91% of lock acquisitions, and sharding reduces contention on the remaining ~9% that do read or write the journal. Complements #434 (striped VirtualProtect outside GUARDS): both reduce lock scope in watch.rs, but on different locks and different code paths — no conflict.

Gate: always on (the bitmap check is cheaper than the lock it replaces). A unit test (summary_tracks_max_asking_per_bucket) verifies the bucket tracks the maximum asking.

Test plan: cargo test -p victory-gpu -- summary_tracks (1 passed); cargo check -p victory-gpu clean.

🤖 Generated with Claude Code

<!-- greyfinch-sync:readmet3xt-lab/foxhound:issue:5609136489 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/472). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-02T15:05:20Z. --- ## Lock-free summary bitmap skips the JOURNAL lock for unchanged copies A static array of 16,384 `AtomicU64` buckets (128 KiB, Fibonacci-hashed by page number) records the highest asking number that noted a write in each bucket. `Shadow::nothing_new()` reads the bucket for the shadow's page range with `Relaxed` loads before taking the `JOURNAL` `RwLock`; when the bitmap's maximum is at or below the shadow's last taken value, the lock is skipped entirely. The JOURNAL's `written_fast` HashMap holds ~460 entries per frame (29 MB at 64 KiB pages). In a measured 26-minute run the submitting thread's `flush_all` iterates thousands of shadows per frame, and most return unchanged — the bitmap turns the common case into one atomic load instead of an `RwLock` read acquisition plus a `HashMap::get`. On the slow path (bitmap says "maybe dirty"), `min_taken` is refreshed for buffers over 64 pages so that subsequent frames benefit from the tighter bound. This is the first level of a layered approach to JOURNAL contention (2–4 ms/frame in heavy scenes, SPLIT[6] in the frame-time log). A second level — 16-way `RwLock` sharding of the JOURNAL's `written`/`written_fast`/`parts` fields — is implemented in #478 as `VICTORY_JOURNAL_SHARDS=1` and is strictly complementary: the bitmap eliminates ~91% of lock acquisitions, and sharding reduces contention on the remaining ~9% that do read or write the journal. Complements #434 (striped VirtualProtect outside GUARDS): both reduce lock scope in `watch.rs`, but on different locks and different code paths — no conflict. Gate: always on (the bitmap check is cheaper than the lock it replaces). A unit test (`summary_tracks_max_asking_per_bucket`) verifies the bucket tracks the maximum asking. Test plan: `cargo test -p victory-gpu -- summary_tracks` (1 passed); `cargo check -p victory-gpu` clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
tillo88 commented 2026-12-02 21:05:59 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by tillo88 on 2026-12-02T21:05:59Z.


Replay-bench measurement: no measurable difference on v0.8.3.3

Measured: PR head 3e28da6 merged onto release v0.8.3.3 (same code as main 7dbcdcb), 2 measured runs (after a warm-up run that is not counted), against 3 runs of v0.8.3.3 without the PR, same route (~6.5 min).

run fps CPU prep ms median ms p95 ms p99 ms frames > 100 ms
release run 1 36.54 19.08 29.00 50.42 60.01 32
release run 2 36.45 20.15 28.10 38.59 45.48 13
release run 3 36.15 19.30 29.35 38.74 46.08 10
PR #472 run 1 36.10 19.61 29.60 39.23 47.88 13
PR #472 run 2 35.90 20.31 29.20 39.04 47.91 19

Mean fps: release 36.38, PR 36.00 (-1.0%). That is below the 2-3% we treat as a real change, so: no measurable difference on this route. Only 3 release runs so far, so the release's own spread is not established yet.

Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost).

Note: two earlier release runs the same evening, with identical switches, were much slower for most of the route (6-9 fps average); the cause is not known yet and they are left out of the comparison above. No PR run fell into that state.

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default play-foxhound.ps1 settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap.

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

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7358788497 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/472#issuecomment-7358788497). Originally posted by [tillo88](https://github.com/tillo88) on 2026-12-02T21:05:59Z. --- **Replay-bench measurement: no measurable difference on v0.8.3.3** Measured: PR head `3e28da6` merged onto release **v0.8.3.3** (same code as main `7dbcdcb`), 2 measured runs (after a warm-up run that is not counted), against 3 runs of v0.8.3.3 without the PR, same route (~6.5 min). | run | fps | CPU prep ms | median ms | p95 ms | p99 ms | frames > 100 ms | |---|---|---|---|---|---|---| | release run 1 | 36.54 | 19.08 | 29.00 | 50.42 | 60.01 | 32 | | release run 2 | 36.45 | 20.15 | 28.10 | 38.59 | 45.48 | 13 | | release run 3 | 36.15 | 19.30 | 29.35 | 38.74 | 46.08 | 10 | | PR #472 run 1 | 36.10 | 19.61 | 29.60 | 39.23 | 47.88 | 13 | | PR #472 run 2 | 35.90 | 20.31 | 29.20 | 39.04 | 47.91 | 19 | Mean fps: release 36.38, PR 36.00 (-1.0%). That is below the 2-3% we treat as a real change, so: **no measurable difference** on this route. Only 3 release runs so far, so the release's own spread is not established yet. Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost). Note: two earlier release runs the same evening, with identical switches, were much slower for most of the route (6-9 fps average); the cause is not known yet and they are left out of the comparison above. No PR run fell into that state. Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default `play-foxhound.ps1` settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap. Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
kwaadrat commented 2026-12-02 23:40:24 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by kwaadrat on 2026-12-02T23:40:24Z.


Reviewed shadow.rs and watch.rs: nice lock-free fast path. Using min_taken (conservative lower bound across copied.pages when unfilled == 0) together with published(start, end) before scanning SUMMARY keeps false negatives impossible while letting collisions/stale min_taken safely fall back to read_journal(). Checked summary_index as well — Fibonacci shift by usize::BITS - 14 cleanly maps into SUMMARY_LEN.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7362287726 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/472#issuecomment-7362287726). Originally posted by [kwaadrat](https://github.com/kwaadrat) on 2026-12-02T23:40:24Z. --- Reviewed `shadow.rs` and `watch.rs`: nice lock-free fast path. Using `min_taken` (conservative lower bound across `copied.pages` when `unfilled == 0`) together with `published(start, end)` before scanning `SUMMARY` keeps false negatives impossible while letting collisions/stale `min_taken` safely fall back to `read_journal()`. Checked `summary_index` as well — Fibonacci shift by `usize::BITS - 14` cleanly maps into `SUMMARY_LEN`.
BrenoMazini commented 2026-12-03 01:08:21 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by BrenoMazini on 2026-12-03T01:08:21Z.


These are the causes behind @tillo88's replay-bench result here and on #478. They come from runs of a similar change in my fork, which covers more of the JOURNAL reads.

My fork's change (journal-mirror, rebased on main):

  • It keeps a lock-free copy of Journal::written: one AtomicU64 per 64 KiB page, plus the latest asking for each group of 64 pages.
  • The asking ahead is published as an Arc whenever it changes.
  • These read from that copy instead of taking the lock: writes_into (hits on the asking ahead and answers from the published bits), writes_since, nothing_new_in and nothing_new_min.
  • A batch's JournalRead holds a snapshot instead of the lock. Writers still take the lock.

So it removes more lock reads than the bitmap does.

Before and after. Same heavy scene, two play sessions: before, 7,027 indirect executions per frame; after, 7,448. Not a replay.

Setup: Ryzen 7 5700X, RTX 4070 SUPER, Series X profile, 3440x1440, DLSS performance.

Version tested: builds of my fork based on v0.8.3.3 (main 7dbcdcb, the same code as the replay bench).

  • The before and after builds differ only in the journal change.
  • Both ran with VICTORY_PORT_CONSTANTS=1, which is off by default upstream. Both also ran with my fork's VICTORY_HOME=1, which keeps hot runs of the title's big buffer in the card's memory and is not upstream.
  • So the submitting thread's absolute times are not comparable with the replay bench's default setup. The difference between before and after is.
Per frame Before After
writes_into answered from the asking ahead or the published bits (~900 calls, all threads) 5-9 ms (~7 µs each, under the read lock) almost nothing
Noting into the journal (~80 calls, ~70 µs each waiting for the write lock) 4.8-7.3 ms 1.7 ms
Checks that a copy has nothing new in batches under the read lock 8,135 in 5.4 ms (~0.5 µs each)
Submitting thread, mean 53.5 ms 53.3 ms
Submitting thread, p90 71.7 ms 65.7 ms
Of which: asking the kernel's write watch 10.2 ms 11.6 ms
Of which: refreshes of host copies 15.3 ms 14.6 ms

The lock contention went away, but the submitting thread's time did not change. That matches the replay bench.

What the submitting thread spends instead. I measured with VICTORY_REFRESH_TOP and a new VICTORY_GUARDS_TOP, which counts the guards' lock by call site and by thread:

  • About 11 ms of the remaining time is the kernel's write-watch asking, and the rest is the copies themselves. Neither is a lock wait.
  • The guards' lock costs the submitting thread 4-6 ms per frame (wait plus hold).
  • The large waits on the guards' lock are elsewhere: the title's file-read threads in kernel_writing wait ~33 ms per frame, summed over threads. Those waits do not correlate with frame spikes (r ≈ −0.01 over 251 one-second lines).

So JOURNAL contention is real, and removing it is harmless, but in my runs it did not shorten the frame. This PR still looks worth keeping as a cheap fast path. I won't open my branch as a competing PR unless covering writes_into and writes_since too is wanted.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7363995202 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/472#issuecomment-7363995202). Originally posted by [BrenoMazini](https://github.com/BrenoMazini) on 2026-12-03T01:08:21Z. --- These are the causes behind @tillo88's replay-bench result here and on #478. They come from runs of a similar change in my fork, which covers more of the `JOURNAL` reads. **My fork's change** (`journal-mirror`, rebased on main): - It keeps a lock-free copy of `Journal::written`: one `AtomicU64` per 64 KiB page, plus the latest asking for each group of 64 pages. - The asking ahead is published as an `Arc` whenever it changes. - These read from that copy instead of taking the lock: `writes_into` (hits on the asking ahead and answers from the published bits), `writes_since`, `nothing_new_in` and `nothing_new_min`. - A batch's `JournalRead` holds a snapshot instead of the lock. Writers still take the lock. So it removes more lock reads than the bitmap does. **Before and after.** Same heavy scene, two play sessions: before, 7,027 indirect executions per frame; after, 7,448. Not a replay. Setup: Ryzen 7 5700X, RTX 4070 SUPER, Series X profile, 3440x1440, DLSS performance. Version tested: builds of my fork based on **v0.8.3.3** (main `7dbcdcb`, the same code as the replay bench). - The before and after builds differ only in the journal change. - Both ran with `VICTORY_PORT_CONSTANTS=1`, which is off by default upstream. Both also ran with my fork's `VICTORY_HOME=1`, which keeps hot runs of the title's big buffer in the card's memory and is not upstream. - So the submitting thread's absolute times are not comparable with the replay bench's default setup. The difference between before and after is. | Per frame | Before | After | |---|---|---| | `writes_into` answered from the asking ahead or the published bits (~900 calls, all threads) | 5-9 ms (~7 µs each, under the read lock) | almost nothing | | Noting into the journal (~80 calls, ~70 µs each waiting for the write lock) | 4.8-7.3 ms | 1.7 ms | | Checks that a copy has nothing new | in batches under the read lock | 8,135 in 5.4 ms (~0.5 µs each) | | **Submitting thread, mean** | **53.5 ms** | **53.3 ms** | | Submitting thread, p90 | 71.7 ms | 65.7 ms | | Of which: asking the kernel's write watch | 10.2 ms | 11.6 ms | | Of which: refreshes of host copies | 15.3 ms | 14.6 ms | The lock contention went away, but the submitting thread's time did not change. That matches the replay bench. **What the submitting thread spends instead.** I measured with `VICTORY_REFRESH_TOP` and a new `VICTORY_GUARDS_TOP`, which counts the guards' lock by call site and by thread: - About 11 ms of the remaining time is the kernel's write-watch asking, and the rest is the copies themselves. Neither is a lock wait. - The guards' lock costs the submitting thread 4-6 ms per frame (wait plus hold). - The large waits on the guards' lock are elsewhere: the title's file-read threads in `kernel_writing` wait ~33 ms per frame, summed over threads. Those waits do not correlate with frame spikes (r ≈ −0.01 over 251 one-second lines). So `JOURNAL` contention is real, and removing it is harmless, but in my runs it did not shorten the frame. This PR still looks worth keeping as a cheap fast path. I won't open my branch as a competing PR unless covering `writes_into` and `writes_since` too is wanted.
GValdezN commented 2026-12-03 01:33:54 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-03T01:33:54Z.


Thanks for the thorough analysis and for sharing your measurements, @BrenoMazini — really helpful to see the before/after breakdown with VICTORY_REFRESH_TOP and VICTORY_GUARDS_TOP.

Your numbers confirm what we suspected: the JOURNAL read-lock contention is real (the writes_into per-call cost drops to nearly zero with your atomic mirror), but the submitting thread's total time barely moves because the bottleneck is downstream — kernel write-watch asking and the copies themselves.

Given that your journal-mirror branch already covers writes_into, writes_since, nothing_new_in and nothing_new_min with the atomic copy, and you have the measurements to back it: would you be open to opening it as a follow-up PR? It would layer cleanly on top of this bitmap (or replace the overlapping parts), and having the full lock-free journal read path upstream seems worth it — especially for the p90 improvement (71.7 → 65.7 ms) even if the mean stays flat.

Happy to help review it if you do.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7364485206 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/472#issuecomment-7364485206). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-03T01:33:54Z. --- Thanks for the thorough analysis and for sharing your measurements, @BrenoMazini — really helpful to see the before/after breakdown with `VICTORY_REFRESH_TOP` and `VICTORY_GUARDS_TOP`. Your numbers confirm what we suspected: the `JOURNAL` read-lock contention is real (the `writes_into` per-call cost drops to nearly zero with your atomic mirror), but the submitting thread's total time barely moves because the bottleneck is downstream — kernel write-watch asking and the copies themselves. Given that your `journal-mirror` branch already covers `writes_into`, `writes_since`, `nothing_new_in` and `nothing_new_min` with the atomic copy, and you have the measurements to back it: would you be open to opening it as a follow-up PR? It would layer cleanly on top of this bitmap (or replace the overlapping parts), and having the full lock-free journal read path upstream seems worth it — especially for the p90 improvement (71.7 → 65.7 ms) even if the mean stays flat. Happy to help review it if you do.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.

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

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

No due date set.

Reference
readmet3xt-lab/foxhound!472
No description provided.