[pr-fix][performance][shaders][memory] Compile memory gate: a compile a draw waits on goes first (#70) #350

Open
tommy66 wants to merge 3 commits from tommy66/Darner:compile-gate-waited-first into main
First-time contributor

Fixes the freezes in #70: a pass over the screen waiting seconds for its pipeline while the PC is short of free RAM.

What was wrong

Under the soft floor of free memory (DARNER_MEMORY_FLOOR_MB, 1024 MB) the gate in compilemem.rs lets one driver compile run at a time. Every other compile, the ones a frame is waiting on and the workers' early compiles alike, waits on the same condition variable, and after notify_all whichever thread takes the lock first starts. The workers' queue already puts a waited-on pipeline first (Queue::expedite) and raises its worker's priority (boost), but once that worker reaches the gate it has no precedence there. So a frame's pipeline could lose its turn to early compiles again and again, until the screen-wait cap (10 s).

The change

DARNER_COMPILE_WAITED_FIRST is on by default; 0 brings back the old behaviour.

  • A pipeline worker tells the gate whether a draw waits on the pipeline it makes (compilemem::asked: its waiters, or kept_up). Threads other than the workers count as waited on, as before. The gate asks again on every look, because a draw may come to wait while the compile is queued. A draw that starts waiting wakes the gate (compilemem::nudge).
  • While a compile a draw waits on is queued at the gate, no compile nobody waits on is let start.
  • Under the soft floor, a compile a draw waits on may run beside the one compile running, one such at a time. The commit floor (DARNER_COMMIT_FLOOR_MB, the hard limit) and the half second after a failed allocation still hold for it, so it adds at most one compile's memory under the soft floor, never under the hard one.
  • The gate's log line now also says how many compiles a draw waited on started, how often and how long one waited at the gate, and how many ran beside another. These figures are counted with the switch off too, for comparing.

Measured

RTX 3090 24 GB, driver 610.88, Ryzen 7 9800X3D, 32 GB, Windows 11 26200. Series X, 1440p, hardware RT, DLAA, DLSS FG 2x. A scripted run (keyboard walk from the save) of 5 min in the world each. Every run started from the same host pipeline library snapshot, with Darner's NVIDIA driver cache files emptied before each run (set aside and restored after), and a helper process holding 12 GB of RAM, which leaves free RAM about where #70's 32 GB PC had it.

run DARNER_COMPILE_WAITED_FIRST free RAM min / median screen-pass waits > 0.5 s their total worst waited-on compiles held at the gate fps samples in play (5 s each)
a4 0 402 / 958 MB 25 120.3 s 10.0 s 146 times, 654 s 5
b4 1 365 / 3586 MB 23 29.7 s 2.6 s 120 times, 95 s (108 ran beside) 45
a5 0 498 / 1212 MB 32 127.3 s 10.0 s 135 times, 415 s 23
b5 1 586 / 1062 MB 28 42.6 s 4.9 s 286 times, 323 s (121 ran beside) 35
  • Off: 124 s of screen waits per run on average, both runs hitting the 10 s cap.
  • On: 36 s on average, the worst 2.6 to 4.9 s. The game spent more of the 5 minutes actually drawing.
  • No crash, no device loss and no out-of-memory in any run. Compiles put off for commit: 71 and 79 off, 17 and 138 on (b5 met more waited-on compiles).
  • b4 had more free RAM than the others (median 3.6 GB), so a5/b5 is the fairer pair: 127 s down to 43 s, worst 10 s down to 4.9 s.
  • With a warm driver cache and no memory pressure (four runs, no helper) nothing waited at the gate with the switch on or off, and fps was the same (15.8/17.2 off, 15.9/17.1 on): the change does nothing there.

Tests

5 new unit tests in compilemem.rs:

  • one waited on runs beside the running one under the soft floor, one at a time
  • the commit floor and a failed allocation still hold for it
  • nothing else starts while one waits
  • the switch off restores the old order
  • a worker's asked probe nests and resets

cargo test -p darner-gpu --lib compilemem: 18 passed. pipeline::: 125 passed.

Written with AI assistance.

Fixes the freezes in #70: a pass over the screen waiting seconds for its pipeline while the PC is short of free RAM. ### What was wrong Under the soft floor of free memory (`DARNER_MEMORY_FLOOR_MB`, 1024 MB) the gate in `compilemem.rs` lets one driver compile run at a time. Every other compile, the ones a frame is waiting on and the workers' early compiles alike, waits on the same condition variable, and after `notify_all` whichever thread takes the lock first starts. The workers' queue already puts a waited-on pipeline first (`Queue::expedite`) and raises its worker's priority (`boost`), but once that worker reaches the gate it has no precedence there. So a frame's pipeline could lose its turn to early compiles again and again, until the screen-wait cap (10 s). ### The change `DARNER_COMPILE_WAITED_FIRST` is on by default; `0` brings back the old behaviour. - A pipeline worker tells the gate whether a draw waits on the pipeline it makes (`compilemem::asked`: its `waiters`, or `kept_up`). Threads other than the workers count as waited on, as before. The gate asks again on every look, because a draw may come to wait while the compile is queued. A draw that starts waiting wakes the gate (`compilemem::nudge`). - While a compile a draw waits on is queued at the gate, no compile nobody waits on is let start. - Under the soft floor, a compile a draw waits on may run beside the one compile running, one such at a time. The commit floor (`DARNER_COMMIT_FLOOR_MB`, the hard limit) and the half second after a failed allocation still hold for it, so it adds at most one compile's memory under the soft floor, never under the hard one. - The gate's log line now also says how many compiles a draw waited on started, how often and how long one waited at the gate, and how many ran beside another. These figures are counted with the switch off too, for comparing. ### Measured RTX 3090 24 GB, driver 610.88, Ryzen 7 9800X3D, 32 GB, Windows 11 26200. Series X, 1440p, hardware RT, DLAA, DLSS FG 2x. A scripted run (keyboard walk from the save) of 5 min in the world each. Every run started from the same host pipeline library snapshot, with Darner's NVIDIA driver cache files emptied before each run (set aside and restored after), and a helper process holding 12 GB of RAM, which leaves free RAM about where #70's 32 GB PC had it. | run | `DARNER_COMPILE_WAITED_FIRST` | free RAM min / median | screen-pass waits > 0.5 s | their total | worst | waited-on compiles held at the gate | fps samples in play (5 s each) | |---|---|---|---|---|---|---|---| | a4 | 0 | 402 / 958 MB | 25 | 120.3 s | 10.0 s | 146 times, 654 s | 5 | | b4 | 1 | 365 / 3586 MB | 23 | 29.7 s | 2.6 s | 120 times, 95 s (108 ran beside) | 45 | | a5 | 0 | 498 / 1212 MB | 32 | 127.3 s | 10.0 s | 135 times, 415 s | 23 | | b5 | 1 | 586 / 1062 MB | 28 | 42.6 s | 4.9 s | 286 times, 323 s (121 ran beside) | 35 | - Off: 124 s of screen waits per run on average, both runs hitting the 10 s cap. - On: 36 s on average, the worst 2.6 to 4.9 s. The game spent more of the 5 minutes actually drawing. - No crash, no device loss and no out-of-memory in any run. Compiles put off for commit: 71 and 79 off, 17 and 138 on (b5 met more waited-on compiles). - b4 had more free RAM than the others (median 3.6 GB), so a5/b5 is the fairer pair: 127 s down to 43 s, worst 10 s down to 4.9 s. - With a warm driver cache and no memory pressure (four runs, no helper) nothing waited at the gate with the switch on or off, and fps was the same (15.8/17.2 off, 15.9/17.1 on): the change does nothing there. ### Tests 5 new unit tests in `compilemem.rs`: - one waited on runs beside the running one under the soft floor, one at a time - the commit floor and a failed allocation still hold for it - nothing else starts while one waits - the switch off restores the old order - a worker's `asked` probe nests and resets `cargo test -p darner-gpu --lib compilemem`: 18 passed. `pipeline::`: 125 passed. Written with AI assistance.
Contributor

Code review only (read the diff at 5e9513a), nothing built or run.

  • The gate logic holds up on reading. Every exit of begin() leaves the waited-on count, counted follows the waiter state while it waits, the worker's probe only reads atomics under the gate lock, and the commit floor and just-failed rule still hold for the compile let run beside. A nudge that races the wait is lost but bounded by the 200 ms look.
  • The trade to test: under the soft floor two compiles can now run instead of one. Commit is still capped, but free physical RAM can go lower. Your A/B covers a 32 GB PC with 12 GB held. One run on a 16 GB PC near the floor (where page-file exhaustion is reported) would show whether the extra compile tips it into paging.
  • With #367 (refused pipelines retried by workers only): the draw no longer waits, so waiters stays 0 and the worker's retry counts as unwaited here, and is held behind every waited-on compile. If both land, a pass refused under real memory pressure may stay missing longer. Maybe kept_up, or a "refused once" flag, could count as waited.

Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.

Code review only (read the diff at `5e9513a`), nothing built or run. - The gate logic holds up on reading. Every exit of `begin()` leaves the waited-on count, `counted` follows the waiter state while it waits, the worker's probe only reads atomics under the gate lock, and the commit floor and just-failed rule still hold for the compile let run beside. A `nudge` that races the wait is lost but bounded by the 200 ms look. - The trade to test: under the soft floor two compiles can now run instead of one. Commit is still capped, but free physical RAM can go lower. Your A/B covers a 32 GB PC with 12 GB held. One run on a 16 GB PC near the floor (where page-file exhaustion is reported) would show whether the extra compile tips it into paging. - With #367 (refused pipelines retried by workers only): the draw no longer waits, so `waiters` stays 0 and the worker's retry counts as unwaited here, and is held behind every waited-on compile. If both land, a pass refused under real memory pressure may stay missing longer. Maybe `kept_up`, or a "refused once" flag, could count as waited. Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'compile-gate-waited-first:tommy66-compile-gate-waited-first'
git switch 'tommy66-compile-gate-waited-first'

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 'tommy66-compile-gate-waited-first'
git switch 'tommy66-compile-gate-waited-first'
git rebase 'main'
git switch 'main'
git merge --ff-only 'tommy66-compile-gate-waited-first'
git switch 'tommy66-compile-gate-waited-first'
git rebase 'main'
git switch 'main'
git merge --no-ff 'tommy66-compile-gate-waited-first'
git switch 'main'
git merge --squash 'tommy66-compile-gate-waited-first'
git switch 'main'
git merge --ff-only 'tommy66-compile-gate-waited-first'
git switch 'main'
git merge 'tommy66-compile-gate-waited-first'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
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
TrustNo1/Darner!350
No description provided.