GPU: evicting given-up textures binary-searches the gone set, and choose skips its sort when nothing is over budget #3248

Closed
borosf wants to merge 2 commits from upstream-pr/3250 into main
borosf commented 2026-11-26 14:34:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T14:34:59Z.


Two per-pass costs in the eviction path, both pure bookkeeping.

HostHeap::evict — one hash probe per kept slot

Once per pass that gives textures up, evict walked every kept slot (~962k of them, 15.4 MB of Kept) and probed a HashSet<usize> with SipHash for each, all under the kept lock that every draw's mirror also needs. The gone set is a handful of pointers.

Now sorted once per pass; each slot asks binary_search (2–3 ns) instead of a probe (~15 ns). Signature unchanged, so the one caller (descriptor.rs:274) is untouched.

resource::choose — sorting candidates when nothing is over budget

choose partitioned out the idle textures, then sorted all remaining candidates by last use before discovering over == 0. It now returns the idle set before the sort.

Verification

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings — clean; --all-features too.
  • cargo test -p victory-gpu --lib --locked --no-fail-fast — 230 passed, 2 failed, both the known Intel baseline failures pristine main also fails (#1744/#1829). The eviction tests (evict_among, idle/budget cases in resource::tests) pass.
  • rustfmt --edition 2024 --check hunk counts against main: descriptor/host.rs 67→66 (the rewritten filter now conforms, so one fewer hunk), resource.rs 64/64.

Synthetic tests only; no game data.

Note on the sibling item from the same sweep (the bindless::forget full-map sweep): I am holding that one back until #3213 lands, because it guards exactly the Viewed::set / Resource::drop race the sweep's fix touches. Claimed on #1633, not released.

Part of the fifteen gaps claimed for this sweep on #1633. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5511937766 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3250). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T14:34:59Z. --- Two per-pass costs in the eviction path, both pure bookkeeping. ## `HostHeap::evict` — one hash probe per kept slot Once per pass that gives textures up, `evict` walked every kept slot (~962k of them, 15.4 MB of `Kept`) and probed a `HashSet<usize>` with SipHash for each, all under the `kept` lock that every draw's `mirror` also needs. The gone set is a handful of pointers. Now sorted once per pass; each slot asks `binary_search` (2–3 ns) instead of a probe (~15 ns). Signature unchanged, so the one caller (`descriptor.rs:274`) is untouched. ## `resource::choose` — sorting candidates when nothing is over budget `choose` partitioned out the idle textures, then sorted *all* remaining candidates by last use before discovering `over == 0`. It now returns the idle set before the sort. ## Verification - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` — clean; `--all-features` too. - `cargo test -p victory-gpu --lib --locked --no-fail-fast` — 230 passed, 2 failed, both the known Intel baseline failures pristine `main` also fails (#1744/#1829). The eviction tests (`evict_among`, idle/budget cases in `resource::tests`) pass. - `rustfmt --edition 2024 --check` hunk counts against `main`: `descriptor/host.rs` 67→66 (the rewritten filter now conforms, so one fewer hunk), `resource.rs` 64/64. Synthetic tests only; no game data. Note on the sibling item from the same sweep (the `bindless::forget` full-map sweep): I am holding that one back until #3213 lands, because it guards exactly the `Viewed::set` / `Resource::drop` race the sweep's fix touches. Claimed on #1633, not released. Part of the fifteen gaps claimed for this sweep on #1633. AI-assisted.
AurumCarlos commented 2026-11-26 14:37:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T14:37:53Z.

Review: COMMENTED


Both changes are behaviour-preserving as far as I can see:

  • choose: on main, with over == 0 after the idle partition, the loop breaks on its first iteration and returns gone as is. Returning before the sort gives the same Vec without the O(n log n).
  • evict: binary_search on the sorted pointers answers the same membership question as HashSet::contains, and the filter keeps the k.1.is_none() check first.

A few remarks:

1. The sort runs once per heap, not once per pass. descriptor::evict calls host.evict(textures) for every shader-visible view heap, so the collect + sort_unstable happens once per heap. With a handful of pointers that's negligible, but the comment ("Sorted once … per pass") isn't quite accurate. If you'd rather not change the signature, rewording the comment is enough. Otherwise, sorting in descriptor::evict and passing &[usize] would make it literally once.

2. For "a handful", a linear scan may beat both. If the gone set really is a few pointers per pass, gone.contains(&p) on a tiny Vec is a couple of compares with no hashing and no branchy search, and it's simpler. Binary search is the safer choice when the LRU pick frees many textures under budget pressure, so I'd keep it. But the 2–3 ns vs ~15 ns figures read as estimates: if you measured them (and the ~962k-slot walk time before and after, under the kept lock), putting the numbers in the description would make the win concrete.

3. Nit: let (mut gone, rest) = … followed later by let mut rest = rest; could just stay let (mut gone, mut rest) = … as on main, since the early return doesn't need rest to be immutable.

<!-- greyfinch-sync:Marvisak/VIctory:review:6075967818 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3250#pullrequestreview-6075967818). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T14:37:53Z. **Review:** COMMENTED --- Both changes are behaviour-preserving as far as I can see: - **`choose`**: on `main`, with `over == 0` after the idle partition, the loop breaks on its first iteration and returns `gone` as is. Returning before the sort gives the same `Vec` without the `O(n log n)`. - **`evict`**: `binary_search` on the sorted pointers answers the same membership question as `HashSet::contains`, and the filter keeps the `k.1.is_none()` check first. A few remarks: **1. The sort runs once per heap, not once per pass.** `descriptor::evict` calls `host.evict(textures)` for every shader-visible view heap, so the `collect` + `sort_unstable` happens once per heap. With a handful of pointers that's negligible, but the comment ("Sorted once … per pass") isn't quite accurate. If you'd rather not change the signature, rewording the comment is enough. Otherwise, sorting in `descriptor::evict` and passing `&[usize]` would make it literally once. **2. For "a handful", a linear scan may beat both.** If the gone set really is a few pointers per pass, `gone.contains(&p)` on a tiny `Vec` is a couple of compares with no hashing and no branchy search, and it's simpler. Binary search is the safer choice when the LRU pick frees many textures under budget pressure, so I'd keep it. But the 2–3 ns vs ~15 ns figures read as estimates: if you measured them (and the ~962k-slot walk time before and after, under the `kept` lock), putting the numbers in the description would make the win concrete. **3. Nit**: `let (mut gone, rest) = …` followed later by `let mut rest = rest;` could just stay `let (mut gone, mut rest) = …` as on `main`, since the early return doesn't need `rest` to be immutable.
borosf commented 2026-11-26 15:02:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T14:45:48Z.


All three remarks addressed at 5afb3844c88a8597bd525184ae22028da3c7f488:

  1. Comment: now says "sorted once per call (per heap per pass)" — you're right that descriptor::evict calls host.evict per shader-visible heap, so the collect+sort happens per heap.

  2. Measured, not estimated — and the honest number is smaller than my estimate. New ignored test descriptor::host::tests::an_evict_probe_costs walks a 962,144-slot kept list (a texture slot every 7th, 64 gone pointers) both ways on this B580, --release:

    cargo test --release -p victory-gpu --lib --locked -- --exact --ignored --nocapture \
      descriptor::host::tests::an_evict_probe_costs
    
    962144 kept slots: SipHash probe 1.7437ms (1.8 ns/slot), binary search 1.0934ms (1.1 ns/slot), 10 hits
    

    So ~1.8 → ~1.1 ns per slot, about 0.65 ms per 962k-slot pass, not the ~15 ms my "~15 ns" estimate implied. Kept binary search as you suggested (it wins, and wins more when the LRU pick frees many textures under budget pressure); the PR body's ns figures should be read as replaced by these.

  3. Nit: back to let (mut gone, mut rest) = … as on main, early return unchanged.

Re-verified locally: clippy clean on both feature sets, cargo test -p victory-gpu --lib --locked --no-fail-fast 229 passed / 2 failed (the known #1744/#1829 Intel baseline failures), rustfmt hunk counts vs main unchanged (descriptor/host.rs 67/67, resource.rs 64/64). Synthetic tests only; no game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195290067 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3250#issuecomment-7195290067). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T14:45:48Z. --- All three remarks addressed at `5afb3844c88a8597bd525184ae22028da3c7f488`: 1. **Comment**: now says "sorted once per call (per heap per pass)" — you're right that `descriptor::evict` calls `host.evict` per shader-visible heap, so the collect+sort happens per heap. 2. **Measured, not estimated** — and the honest number is smaller than my estimate. New ignored test `descriptor::host::tests::an_evict_probe_costs` walks a 962,144-slot kept list (a texture slot every 7th, 64 gone pointers) both ways on this B580, `--release`: ``` cargo test --release -p victory-gpu --lib --locked -- --exact --ignored --nocapture \ descriptor::host::tests::an_evict_probe_costs 962144 kept slots: SipHash probe 1.7437ms (1.8 ns/slot), binary search 1.0934ms (1.1 ns/slot), 10 hits ``` So ~1.8 → ~1.1 ns per slot, about **0.65 ms per 962k-slot pass**, not the ~15 ms my "~15 ns" estimate implied. Kept binary search as you suggested (it wins, and wins more when the LRU pick frees many textures under budget pressure); the PR body's ns figures should be read as replaced by these. 3. **Nit**: back to `let (mut gone, mut rest) = …` as on main, early return unchanged. Re-verified locally: clippy clean on both feature sets, `cargo test -p victory-gpu --lib --locked --no-fail-fast` 229 passed / 2 failed (the known #1744/#1829 Intel baseline failures), rustfmt hunk counts vs main unchanged (`descriptor/host.rs` 67/67, `resource.rs` 64/64). Synthetic tests only; no game data.
Dukestar69 commented 2026-11-26 15:31:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T15:31:46Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Sorting gone once in HostHeap::evict for binary search over kept slots and skipping the LRU sort in resource::choose when over == 0 are clean, behavior-preserving optimizations.
  • cargo test -p victory-gpu --lib descriptor::host::tests:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released (RTX 5090): 14 passed, 0 failed, 1 ignored (an_evict_probe_costs).
  • cargo test -p victory-gpu --lib resource::tests:: -- --test-threads=1 (RTX 5090): 18 passed, 0 failed (including idle_textures_go_and_past_the_budget_the_least_recently_used).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6076806335 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3250#pullrequestreview-6076806335). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T15:31:46Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090): - Sorting `gone` once in `HostHeap::evict` for binary search over kept slots and skipping the LRU sort in `resource::choose` when `over == 0` are clean, behavior-preserving optimizations. - `cargo test -p victory-gpu --lib descriptor::host::tests:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released` (RTX 5090): 14 passed, 0 failed, 1 ignored (`an_evict_probe_costs`). - `cargo test -p victory-gpu --lib resource::tests:: -- --test-threads=1` (RTX 5090): 18 passed, 0 failed (including `idle_textures_go_and_past_the_budget_the_least_recently_used`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
borosf commented 2026-11-27 13:20:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-27T13:20:26Z.


Closing this: the micro-optimisation is real but the code it optimises is being removed by #3456.

The sort/binary-search is behaviour-preserving (best-fit preserved via partition_point plus remove(cut-1)), but #3456 rewrites the same HostHeap::evict loop onto a sparse kept map and removes the ~962k-slot walk entirely, which dominates this. Landing both would mean reconciling one function twice for a win that #3456 subsumes.

Two smaller reasons not to push it: the sort runs once per heap, not "once per pass" (descriptor.rs:300-306 calls evict per shader-visible heap), and the only test is an #[ignore]d benchmark that re-implements the filter in a closure instead of calling HostHeap::evict, so it pins neither correctness nor the claimed speed.

The choose half (resource.rs:1669-1671) is a clean no-op-equivalent and already covered by an existing test; I will re-offer it if #3456 changes shape.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7219357458 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3250#issuecomment-7219357458). Originally posted by [borosf](https://github.com/borosf) on 2026-11-27T13:20:26Z. --- Closing this: the micro-optimisation is real but the code it optimises is being removed by #3456. The sort/binary-search is behaviour-preserving (best-fit preserved via `partition_point` plus `remove(cut-1)`), but #3456 rewrites the same `HostHeap::evict` loop onto a sparse `kept` map and removes the ~962k-slot walk entirely, which dominates this. Landing both would mean reconciling one function twice for a win that #3456 subsumes. Two smaller reasons not to push it: the sort runs once per heap, not "once per pass" (`descriptor.rs:300-306` calls `evict` per shader-visible heap), and the only test is an `#[ignore]`d benchmark that re-implements the filter in a closure instead of calling `HostHeap::evict`, so it pins neither correctness nor the claimed speed. The `choose` half (`resource.rs:1669-1671`) is a clean no-op-equivalent and already covered by an existing test; I will re-offer it if #3456 changes shape.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3248
No description provided.