Refreshes: a quick look with no lock for copies with nothing new, fan-out only when there is enough, and why each is not known clean #2010

Closed
github-bot wants to merge 4 commits from github-bot/gh-2010 into main AGit
Owner

@⁠binarycleric on GitHub · 2026-11-29 07:10 UTC · original
up/refresh-clean-check → main · +1096 −39 · 4 files · 4 commits · closed without merging

What it changes

Before each submission, the submitting thread refreshes the host copies its lists read. In the game world that's about 10,000 refreshes a frame, and about nine in ten of them find nothing new. Each of those still walked several cold cache lines of its copy, took its pending, copied and ranged locks, and took the journal's read lock. When four or more were left, the work also went out to the thread pool. In a play log (RTX 5070 Ti, about 17 fps in the game world), refreshes were the largest part of the submitting thread's time: 15.2 ms of 45 ms a frame.

Four commits:

  1. Counting why. nothing_new says why a range isn't known to copy nothing (NotNew: shader writes pending, not watched, not covered by the asking or the bits, a page never taken, written since taken). The refreshes' frame line counts each reason. Nothing changes behaviour.
  2. A bench. host::refresh_tests::refreshes_at_a_frames_shape (ignored) runs refreshes shaped like a gameplay frame: 6,000 copies, about 10,300 refreshes in 40 flushes, 8 busy threads. It needs switch::set_for_test, so VICTORY_BENCH_AB=<switch> can turn a switch off and on in one process.
  3. A quick look with no lock (VICTORY_CLEAN_CHECK, on; =0 off). Each copy keeps Clean, a 32-byte record read under a sequence count: the range it is known to hold as of an asking. watch::LAST_WRITTEN keeps, per page, the latest asking that found it written, and watch::Ahead keeps the standing asking. A refresh whose range lies inside the record is told "nothing new" with no lock. That needs three things: the range is covered by the standing asking or known by its bits, no page of it was found written after the record's asking, and no shader writes are declared. The look runs once per item, before any fan-out. It only ever gives the answer nothing_new would give; anything else goes on to nothing_new as before.
    • What resets the record: shaders given the copy (writing), shader writes landing (copy_back_done), merges under title writes (merge_under_writes), landings beside a guard (landed), and memory found not watched (update). The record only moves forward in copy_stale and in nothing_new, under the copied lock.
    • Writers audited on this base: copied is private to shadow.rs, and its only writers are Shadow::new, copy_stale, merge_under_writes and landed. Each either moves the record forward under the lock or calls forget. The Copied struct on main merged the old copied and ranged locks into one but added no new writer.
    • Memory its owner reports writes for (watch::owned, the ray-tracing arenas): the quick look doesn't accept it, so those ranges fall through to nothing_new as before.
  4. Fan out only when there's enough work (VICTORY_SPREAD_FEW, on; =0 off). The refreshes left after the looks go to several threads only at 32 or more items or 256 KiB or more. Before, it was at 4 or more, and each wide() paid for waking threads. This commit also counts the spans after nothing_new filters them (refresh_found(found, due)). Counting them before indexed past the end. The test refreshes_in_front_of_the_steps_some_left_out_by_nothing_new covers it.

How it was tested

  • GPU and profile: RTX 5070 Ti 16 GB, Ryzen 9 7900X; Series X profile, 1440p native, DLSS off, frame generation off; ray-tracing passes left out (no structure builder)
  • Before: bench, median flush time 5.53 ms a frame (95th percentile 22.4 ms), 4,100 refreshes a frame sent to several threads. In play, before this work, refreshes took 15.2 ms a frame (10,367 refreshes, 9,319 of them known to copy nothing).
  • After: bench, median 3.11 ms a frame (95th percentile 17.6 ms), 389 sent to several threads. The quick look answered all 9,738 that copy nothing. In play with these commits on our integration branch (26–28 fps), refreshes take 8.9–9.2 ms a frame: about 6,050–6,090 refreshes in 38 flushes, 5,280–5,350 known to copy nothing, 4,820–4,870 of them by the quick look. Other changes are in that build too, so the bench is the measurement of this PR.
  • cargo test passed for the crates it touches: yes. Rebased onto main e1f2eb60: cargo test --release -p victory-gpu --lib, 594 passed, 0 failed, 45 ignored, twice in a row. A first run had one failure in descriptor::host::tests::views_off_their_elements_get_a_copy_of_their_bytes (a file this PR doesn't touch), which passed 3 of 3 times run alone. cargo clippy -p victory-gpu --release --all-targets reports nothing new in the changed files.

Bench:

VICTORY_BENCH_AB=VICTORY_CLEAN_CHECK cargo test --release -p victory-gpu --lib refreshes_at_a_frames_shape -- --ignored --nocapture

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related:

    • #614 also makes no-op refreshes lock-free. It turns Shadow::copied into atomics with a copy lock.
    • #318 batches the no-op refresh checks.
    • #1298 and #1561 cut lock contention in refreshes among other changes.

    This PR keeps copied's lock and adds a separate record that only ever gives nothing_new's answer, behind a switch, plus the per-reason counts and the bench. If one of the others lands first, the counts, the bench and the fan-out threshold can be rebased onto it.

**@⁠binarycleric** on GitHub · 2026-11-29 07:10 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2010) `up/refresh-clean-check` → `main` · +1096 −39 · 4 files · 4 commits · closed without merging ## What it changes Before each submission, the submitting thread refreshes the host copies its lists read. In the game world that's about 10,000 refreshes a frame, and about nine in ten of them find nothing new. Each of those still walked several cold cache lines of its copy, took its `pending`, `copied` and `ranged` locks, and took the journal's read lock. When four or more were left, the work also went out to the thread pool. In a play log (RTX 5070 Ti, about 17 fps in the game world), refreshes were the largest part of the submitting thread's time: 15.2 ms of 45 ms a frame. Four commits: 1. **Counting why.** `nothing_new` says why a range isn't known to copy nothing (`NotNew`: shader writes pending, not watched, not covered by the asking or the bits, a page never taken, written since taken). The refreshes' frame line counts each reason. Nothing changes behaviour. 2. **A bench.** `host::refresh_tests::refreshes_at_a_frames_shape` (ignored) runs refreshes shaped like a gameplay frame: 6,000 copies, about 10,300 refreshes in 40 flushes, 8 busy threads. It needs `switch::set_for_test`, so `VICTORY_BENCH_AB=<switch>` can turn a switch off and on in one process. 3. **A quick look with no lock** (`VICTORY_CLEAN_CHECK`, on; `=0` off). Each copy keeps `Clean`, a 32-byte record read under a sequence count: the range it is known to hold as of an asking. `watch::LAST_WRITTEN` keeps, per page, the latest asking that found it written, and `watch::Ahead` keeps the standing asking. A refresh whose range lies inside the record is told "nothing new" with no lock. That needs three things: the range is covered by the standing asking or known by its bits, no page of it was found written after the record's asking, and no shader writes are declared. The look runs once per item, before any fan-out. It only ever gives the answer `nothing_new` would give; anything else goes on to `nothing_new` as before. - **What resets the record:** shaders given the copy (`writing`), shader writes landing (`copy_back_done`), merges under title writes (`merge_under_writes`), landings beside a guard (`landed`), and memory found not watched (`update`). The record only moves forward in `copy_stale` and in `nothing_new`, under the `copied` lock. - **Writers audited on this base:** `copied` is private to `shadow.rs`, and its only writers are `Shadow::new`, `copy_stale`, `merge_under_writes` and `landed`. Each either moves the record forward under the lock or calls `forget`. The `Copied` struct on main merged the old `copied` and `ranged` locks into one but added no new writer. - **Memory its owner reports writes for** (`watch::owned`, the ray-tracing arenas): the quick look doesn't accept it, so those ranges fall through to `nothing_new` as before. 4. **Fan out only when there's enough work** (`VICTORY_SPREAD_FEW`, on; `=0` off). The refreshes left after the looks go to several threads only at 32 or more items or 256 KiB or more. Before, it was at 4 or more, and each `wide()` paid for waking threads. This commit also counts the spans *after* `nothing_new` filters them (`refresh_found(found, due)`). Counting them before indexed past the end. The test `refreshes_in_front_of_the_steps_some_left_out_by_nothing_new` covers it. ## How it was tested - GPU and profile: RTX 5070 Ti 16 GB, Ryzen 9 7900X; Series X profile, 1440p native, DLSS off, frame generation off; ray-tracing passes left out (no structure builder) - Before: bench, median flush time 5.53 ms a frame (95th percentile 22.4 ms), 4,100 refreshes a frame sent to several threads. In play, before this work, refreshes took 15.2 ms a frame (10,367 refreshes, 9,319 of them known to copy nothing). - After: bench, median 3.11 ms a frame (95th percentile 17.6 ms), 389 sent to several threads. The quick look answered all 9,738 that copy nothing. In play with these commits on our integration branch (26–28 fps), refreshes take 8.9–9.2 ms a frame: about 6,050–6,090 refreshes in 38 flushes, 5,280–5,350 known to copy nothing, 4,820–4,870 of them by the quick look. Other changes are in that build too, so the bench is the measurement of this PR. - `cargo test` passed for the crates it touches: yes. Rebased onto main e1f2eb60: `cargo test --release -p victory-gpu --lib`, 594 passed, 0 failed, 45 ignored, twice in a row. A first run had one failure in `descriptor::host::tests::views_off_their_elements_get_a_copy_of_their_bytes` (a file this PR doesn't touch), which passed 3 of 3 times run alone. `cargo clippy -p victory-gpu --release --all-targets` reports nothing new in the changed files. Bench: ``` VICTORY_BENCH_AB=VICTORY_CLEAN_CHECK cargo test --release -p victory-gpu --lib refreshes_at_a_frames_shape -- --ignored --nocapture ``` ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: - #614 also makes no-op refreshes lock-free. It turns `Shadow::copied` into atomics with a copy lock. - #318 batches the no-op refresh checks. - #1298 and #1561 cut lock contention in refreshes among other changes. This PR keeps `copied`'s lock and adds a separate record that only ever gives `nothing_new`'s answer, behind a switch, plus the per-reason counts and the bench. If one of the others lands first, the counts, the bench and the fan-out threshold can be rebased onto it.
Shadow::nothing_new now says why it could not tell a refresh copies
nothing: shader writes pending, memory not watched, the range not covered
by the submission's asking nor known by its bits, a page never taken, or
a page written since it was taken. The frame line of the refreshes gives
the split per frame, in every build: nine in ten refreshes are told they
copy nothing, and this says what keeps the tenth from it.
10,340 refreshes in 40 flushes of 6,000 copies (a sixth of view memory)
made in no order among other allocations, a tenth of them written by the
title between frames, eight threads walking memory beside: the play log
of 2026-11-28 at 1120dd7 had 10,367 in 41 flushes, 9,319 known to copy
nothing. The switches named in VICTORY_BENCH_AB are turned off and on in
turns, so both see the same load, and the median and 95th percentile of
the submitting thread's time in the flushes are printed for each, with
why the refreshes not known to copy nothing were not.

Tests may set a switch for their process (switch::set_for_test); the
switches that read it do so where they are read once a flush.
Each flush of refreshes now first looks at every refresh on the
submitting thread, once, before nothing_new, the write-watch asking or
any thread is woken: a range a copy knows clean, covered by the standing
asking (a copy of its ranges kept in the Batch) or known by its bits, of
which no page was found written by an asking after the copy's, is told
it copies nothing there. Anything else goes on as before.

What the look reads, with no lock:
- per copy, a record (from, to, asking) in a line of its own, under a
  sequence count: written at the end of copy_stale (the asking it took the
  range under) and where nothing_new says yes (the asking count read with
  the journal held: any write noted later has a higher number);
- per 64 KiB page, the last asking that found it written, and the highest
  per 2 MiB (watch::LAST_WRITTEN), raised in Journal::note, the journal's
  only writer, before the journal's lock is let go and before anything is
  published;
- a count of changes to the asking's ranges, so the Batch's copy of them
  stands for nothing once another asking or ask_once_end changes them.

The look is only ever the journal's answer: the record is forgotten when
shader writes are declared (and nothing is noted until they land and a
refresh looks again), when they land (copy_back_done, a landing beside a
guard, merge_under_writes) and when the memory turns out not to be
watched. Copies in place are always clean, as nothing_new says.

VICTORY_CLEAN_CHECK=0 turns it off. The frame line says how many the look
told.

Bench (refreshes_at_a_frames_shape, 10,340 refreshes in 40 flushes, 8
threads busy, off and on in 10 alternating turns of 40 frames, on main
with the next commit's switch on as well): the submitting thread's
flushes 3.33 ms a frame in the median off, 1.64 on, lower in every turn.
All 9,737 refreshes a frame known to copy nothing were told by the look;
391 a frame went to the threads against 4,100. (On the hcx work, before
the next commit: 4.82 ms and 6.18 at the 95th percentile off, 2.88 and
3.94 on.)

Tests: clean copies of both kinds of memory are known, and each writer
has the record forgotten; under four threads writing all the while,
every number written before a submission's asking is in its copies
after the submission (both fail with the look's page check taken out).
What is left of a flush's refreshes once the looks have told those that
copy nothing (flush_all, and in front of the steps refresh_front and
refresh_ahead) goes to the kept threads only when there are 32 or more,
or their ranges come to 256 KiB, the most they can copy; else the
submitting thread does them itself. Before: 4 or more in a flush, any
number in front of the steps. Textures in front of the steps, two or more,
still go to the threads.

A frame makes some 70 such calls, each waking a sleeping helper (50
microseconds or more) for what the submitting thread now does in a few
microseconds alone. VICTORY_SPREAD_FEW=0: as before. The same refreshes
are done either way, only on other threads.

Bench (refreshes_at_a_frames_shape, quick look on, off and on in 10
alternating turns of 40 frames, on main): flushes 2.21 ms a frame in the
median, 15.33 at the 95th percentile off; 1.82 and 8.76 on, lower in
every turn. With the quick look's switch as well, both off against both
on, two runs: 4.75 against 1.92 ms and 3.88 against 1.61 ms in the
median. (On the hcx work: 4.20 and 5.72 off, 3.15 and 4.86 on.)

In front of the steps the count of what is left is taken after
nothing_new has had its look (Batch::front_refreshes, split out of
refresh_front so a test can call it without a list): taken before, it
indexed past the end whenever 256 or fewer were left and nothing_new
told one of them it copies nothing. The new test
refreshes_in_front_of_the_steps_some_left_out_by_nothing_new has 40,
half of them clean in a way only nothing_new sees, with the quick look
on and off and on one thread and several; with the count taken before
it fails as the game did (index out of bounds).

Tests that set a switch, or assert on one, hold switch::hold_for_test,
so they do not run beside each other.

The racing-writes test now submits until its writers have written enough
as well: beside other tests a write may wait in its fault a while.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 07:10:45 +00:00
github-bot changed title from [pr-performance] Refreshes: a quick look with no lock for copies with nothing new, fan-out only when there is enough, and why each is not known clean to Refreshes: a quick look with no lock for copies with nothing new, fan-out only when there is enough, and why each is not known clean 2026-11-29 07:16:14 +00:00
github-bot 2026-11-29 07:16:14 +00:00

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