Lock-free summary bitmap skips the JOURNAL lock for unchanged copies #472
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/472"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
AtomicU64buckets (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 withRelaxedloads before taking theJOURNALRwLock; when the bitmap's maximum is at or below the shadow's last taken value, the lock is skipped entirely.The JOURNAL's
written_fastHashMap holds ~460 entries per frame (29 MB at 64 KiB pages). In a measured 26-minute run the submitting thread'sflush_alliterates thousands of shadows per frame, and most return unchanged — the bitmap turns the common case into one atomic load instead of anRwLockread acquisition plus aHashMap::get. On the slow path (bitmap says "maybe dirty"),min_takenis 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
RwLocksharding of the JOURNAL'swritten/written_fast/partsfields — is implemented in #478 asVICTORY_JOURNAL_SHARDS=1and 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 inwatch.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-gpuclean.🤖 Generated with Claude Code
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
3e28da6merged onto release v0.8.3.3 (same code as main7dbcdcb), 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).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.ps1settings (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.
Imported from readmet3xt-lab/foxhound. Originally posted by kwaadrat on 2026-12-02T23:40:24Z.
Reviewed
shadow.rsandwatch.rs: nice lock-free fast path. Usingmin_taken(conservative lower bound acrosscopied.pageswhenunfilled == 0) together withpublished(start, end)before scanningSUMMARYkeeps false negatives impossible while letting collisions/stalemin_takensafely fall back toread_journal(). Checkedsummary_indexas well — Fibonacci shift byusize::BITS - 14cleanly maps intoSUMMARY_LEN.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
JOURNALreads.My fork's change (
journal-mirror, rebased on main):Journal::written: oneAtomicU64per 64 KiB page, plus the latest asking for each group of 64 pages.Arcwhenever it changes.writes_into(hits on the asking ahead and answers from the published bits),writes_since,nothing_new_inandnothing_new_min.JournalReadholds 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).VICTORY_PORT_CONSTANTS=1, which is off by default upstream. Both also ran with my fork'sVICTORY_HOME=1, which keeps hot runs of the title's big buffer in the card's memory and is not upstream.writes_intoanswered from the asking ahead or the published bits (~900 calls, all threads)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_TOPand a newVICTORY_GUARDS_TOP, which counts the guards' lock by call site and by thread:kernel_writingwait ~33 ms per frame, summed over threads. Those waits do not correlate with frame spikes (r ≈ −0.01 over 251 one-second lines).So
JOURNALcontention 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 coveringwrites_intoandwrites_sincetoo is wanted.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_TOPandVICTORY_GUARDS_TOP.Your numbers confirm what we suspected: the
JOURNALread-lock contention is real (thewrites_intoper-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-mirrorbranch already coverswrites_into,writes_since,nothing_new_inandnothing_new_minwith 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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.