[pr-performance][memory] Drain retirement queues without walking entries that must wait #274

Open
MystifiedSky wants to merge 1 commit from MystifiedSky/Darner:fix/retirement-queue-walk into main
First-time contributor

Contributor role (optional)

  • Fluxer ID: MystifiedSky#6443

What it changes

  • The problem on main: a CPU trace of darner-late-wait had 752 of its 1,098 samples (4 s window) in the queue walk of host::bury (select_ready, inlined).

Since 412a0ec (#81), a retirement queue with one out-of-order entry is rotated whole on every drain, with the shard's lock held. Objects wait 64 submissions, so the queues are long. Timelines also showed darner-relay blocked in retire_resource behind darner-late-wait (about 6 and 9 ms a frame in two of three 4-frame samples).

Each shard now keeps its entries by submission number (BTreeMap<u64, Vec<T>>). A drain visits only the entries it releases. #81's behaviour is kept: an entry behind a later one is still released as soon as its own number has finished.

Two differences: released entries now come out ordered by number (arrival order within one) rather than by arrival, and DARNER_RETIRE_ORDER_DIAG is removed with its four tests, because it counted ready entries hidden behind the head of a queue, which can no longer happen.

How it was tested

  • Tested revision: d21753c (main f3840d7 plus this change). The game runs used a build of the same code before it was committed; only a code comment differs.

  • Commands and results: cargo test -p darner-gpu --lib -- bury_tests: 5 passed, also with --features diagnostics. cargo xtask policy --staged: ok. Test failures are listed under the gates.

  • Changes a default: no switch or value changes. The code path changes for everyone.

  • A/B result: one saved outdoor scene, camera turning at a constant rate by input replay, 60 s warm-up then 120 s measured, old and new alternated (old, new, new, old, old, new), three runs each. Same machine and settings, shaders already cached. Frame rates are rendered frames, not generated ones.

    Per run (3 runs each) A: main f3840d7 B: with this change
    Average fps 24.0 / 24.4 / 25.0 27.7 / 27.0 / 25.9
    Median frame ms 39.7 / 39.7 / 39.2 35.7 / 36.8 / 38.3
    95th percentile ms 58.6 / 55.0 / 51.4 50.1 / 49.3 / 47.1
    99th percentile ms 104.3 / 70.6 / 65.7 58.2 / 56.7 / 57.5
    1% low fps 8.5 / 12.2 / 13.6 15.8 / 16.1 / 15.2
    Copy-back ms a frame 17.0 / 16.8 / 17.2 9.4 / 9.3 / 8.5
    Preparing ms a frame 22.2 / 21.7 / 22.0 19.2 / 19.4 / 19.3

    Mean frame time grouped by draw count (quartiles), averaged over each side's runs: 33.0 → 31.3, 38.2 → 35.8, 40.1 → 38.2, 45.0 → 40.7 ms. Not every B run beats every A run in every group.

    CPU trace, share of the game's samples passing through host::bury: 2.1% before (772 of 36,179), 0.14% after (79 of 54,507). The "before" trace had procdump attached and the "after" one did not, so treat the comparison as approximate. The A/B runs above had no procdump.

  • Gates run on that revision: the clippy and test runs used the tree of this commit.

    • cargo clippy --workspace --all-targets --locked -- -D warnings: passed.
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passed.
    • cargo test --workspace --locked (with --no-fail-fast): not clean on this machine. One complete run: 3336 passed, 4 failed, 121 ignored. A second run hung in the watch tests; its thread stacks showed no thread in the retirement code.
    • Of those 4, three also fail on main f3840d7: sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders (DXIL validator), shared_scratch_eliminates_flight_settle_end_to_end, ask_once_by_res_splits_reservations_without_refusals.
    • The fourth, upload::tests::the_pool_is_reserved_in_the_window_and_handed_out_once, was not seen on main. It asserts on a process-wide pool in upload.rs, which this change does not touch. descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync also failed once with the change in an earlier run and was not seen on main.
    • main f3840d7 is flaky here too: its darner-gpu lib tests failed in 7 of 8 runs with varying tests, and its workspace run did not finish within 7 minutes.
  • Gameplay: RTX 5090 (driver 621.24), Ryzen 9 9950X3D, 64 GB, Windows 11; Series X profile, 4K, DLSS ultra performance, 6x DLSS frame generation, hardware ray tracing. No errors or device removal in the six logs.

  • Shader wave sizes tested: not applicable.

  • Hardware dependence: likely general (CPU-side bookkeeping); measured on one machine only.

  • Coverage gaps:

    • One scene, standing still with the camera turning. No walking or driving, and no long session.
    • GPU memory rose at a similar rate in A and B during the runs; one B run sat about 1 GB higher throughout, which the other two did not.
    • The timelines were taken before the change only, and with procdump attached, as was the first CPU trace.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #81 (introduced the walk this removes). 31 open PRs touch host.rs; none changes the retirement queue.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: MystifiedSky#6443 ## What it changes - The problem on main: a CPU trace of `darner-late-wait` had 752 of its 1,098 samples (4 s window) in the queue walk of `host::bury` (`select_ready`, inlined). Since 412a0ec (#81), a retirement queue with one out-of-order entry is rotated whole on every drain, with the shard's lock held. Objects wait 64 submissions, so the queues are long. Timelines also showed `darner-relay` blocked in `retire_resource` behind `darner-late-wait` (about 6 and 9 ms a frame in two of three 4-frame samples). Each shard now keeps its entries by submission number (`BTreeMap<u64, Vec<T>>`). A drain visits only the entries it releases. #81's behaviour is kept: an entry behind a later one is still released as soon as its own number has finished. Two differences: released entries now come out ordered by number (arrival order within one) rather than by arrival, and `DARNER_RETIRE_ORDER_DIAG` is removed with its four tests, because it counted ready entries hidden behind the head of a queue, which can no longer happen. ## How it was tested - Tested revision: d21753c (main f3840d7 plus this change). The game runs used a build of the same code before it was committed; only a code comment differs. - Commands and results: `cargo test -p darner-gpu --lib -- bury_tests`: 5 passed, also with `--features diagnostics`. `cargo xtask policy --staged`: ok. Test failures are listed under the gates. - Changes a default: no switch or value changes. The code path changes for everyone. - A/B result: one saved outdoor scene, camera turning at a constant rate by input replay, 60 s warm-up then 120 s measured, old and new alternated (old, new, new, old, old, new), three runs each. Same machine and settings, shaders already cached. Frame rates are rendered frames, not generated ones. | Per run (3 runs each) | A: main f3840d7 | B: with this change | |---|---|---| | Average fps | 24.0 / 24.4 / 25.0 | 27.7 / 27.0 / 25.9 | | Median frame ms | 39.7 / 39.7 / 39.2 | 35.7 / 36.8 / 38.3 | | 95th percentile ms | 58.6 / 55.0 / 51.4 | 50.1 / 49.3 / 47.1 | | 99th percentile ms | 104.3 / 70.6 / 65.7 | 58.2 / 56.7 / 57.5 | | 1% low fps | 8.5 / 12.2 / 13.6 | 15.8 / 16.1 / 15.2 | | Copy-back ms a frame | 17.0 / 16.8 / 17.2 | 9.4 / 9.3 / 8.5 | | Preparing ms a frame | 22.2 / 21.7 / 22.0 | 19.2 / 19.4 / 19.3 | Mean frame time grouped by draw count (quartiles), averaged over each side's runs: 33.0 → 31.3, 38.2 → 35.8, 40.1 → 38.2, 45.0 → 40.7 ms. Not every B run beats every A run in every group. CPU trace, share of the game's samples passing through `host::bury`: 2.1% before (772 of 36,179), 0.14% after (79 of 54,507). The "before" trace had procdump attached and the "after" one did not, so treat the comparison as approximate. The A/B runs above had no procdump. - Gates run on that revision: the clippy and test runs used the tree of this commit. - `cargo clippy --workspace --all-targets --locked -- -D warnings`: passed. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passed. - `cargo test --workspace --locked` (with `--no-fail-fast`): **not clean on this machine.** One complete run: 3336 passed, 4 failed, 121 ignored. A second run hung in the `watch` tests; its thread stacks showed no thread in the retirement code. - Of those 4, three also fail on main f3840d7: `sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders` (DXIL validator), `shared_scratch_eliminates_flight_settle_end_to_end`, `ask_once_by_res_splits_reservations_without_refusals`. - The fourth, `upload::tests::the_pool_is_reserved_in_the_window_and_handed_out_once`, was not seen on main. It asserts on a process-wide pool in `upload.rs`, which this change does not touch. `descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync` also failed once with the change in an earlier run and was not seen on main. - main f3840d7 is flaky here too: its `darner-gpu` lib tests failed in 7 of 8 runs with varying tests, and its workspace run did not finish within 7 minutes. - Gameplay: RTX 5090 (driver 621.24), Ryzen 9 9950X3D, 64 GB, Windows 11; Series X profile, 4K, DLSS ultra performance, 6x DLSS frame generation, hardware ray tracing. No errors or device removal in the six logs. - Shader wave sizes tested: not applicable. - Hardware dependence: likely general (CPU-side bookkeeping); measured on one machine only. - Coverage gaps: - One scene, standing still with the camera turning. No walking or driving, and no long session. - GPU memory rose at a similar rate in A and B during the runs; one B run sat about 1 GB higher throughout, which the other two did not. - The timelines were taken before the change only, and with procdump attached, as was the first CPU trace. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #81 (introduced the walk this removes). 31 open PRs touch `host.rs`; none changes the retirement queue. - Replaces or overlaps: none found
Since 412a0ec a queue with one out-of-order entry is rotated whole on every drain, holding the shard's lock. Keep entries by submission number instead: a drain visits only what it releases, and entries behind a later one are still released when they may be.

The DARNER_RETIRE_ORDER_DIAG sample counted entries hidden behind the head of a queue, which cannot happen now, so it and its tests are removed.
First-time contributor

Independent Windows build, gate, and code-read review of d21753ca9 (based on main f3840d73d). Not run in-game.

Environment

  • Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (driver 621.47), Rust 1.98.1 (x86_64-pc-windows-msvc), MSVC 19.44.35230.

Measured results

  • Gate 1 (cargo clippy --workspace --all-targets --locked -- -D warnings): PASS
  • Gate 2 (cargo clippy --workspace --all-targets --all-features --locked -- -D warnings): PASS
  • cargo xtask policy: PASS (asset policy: 555 files ok)
  • cargo test --locked -p darner-gpu --lib host::tests: PASS (123 passed; 0 failed; 3 ignored in 2.38 s)
  • cargo test --locked -p darner-gpu --lib retire: PASS (13 passed; 0 failed in 0.47 s, including host::bury_tests::retirement_queue_moves_objects_without_dropping_them, retirement_queue_releases_up_to_the_threshold_by_number_then_arrival, retirement_queue_matches_partition_for_small_sequences, and bury_drops_retired_objects_asynchronously)

Code-read audit

  • Out-of-order safety: Replacing VecDeque<(u64, T)> + ordered with BTreeMap<u64, Vec<T>> (crates/darner-gpu/src/host.rs:1248-1297) handles non-monotonic retirement numbers cleanly (both multi-thread pushers sampling STARTED + queued before locking a shard at host.rs:1170-1197 and QUEUED moving on Job::Present/unfold). select_ready iterates first_entry() and breaks only when *first.key() > finished, so no ready item can be blocked behind a later submission number.
  • No drops under shard lock: select_ready moves every T via objects.into_iter() into done: &mut Vec<(u64, T)>, keeping all Retired::drop() / COM Release calls outside every shard lock (handed off to BuryPool or dropped after unlocking at host.rs:1384-1397).
  • Drop order & cleanup: Ordering ready items by (submission_number, arrival_order) breaks no invariants (callers are already sharded across RETIRED_SHARDS = 16 and dependent pairs are bundled in tuples). Verified zero remaining references to removed DARNER_RETIRE_ORDER_DIAG across the tree.

Verdict: PASS.

AI-assisted: yes (Gemini 4 Neon).

Independent Windows build, gate, and code-read review of `d21753ca9` (based on `main` `f3840d73d`). Not run in-game. ### Environment - Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (driver 621.47), Rust 1.98.1 (`x86_64-pc-windows-msvc`), MSVC 19.44.35230. ### Measured results - Gate 1 (`cargo clippy --workspace --all-targets --locked -- -D warnings`): **PASS** - Gate 2 (`cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`): **PASS** - `cargo xtask policy`: **PASS** (`asset policy: 555 files ok`) - `cargo test --locked -p darner-gpu --lib host::tests`: **PASS** (`123 passed; 0 failed; 3 ignored` in 2.38 s) - `cargo test --locked -p darner-gpu --lib retire`: **PASS** (`13 passed; 0 failed` in 0.47 s, including `host::bury_tests::retirement_queue_moves_objects_without_dropping_them`, `retirement_queue_releases_up_to_the_threshold_by_number_then_arrival`, `retirement_queue_matches_partition_for_small_sequences`, and `bury_drops_retired_objects_asynchronously`) ### Code-read audit - **Out-of-order safety:** Replacing `VecDeque<(u64, T)> + ordered` with `BTreeMap<u64, Vec<T>>` (`crates/darner-gpu/src/host.rs:1248-1297`) handles non-monotonic retirement numbers cleanly (both multi-thread pushers sampling `STARTED + queued` before locking a shard at `host.rs:1170-1197` and `QUEUED` moving on `Job::Present`/`unfold`). `select_ready` iterates `first_entry()` and breaks only when `*first.key() > finished`, so no ready item can be blocked behind a later submission number. - **No drops under shard lock:** `select_ready` moves every `T` via `objects.into_iter()` into `done: &mut Vec<(u64, T)>`, keeping all `Retired::drop()` / COM `Release` calls outside every shard lock (handed off to `BuryPool` or dropped after unlocking at `host.rs:1384-1397`). - **Drop order & cleanup:** Ordering ready items by `(submission_number, arrival_order)` breaks no invariants (callers are already sharded across `RETIRED_SHARDS = 16` and dependent pairs are bundled in tuples). Verified zero remaining references to removed `DARNER_RETIRE_ORDER_DIAG` across the tree. **Verdict:** PASS. AI-assisted: yes (Gemini 4 Neon).
Contributor

Test report from another PC, in case it helps the review.

I ran into the same thing from the other side: counters of my own showed the retirement queues walking 3.6 to 4.3 million entries a frame while standing still in the open world, nearly all of them entries that had to wait. I had a fix of my own half done when I found this PR, so I dropped it and tested this one instead.

Setup: Ryzen 7 5800X3D (8 cores, 16 threads), RTX 5080, 32 GB, Windows 11. scripts\play-nvidia.ps1 -NoMenu -Dlss ultraperformance (Series X profile, 2560x1440 output, no ray tracing, no frame generation), standing still in the open world on a daytime save, copied fresh from a backup for each run, 45 s to settle and 120 s measured. Release builds. Tested: this PR's commit (the same diff as d21753c) on my local tree, which is main f3840d7 plus #216 and local changes that are off by default and do not touch the retirement queues. "Base" below is that tree without this PR.

base with this PR
darner-late-wait CPU time (ms a second, 2 runs each) 518 to 568 194 to 206
errors, device losses 0 0

So the thread that drains the queues does about 0.35 of a core less work here, and the game ran as before (loaded, stood in the world, no errors in the log).

What I could not show: a frame-time difference. On this PC the frame time in that scene moves by more between runs than the change would make, and the first runs of a new exe are slower here for a reason that has nothing to do with the change (the driver's background threads recompile shaders for the new exe for several runs), so my run-against-run numbers are not usable either way. An in-run A/B behind a switch would settle it; I have not built one for this PR.

Not tested: driving, cutscenes, ray tracing, other hardware.

Test report from another PC, in case it helps the review. I ran into the same thing from the other side: counters of my own showed the retirement queues walking 3.6 to 4.3 million entries a frame while standing still in the open world, nearly all of them entries that had to wait. I had a fix of my own half done when I found this PR, so I dropped it and tested this one instead. Setup: Ryzen 7 5800X3D (8 cores, 16 threads), RTX 5080, 32 GB, Windows 11. `scripts\play-nvidia.ps1 -NoMenu -Dlss ultraperformance` (Series X profile, 2560x1440 output, no ray tracing, no frame generation), standing still in the open world on a daytime save, copied fresh from a backup for each run, 45 s to settle and 120 s measured. Release builds. Tested: this PR's commit (the same diff as `d21753c`) on my local tree, which is main `f3840d7` plus #216 and local changes that are off by default and do not touch the retirement queues. "Base" below is that tree without this PR. | | base | with this PR | |---|---|---| | `darner-late-wait` CPU time (ms a second, 2 runs each) | 518 to 568 | 194 to 206 | | errors, device losses | 0 | 0 | So the thread that drains the queues does about 0.35 of a core less work here, and the game ran as before (loaded, stood in the world, no errors in the log). What I could not show: a frame-time difference. On this PC the frame time in that scene moves by more between runs than the change would make, and the first runs of a new exe are slower here for a reason that has nothing to do with the change (the driver's background threads recompile shaders for the new exe for several runs), so my run-against-run numbers are not usable either way. An in-run A/B behind a switch would settle it; I have not built one for this PR. Not tested: driving, cutscenes, ray tracing, other hardware.
Contributor

Code review only (read the diff at d21753c), nothing built or run. I agree with the earlier audit. A few more checks, all fine:

  • Release timing is unchanged. An object is released only once its own submission number has finished, as on main, so nothing can be freed earlier. Only the order among already-safe objects changes (number, then arrival), and all of them are reference-counted objects whose GPU work has completed.
  • RetirementQueue and select_ready have no users outside the bury code. DARNER_RETIRE_ORDER_DIAG has no README row, menu item or script reference on main, so removing it leaves nothing dangling.
  • Each push is now an O(log n) map lookup, a good trade for the removed whole-queue rotation under the shard lock.
  • The frame-rate table is from one PC. The second PC's report above shows the CPU saving but not a frame-time change, so a same-run A/B would be the way to confirm the fps part.

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

Code review only (read the diff at `d21753c`), nothing built or run. I agree with the earlier audit. A few more checks, all fine: - Release timing is unchanged. An object is released only once its own submission number has finished, as on `main`, so nothing can be freed earlier. Only the order among already-safe objects changes (number, then arrival), and all of them are reference-counted objects whose GPU work has completed. - `RetirementQueue` and `select_ready` have no users outside the bury code. `DARNER_RETIRE_ORDER_DIAG` has no README row, menu item or script reference on `main`, so removing it leaves nothing dangling. - Each push is now an O(log n) map lookup, a good trade for the removed whole-queue rotation under the shard lock. - The frame-rate table is from one PC. The second PC's report above shows the CPU saving but not a frame-time change, so a same-run A/B would be the way to confirm the fps part. Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.
Contributor

Testing this now in-game against main (d21753c vs f3840d7, both release builds with bench-remote).

Setup: RTX 5070 Ti 16 GB, driver 621.47, Ryzen 7 7800X3D, 31 GB RAM, Series X profile, 1080p upscaled to 1440p with DLSS Quality, DLSS frame generation 2x, hardware RT on. Automated camera flight over the city (same route and save every run, own home/data folders, both builds on the same warm shader cache since the translator is unchanged).

Plan: 6 alternated runs (main, PR, main, PR, main, PR). I'll post fps / p99 / long frames when it's done.

Testing this now in-game against main (d21753c vs f3840d7, both release builds with bench-remote). Setup: RTX 5070 Ti 16 GB, driver 621.47, Ryzen 7 7800X3D, 31 GB RAM, Series X profile, 1080p upscaled to 1440p with DLSS Quality, DLSS frame generation 2x, hardware RT on. Automated camera flight over the city (same route and save every run, own home/data folders, both builds on the same warm shader cache since the translator is unchanged). Plan: 6 alternated runs (main, PR, main, PR, main, PR). I'll post fps / p99 / long frames when it's done.
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 'fix/retirement-queue-walk:MystifiedSky-fix/retirement-queue-walk'
git switch 'MystifiedSky-fix/retirement-queue-walk'

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 'MystifiedSky-fix/retirement-queue-walk'
git switch 'MystifiedSky-fix/retirement-queue-walk'
git rebase 'main'
git switch 'main'
git merge --ff-only 'MystifiedSky-fix/retirement-queue-walk'
git switch 'MystifiedSky-fix/retirement-queue-walk'
git rebase 'main'
git switch 'main'
git merge --no-ff 'MystifiedSky-fix/retirement-queue-walk'
git switch 'main'
git merge --squash 'MystifiedSky-fix/retirement-queue-walk'
git switch 'main'
git merge --ff-only 'MystifiedSky-fix/retirement-queue-walk'
git switch 'main'
git merge 'MystifiedSky-fix/retirement-queue-walk'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
5 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!274
No description provided.