[pr-fix][memory][RAYTRACING] hwrt: segregate BLAS heaps by size class, prefer fullest-fit, and release empty heaps #318

Open
Deyzrakc wants to merge 2 commits from Deyzrakc/Darner:fix/blas-heap-fragmentation into main
First-time contributor

Refs #290

Contributor role (optional)

  • Fluxer ID: 1574516750119079936

What it changes

  • The problem on main: During extended driving with Hardware RT enabled, hwrt places all BLAS allocations into a single shared pool of 64 MiB ID3D12Heaps using a first-fit scan and never releases empty heaps. On main (f3840d7), two city driving runs ended with:
    • Run A1: 1,851 MB BLAS in use across 3,072 MB allocated (48 × 64 MiB heaps) → 1,221 MB (39.7%) empty fragmentation waste
    • Run A2: 1,799 MB BLAS in use across 3,264 MB allocated (51 × 64 MiB heaps) → 1,465 MB (44.9%) empty fragmentation waste

This change updates crates/darner-gpu/src/hwrt.rs to:

  1. Segregate BLAS heaps into 4 size classes so tiny BLASes do not fragment contiguous runs needed by larger BLASes:
    • Class 0: 1 unit (≤ 64 KiB), 16 MiB heaps, with a trailing_zeros single-unit fast path
    • Class 1: 2..=8 units (128–512 KiB), 16 MiB heaps
    • Class 2: 9..=32 units (576 KiB–2 MiB), 16 MiB heaps
    • Class 3: 33..=256 units (> 2–16 MiB), 32 MiB heaps
  2. Prefer fullest-fit allocation via a lock-free free_units: AtomicUsize scan per heap. Packing nearly full heaps first lets LRU-draining heaps finish emptying instead of being refilled, while keeping empty spare heaps untouched until needed.
  3. Release empty heaps past BLAS_HEAP_SPARE = 1 per class when SlotReturn::drop returns the last slot of a heap. Lock ordering is always blas_heaps -> heap.used, and the ID3D12Heap is dropped outside the pool lock.
  4. Report allocated heap MB and released heap count in blas_heap_line().
  5. Add 4 unit tests covering size-class segregation, fullest-fit packing, empty-heap release past the spare, and bounded overhead under LRU driving churn.

How it was tested

  • Tested revision: 9e6c112 (based on origin/main f3840d7)

  • Commands and results:

    • cargo test --locked --offline -p darner-gpu --lib -- hwrt:: → 29 passed; 0 failed (including the 4 new BLAS heap tests)
    • cargo build --release --locked --offline -p darner --bin darner → passed
  • Changes a default: yes, for all setups using Hardware RT (hwrt BLAS placed-resource heap allocation).

  • A/B result (ABBA order: A1, B1, B2, A2 — 4 runs on the same city driving route, shared pre-warmed v2026.12.03.02 cache of 76,582 pipelines, 2 GiB nv-profile, 0 errors in all 4 runs):

    Metric A1 (f3840d7) A2 (f3840d7) B1 (9e6c112) B2 (9e6c112) Avg A → B
    BLAS in use 1,851 MB 1,799 MB 1,867 MB 1,892 MB +55 MB
    BLAS heaps allocated 3,072 MB (48 heaps) 3,264 MB (51 heaps) 2,320 MB (123 heaps, 3 released) 2,288 MB (122 heaps, 0 released) −864 MB (−27.3%)
    Empty BLAS heap waste 1,221 MB (39.7%) 1,465 MB (44.9%) 453 MB (19.5%) 396 MB (17.3%) −918 MB (−68%)
    Peak local VRAM 13,652 MB 13,670 MB 12,667 MB 12,799 MB −928 MB
    Avg BLAS creation time 25.7 µs 28.8 µs 20.7 µs 20.3 µs −24.8%
    Median base fps 20.4 fps 18.5 fps 20.7 fps 21.0 fps +1.4 fps
  • Gates run on that revision:

    • cargo clippy --workspace --all-targets --locked --offline -- -D warnings -A deprecated → passed (-A deprecated used only for pre-existing Atomic::fetch_update deprecation warnings in untouched files on local rustc 1.100.0 vs pinned 1.98.1)
    • cargo clippy --workspace --all-targets --all-features --locked --offline -- -D warnings -A deprecated → passed
    • cargo xtask policy → asset policy: 555 files ok
  • Gameplay: NVIDIA GeForce RTX 5070 Ti (16 GB VRAM), Xbox Series X profile, 1440p, DLSS Balanced, 2x DLSS Frame Generation, High textures, Hardware Ray Tracing enabled, 2 GiB NVIDIA shader heap profile (darner.exe nv-profile --mb 2048).

  • Shader wave sizes tested: N/A (CPU/D3D12 heap allocator only; no shader changes).

  • Hardware dependence: likely general across all D3D12 GPUs using Hardware RT (hwrt); only benchmarked on RTX 5070 Ti 16 GB.

  • Coverage gaps: Not tested on AMD/Intel GPUs or with pinned rustc 1.98.1 (tested with rustc 1.100.0).

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: #268, #279 (also touch crates/darner-gpu/src/hwrt.rs, unrelated TLAS/Backlog/MeshletCache code)
  • Replaces or overlaps: none found
Refs #290 ## Contributor role (optional) - Fluxer ID: 1574516750119079936 ## What it changes - The problem on main: During extended driving with Hardware RT enabled, `hwrt` places all BLAS allocations into a single shared pool of 64 MiB `ID3D12Heap`s using a first-fit scan and never releases empty heaps. On `main` (`f3840d7`), two city driving runs ended with: - Run A1: `1,851 MB` BLAS in use across `3,072 MB` allocated (`48 × 64 MiB` heaps) → `1,221 MB` (`39.7%`) empty fragmentation waste - Run A2: `1,799 MB` BLAS in use across `3,264 MB` allocated (`51 × 64 MiB` heaps) → `1,465 MB` (`44.9%`) empty fragmentation waste This change updates `crates/darner-gpu/src/hwrt.rs` to: 1. **Segregate BLAS heaps into 4 size classes** so tiny BLASes do not fragment contiguous runs needed by larger BLASes: - Class 0: `1` unit (`≤ 64 KiB`), `16 MiB` heaps, with a `trailing_zeros` single-unit fast path - Class 1: `2..=8` units (`128–512 KiB`), `16 MiB` heaps - Class 2: `9..=32` units (`576 KiB–2 MiB`), `16 MiB` heaps - Class 3: `33..=256` units (`> 2–16 MiB`), `32 MiB` heaps 2. **Prefer fullest-fit allocation** via a lock-free `free_units: AtomicUsize` scan per heap. Packing nearly full heaps first lets LRU-draining heaps finish emptying instead of being refilled, while keeping empty spare heaps untouched until needed. 3. **Release empty heaps past `BLAS_HEAP_SPARE = 1` per class** when `SlotReturn::drop` returns the last slot of a heap. Lock ordering is always `blas_heaps -> heap.used`, and the `ID3D12Heap` is dropped outside the pool lock. 4. **Report allocated heap MB and released heap count** in `blas_heap_line()`. 5. **Add 4 unit tests** covering size-class segregation, fullest-fit packing, empty-heap release past the spare, and bounded overhead under LRU driving churn. ## How it was tested - Tested revision: `9e6c112` (based on `origin/main` `f3840d7`) - Commands and results: - `cargo test --locked --offline -p darner-gpu --lib -- hwrt::` → **29 passed; 0 failed** (including the 4 new BLAS heap tests) - `cargo build --release --locked --offline -p darner --bin darner` → **passed** - Changes a default: yes, for all setups using Hardware RT (`hwrt` BLAS placed-resource heap allocation). - A/B result (ABBA order: A1, B1, B2, A2 — 4 runs on the same city driving route, shared pre-warmed `v2026.12.03.02` cache of 76,582 pipelines, 2 GiB `nv-profile`, 0 errors in all 4 runs): | Metric | A1 (`f3840d7`) | A2 (`f3840d7`) | B1 (`9e6c112`) | B2 (`9e6c112`) | Avg A → B | |---|---|---|---|---|---| | BLAS in use | 1,851 MB | 1,799 MB | 1,867 MB | 1,892 MB | +55 MB | | BLAS heaps allocated | 3,072 MB (48 heaps) | 3,264 MB (51 heaps) | 2,320 MB (123 heaps, 3 released) | 2,288 MB (122 heaps, 0 released) | **−864 MB (−27.3%)** | | Empty BLAS heap waste | 1,221 MB (39.7%) | 1,465 MB (44.9%) | 453 MB (19.5%) | 396 MB (17.3%) | **−918 MB (−68%)** | | Peak local VRAM | 13,652 MB | 13,670 MB | 12,667 MB | 12,799 MB | **−928 MB** | | Avg BLAS creation time | 25.7 µs | 28.8 µs | 20.7 µs | 20.3 µs | **−24.8%** | | Median base fps | 20.4 fps | 18.5 fps | 20.7 fps | 21.0 fps | **+1.4 fps** | - Gates run on that revision: - `cargo clippy --workspace --all-targets --locked --offline -- -D warnings -A deprecated` → **passed** (`-A deprecated` used only for pre-existing `Atomic::fetch_update` deprecation warnings in untouched files on local `rustc 1.100.0` vs pinned `1.98.1`) - `cargo clippy --workspace --all-targets --all-features --locked --offline -- -D warnings -A deprecated` → **passed** - `cargo xtask policy` → **`asset policy: 555 files ok`** - Gameplay: NVIDIA GeForce RTX 5070 Ti (16 GB VRAM), Xbox Series X profile, 1440p, DLSS Balanced, 2x DLSS Frame Generation, High textures, Hardware Ray Tracing enabled, 2 GiB NVIDIA shader heap profile (`darner.exe nv-profile --mb 2048`). - Shader wave sizes tested: N/A (CPU/D3D12 heap allocator only; no shader changes). - Hardware dependence: likely general across all D3D12 GPUs using Hardware RT (`hwrt`); only benchmarked on RTX 5070 Ti 16 GB. - Coverage gaps: Not tested on AMD/Intel GPUs or with pinned `rustc 1.98.1` (tested with `rustc 1.100.0`). ## 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: #268, #279 (also touch `crates/darner-gpu/src/hwrt.rs`, unrelated TLAS/Backlog/MeshletCache code) - Replaces or overlaps: none found
First-time contributor

Independent Windows build, gate, and code-read review of head 9e6c1120c02d7855c6b3497ca92782ea3d6944be, based on main f3840d73d3504c30218debfbc8795b9225447317. 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 hwrt::: PASS (29 passed; 0 failed; 0 ignored).
  • No in-game or hardware RT runtime check was performed in this review. The PR description reports an A/B run; I did not independently reproduce it.

Code-read audit

  • first_free_run's one-unit path searches only the active bitmap words; try_take rechecks available units and contiguous space under used before marking and publishing the updated free count (crates/darner-gpu/src/hwrt.rs:2533-2627). Class-specific capacities are multiples of 64 units, and take_existing treats the atomic count as a candidate hint, with the bitmap mutex as the allocator's final authority (hwrt.rs:2643-2694).
  • take_existing searches the correct size class, prefers the partially used heap with the least free space that fits, retries other partially used heaps when fragmentation blocks a run, and only then consumes an empty spare (hwrt.rs:2643-2694). create_blas_buffer bounds placement to 16 MiB and falls back to committed resources if placement fails (hwrt.rs:2896-2958).
  • SlotReturn::drop releases the per-heap lock before taking the pool lock; pool operations consistently use blas_heaps -> heap.used. Empty heaps beyond one spare are removed under the pool lock, while the retained Arcs are dropped outside it (hwrt.rs:2717-2776, 2787-2795). The resource's private-data interface retains the slot return until the resource itself is released (hwrt.rs:2923-2958).
  • The added tests cover class boundaries, fullest-fit behavior, spare retention/release, and synthetic LRU churn (hwrt.rs:6246-6431). These validate allocator bookkeeping, not actual D3D12 heap behavior or the reported VRAM/performance effect.

No blocking issue found.

Verdict: PASS.

AI-assisted: yes (GitHub Copilot, Auto mode).

Independent Windows build, gate, and code-read review of head `9e6c1120c02d7855c6b3497ca92782ea3d6944be`, based on `main` `f3840d73d3504c30218debfbc8795b9225447317`. 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 hwrt::`: **PASS** (`29 passed; 0 failed; 0 ignored`). - No in-game or hardware RT runtime check was performed in this review. The PR description reports an A/B run; I did not independently reproduce it. ### Code-read audit - `first_free_run`'s one-unit path searches only the active bitmap words; `try_take` rechecks available units and contiguous space under `used` before marking and publishing the updated free count (`crates/darner-gpu/src/hwrt.rs:2533-2627`). Class-specific capacities are multiples of 64 units, and `take_existing` treats the atomic count as a candidate hint, with the bitmap mutex as the allocator's final authority (`hwrt.rs:2643-2694`). - `take_existing` searches the correct size class, prefers the partially used heap with the least free space that fits, retries other partially used heaps when fragmentation blocks a run, and only then consumes an empty spare (`hwrt.rs:2643-2694`). `create_blas_buffer` bounds placement to 16 MiB and falls back to committed resources if placement fails (`hwrt.rs:2896-2958`). - `SlotReturn::drop` releases the per-heap lock before taking the pool lock; pool operations consistently use `blas_heaps -> heap.used`. Empty heaps beyond one spare are removed under the pool lock, while the retained `Arc`s are dropped outside it (`hwrt.rs:2717-2776`, `2787-2795`). The resource's private-data interface retains the slot return until the resource itself is released (`hwrt.rs:2923-2958`). - The added tests cover class boundaries, fullest-fit behavior, spare retention/release, and synthetic LRU churn (`hwrt.rs:6246-6431`). These validate allocator bookkeeping, not actual D3D12 heap behavior or the reported VRAM/performance effect. No blocking issue found. **Verdict: PASS.** AI-assisted: yes (GitHub Copilot, Auto mode).
Contributor

In-game A/B on an RTX 5080 16 GB: same settings, warm caches, one pair.

Setup: RTX 5080 16 GB, driver 621.40; Ryzen 7 9800X3D, 64 GB, Windows 11 (26300). Series X, 2560x1440, DLAA, DLSS frame generation 2x, hardware RT, darner nv-profile --mb 2048, default BLAS budget (1,490-1,497 MB from the logs). Release builds. A = f3840d7 (built from a fork whose only other changes are a launcher script and docs, nothing in darner.exe); B = A plus this PR (9e6c112). Same save and driving route by hand. An earlier pair started with a cold driver shader cache for A and is left out.

A (f3840d7) B (this PR)
time in the world 6.2 min 5.3 min
BLAS heaps at minute 5, allocated / in use 4,160 / 1,726 MB (65 × 64 MB) 2,256 / 1,880 MB (11 released)
BLAS heaps at the end, allocated / in use 4,160 / 2,230 MB 2,272 / 2,062 MB
live BLAS at minute 5 1,430 MB 1,426 MB
DXGI local in use / budget at minute 5 17,594 / 14,918 MB 15,217 / 14,912 MB
ledger "unaccounted" at minute 5 5,908 MB 4,018 MB
median frame time (world windows) 44.3 ms (22.6 fps) 39.7 ms (25.2 fps)
frames over 100 ms 159 (25.7 a minute) 38 (7.2 a minute)
longest frame in the world 409 ms 220 ms
BLAS buffer creation 28.0 µs each 13.9 µs each

By eye: distant objects disappeared for a moment more often in A. No new visual problems in B. Neither run lost the device (both were about 6 minutes, so no claim about device losses).

What this does not show: one pair, driven by hand, of different lengths, so part of the frame-time difference may be the route. The card being 2.7 GB over its budget in A against 0.3 GB in B may explain part of it.

One note from the logs: the PR keeps cardledger::retag(&heap, Kind::AccelStructure) after CreateHeap. retag only changes the kind of an allocation the ledger already counts, so the BLAS heaps stay out of it (the gap noted in #290). The "unaccounted" row above moves by about the heap difference. cardledger::heap(&heap, Kind::AccelStructure) would count them, as alias_rt and pagebuf do.

🤖 Generated with Claude Code

In-game A/B on an RTX 5080 16 GB: same settings, warm caches, one pair. **Setup:** RTX 5080 16 GB, driver 621.40; Ryzen 7 9800X3D, 64 GB, Windows 11 (26300). Series X, 2560x1440, DLAA, DLSS frame generation 2x, hardware RT, `darner nv-profile --mb 2048`, default BLAS budget (1,490-1,497 MB from the logs). Release builds. A = `f3840d7` (built from a fork whose only other changes are a launcher script and docs, nothing in darner.exe); B = A plus this PR (`9e6c112`). Same save and driving route by hand. An earlier pair started with a cold driver shader cache for A and is left out. | | A (`f3840d7`) | B (this PR) | |---|---|---| | time in the world | 6.2 min | 5.3 min | | BLAS heaps at minute 5, allocated / in use | 4,160 / 1,726 MB (65 × 64 MB) | 2,256 / 1,880 MB (11 released) | | BLAS heaps at the end, allocated / in use | 4,160 / 2,230 MB | 2,272 / 2,062 MB | | live BLAS at minute 5 | 1,430 MB | 1,426 MB | | DXGI local in use / budget at minute 5 | 17,594 / 14,918 MB | 15,217 / 14,912 MB | | ledger "unaccounted" at minute 5 | 5,908 MB | 4,018 MB | | median frame time (world windows) | 44.3 ms (22.6 fps) | 39.7 ms (25.2 fps) | | frames over 100 ms | 159 (25.7 a minute) | 38 (7.2 a minute) | | longest frame in the world | 409 ms | 220 ms | | BLAS buffer creation | 28.0 µs each | 13.9 µs each | By eye: distant objects disappeared for a moment more often in A. No new visual problems in B. Neither run lost the device (both were about 6 minutes, so no claim about device losses). What this does not show: one pair, driven by hand, of different lengths, so part of the frame-time difference may be the route. The card being 2.7 GB over its budget in A against 0.3 GB in B may explain part of it. One note from the logs: the PR keeps `cardledger::retag(&heap, Kind::AccelStructure)` after `CreateHeap`. `retag` only changes the kind of an allocation the ledger already counts, so the BLAS heaps stay out of it (the gap noted in #290). The "unaccounted" row above moves by about the heap difference. `cardledger::heap(&heap, Kind::AccelStructure)` would count them, as `alias_rt` and `pagebuf` do. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Contributor

Test on an RTX 4070 Ti 12 GB: this held 30 minutes with hardware RT and kept the card under its budget, where main ended over it. One run per arm, so treat it as a data point.

  • Setup: native 1440p, DLSS off, frame generation off, hardware RT, nv-profile 2048 MiB. Release builds of f3840d73 and of this PR's head 9e6c1120. Standing in one area with a scripted camera turn every 20 s.
  • At minute 29, main: 31 heaps, 1,984 MB held for 1,042 MB live BLAS, 3,223 evictions, card 355 MB over budget.
  • At minute 29, this PR: 80 heaps, 1,424 MB allocated for 883 MB live BLAS, 0 evictions, 0 heaps released, card 82 MB under budget.
  • It's close to the edge though: least room during the run was 21 MB.
  • For reference, DARNER_HW_RT_BLAS_HEAPS=0 on main: 883 MB live BLAS, card 689 MB under budget.
  • fps: no difference I can separate. In the first quarter, where all three had the same scene, 21.0 / 21.6 / 21.4.
  • No device loss or driver event in any of the three.
Test on an RTX 4070 Ti 12 GB: this held 30 minutes with hardware RT and kept the card under its budget, where main ended over it. One run per arm, so treat it as a data point. - Setup: native 1440p, DLSS off, frame generation off, hardware RT, `nv-profile` 2048 MiB. Release builds of f3840d73 and of this PR's head 9e6c1120. Standing in one area with a scripted camera turn every 20 s. - At minute 29, main: 31 heaps, 1,984 MB held for 1,042 MB live BLAS, 3,223 evictions, card 355 MB over budget. - At minute 29, this PR: 80 heaps, 1,424 MB allocated for 883 MB live BLAS, 0 evictions, 0 heaps released, card 82 MB under budget. - It's close to the edge though: least room during the run was 21 MB. - For reference, `DARNER_HW_RT_BLAS_HEAPS=0` on main: 883 MB live BLAS, card 689 MB under budget. - fps: no difference I can separate. In the first quarter, where all three had the same scene, 21.0 / 21.6 / 21.4. - No device loss or driver event in any of the three.
First-time contributor

Tested on a different GPU: RTX 5080 16 GB, Ryzen 7 9800X3D, driver 32.0.16.2140, Windows. A/B on the same save/area with the same settings (DARNER_MENU_RT=hardware, fullscreen, frame gen on): main f3840d7 vs this PR 9e6c112.

Method: sampled the hardware RT BLAS buffers: line every 5 s and compared the loaded part of each run (BLAS in use >= 1,500 MB — 44 samples over ~3.7 min on main, 38 over ~3.2 min on the PR). The scenes were not frame-identical, but mean BLAS in use matched closely (2,001 vs 2,020 MB).

main f3840d7 PR 9e6c112
Heaps up to 46 x 64 MB 91–141 (16/32 MB classes)
Allocated, mean 2,861 MB 2,389 MB
In use, mean 2,001 MB 2,020 MB
Waste, mean 860 MB (30.0%) 369 MB (15.5%)
Peak in use 2,281 MB @ 2,944 MB allocated (663 MB waste) 2,390 MB @ 2,720 MB allocated (330 MB waste)

At comparable live BLAS, allocated heap memory was ~470 MB lower on average and waste roughly halved. At the highest observed load the PR held 109 MB more BLAS in 224 MB less heap. No correctness issues seen: ~10k live BLAS, AS memory 1,478 MB, no device loss.

One caveat on the release path: at the end, BLAS in use fell from 2,390 to 1,876 MB within 5 s, but allocated only went 2,720 -> 2,688 MB and waste rose to ~810 MB — only 1 heap was released. Since a heap is only released once it is completely empty (one spare per class kept), a sudden drop in live BLAS leaves allocated high until heaps drain. Not necessarily wrong, but the release path got little exercise at this load.

Not tested: DARNER_HW_RT_BLAS_HEAPS=0, other GPUs, and performance (scenes/durations differ, so fps is not comparable).

Drafted with AI assistance (OpenCode).

Tested on a different GPU: RTX 5080 16 GB, Ryzen 7 9800X3D, driver 32.0.16.2140, Windows. A/B on the same save/area with the same settings (`DARNER_MENU_RT=hardware`, fullscreen, frame gen on): main `f3840d7` vs this PR `9e6c112`. Method: sampled the `hardware RT BLAS buffers:` line every 5 s and compared the loaded part of each run (BLAS in use >= 1,500 MB — 44 samples over ~3.7 min on main, 38 over ~3.2 min on the PR). The scenes were not frame-identical, but mean BLAS in use matched closely (2,001 vs 2,020 MB). | | main `f3840d7` | PR `9e6c112` | |---|---|---| | Heaps | up to 46 x 64 MB | 91–141 (16/32 MB classes) | | Allocated, mean | 2,861 MB | 2,389 MB | | In use, mean | 2,001 MB | 2,020 MB | | Waste, mean | 860 MB (30.0%) | 369 MB (15.5%) | | Peak in use | 2,281 MB @ 2,944 MB allocated (663 MB waste) | 2,390 MB @ 2,720 MB allocated (330 MB waste) | At comparable live BLAS, allocated heap memory was ~470 MB lower on average and waste roughly halved. At the highest observed load the PR held 109 MB more BLAS in 224 MB less heap. No correctness issues seen: ~10k live BLAS, AS memory 1,478 MB, no device loss. One caveat on the release path: at the end, BLAS in use fell from 2,390 to 1,876 MB within 5 s, but allocated only went 2,720 -> 2,688 MB and waste rose to ~810 MB — only 1 heap was released. Since a heap is only released once it is completely empty (one spare per class kept), a sudden drop in live BLAS leaves allocated high until heaps drain. Not necessarily wrong, but the release path got little exercise at this load. Not tested: `DARNER_HW_RT_BLAS_HEAPS=0`, other GPUs, and performance (scenes/durations differ, so fps is not comparable). Drafted with AI assistance (OpenCode).
Contributor

Code review only (read the diff at 9e6c1120), nothing built or run. I agree with lewis499's allocator audit and with 1rascen's ledger note (cardledger::heap instead of retag). New points:

  • Links: this addresses most of #290 (heaps never given back), so Refs #290 would connect them. It doesn't touch any shader or descriptor code, so it doesn't conflict with #292, which fixes constant reads.
  • Heap creation under the pool lock: SlotReturn::drop now takes blas_heaps() (when a heap empties) from inside the runtime's release of the placed buffer, while take_units holds that lock across CreateHeap. I can't show a deadlock from code, but making the heap before taking the pool lock would rule one out at little cost.
  • Churn with #268: #268 holds each BLAS until its list resets, which likely turns per-frame refits into new allocations. Together with one spare heap per class, that could make and release heaps repeatedly. The three hwrt.rs PRs (#268, #279, this one) merge cleanly together. A run with #268 merged too, watching the "released" count in blas_heap_line(), would show it.

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

Code review only (read the diff at `9e6c1120`), nothing built or run. I agree with lewis499's allocator audit and with 1rascen's ledger note (`cardledger::heap` instead of `retag`). New points: - **Links:** this addresses most of #290 (heaps never given back), so `Refs #290` would connect them. It doesn't touch any shader or descriptor code, so it doesn't conflict with #292, which fixes constant reads. - **Heap creation under the pool lock:** `SlotReturn::drop` now takes `blas_heaps()` (when a heap empties) from inside the runtime's release of the placed buffer, while `take_units` holds that lock across `CreateHeap`. I can't show a deadlock from code, but making the heap before taking the pool lock would rule one out at little cost. - **Churn with #268:** #268 holds each BLAS until its list resets, which likely turns per-frame refits into new allocations. Together with one spare heap per class, that could make and release heaps repeatedly. The three `hwrt.rs` PRs (#268, #279, this one) merge cleanly together. A run with #268 merged too, watching the "released" count in `blas_heap_line()`, would show it. Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.
Author
First-time contributor

Thank you all so much for the reviews and independent in-game tests — @lewis499, @1rascen, @Remorce, @m3m3dub5, and @iBooM72! Seeing the results across RTX 4070 Ti 12 GB and RTX 5080 16 GB is super helpful.

I addressed the review points in follow-up commit 347b421:

  • Heap creation outside the pool lock (@iBooM72): take_units now releases blas_heaps() before calling ID3D12Device::CreateHeap, then re-acquires the lock and re-checks take_existing (dropping any redundant heap outside the lock) so SlotReturn::drop cannot invert lock order with CreateHeap.
  • Ledger accounting (@1rascen, @iBooM72): switched cardledger::retag(&heap, Kind::AccelStructure) to cardledger::heap(&heap, Kind::AccelStructure) so BLAS heaps are counted directly under AccelStructure instead of showing up as unaccounted.
  • Added Refs #290 to the PR description.

Re-ran on 347b421:

  • cargo test --locked --offline -p darner-gpu --lib -- hwrt:: → 29 passed, 0 failed
  • cargo clippy --workspace --all-targets --locked --offline -- -D warnings -A deprecated → passed
  • cargo clippy --workspace --all-targets --all-features --locked --offline -- -D warnings -A deprecated → passed
  • cargo xtask policy → asset policy: 555 files ok
Thank you all so much for the reviews and independent in-game tests — @lewis499, @1rascen, @Remorce, @m3m3dub5, and @iBooM72! Seeing the results across RTX 4070 Ti 12 GB and RTX 5080 16 GB is super helpful. I addressed the review points in follow-up commit `347b421`: - **Heap creation outside the pool lock** (@iBooM72): `take_units` now releases `blas_heaps()` before calling `ID3D12Device::CreateHeap`, then re-acquires the lock and re-checks `take_existing` (dropping any redundant heap outside the lock) so `SlotReturn::drop` cannot invert lock order with `CreateHeap`. - **Ledger accounting** (@1rascen, @iBooM72): switched `cardledger::retag(&heap, Kind::AccelStructure)` to `cardledger::heap(&heap, Kind::AccelStructure)` so BLAS heaps are counted directly under `AccelStructure` instead of showing up as unaccounted. - Added `Refs #290` to the PR description. Re-ran on `347b421`: - `cargo test --locked --offline -p darner-gpu --lib -- hwrt::` → 29 passed, 0 failed - `cargo clippy --workspace --all-targets --locked --offline -- -D warnings -A deprecated` → passed - `cargo clippy --workspace --all-targets --all-features --locked --offline -- -D warnings -A deprecated` → passed - `cargo xtask policy` → `asset policy: 555 files ok`
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/blas-heap-fragmentation:Deyzrakc-fix/blas-heap-fragmentation'
git switch 'Deyzrakc-fix/blas-heap-fragmentation'

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 'Deyzrakc-fix/blas-heap-fragmentation'
git switch 'Deyzrakc-fix/blas-heap-fragmentation'
git rebase 'main'
git switch 'main'
git merge --ff-only 'Deyzrakc-fix/blas-heap-fragmentation'
git switch 'Deyzrakc-fix/blas-heap-fragmentation'
git rebase 'main'
git switch 'main'
git merge --no-ff 'Deyzrakc-fix/blas-heap-fragmentation'
git switch 'main'
git merge --squash 'Deyzrakc-fix/blas-heap-fragmentation'
git switch 'main'
git merge --ff-only 'Deyzrakc-fix/blas-heap-fragmentation'
git switch 'main'
git merge 'Deyzrakc-fix/blas-heap-fragmentation'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
6 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!318
No description provided.