GPU: reuse the matched entry when finding a shadow buffer #3503

Open
github-bot wants to merge 1 commit from github-bot/gh-3503 into main AGit
Owner

@⁠Venom2054 on GitHub · 2026-11-27 10:59 UTC · original
Venom2054:gpu/one-shadow-range-search → main · +3 −5 · 1 files · 1 commits · open

shadow::find searches the locked buffer registry backwards for the first range that covers the requested bytes. It then looks up the same key again to access its entry. This change uses the mutable entry returned by the first search. The ordering, 64-entry search limit, lock scope, loose-buffer fallback and lazy allocation are unchanged.

Measurements. A synthetic CPU comparison over 16,384 registered ranges and 262,144 queries took a median of 116.2 ns per query with the second lookup and 71.7 ns with a single lookup. Separate checks covered 4,053 queries across overlaps, replacement and removal.

In a separate instrumented local renderer, one in 256 actual lookups ran both implementations under the same lock, alternating which ran first. Across 468,270 sampled lookups they agreed on every hit, miss, entry and extent. The two measured scene intervals went from 228.63 to 108.81 ns per lookup and from 234.87 to 108.36 ns per lookup. These measure the CPU registry search. The scene runs differed in texture streaming, so there is no matched whole-frame FPS claim.

Verification on Windows, based on 63bcca4. All 10 victory-gpu shadow tests passed. GPU crate Clippy (--all-targets --all-features --locked -- -D warnings) and cargo xtask policy passed. The full GPU library suite reported 229 passed, two failed, one ignored on both this revision and the clean base on this machine. The two failures were descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views.

The production diff is confined to the lookup in crates/victory-gpu/src/shadow.rs. Implementation and testing were assisted by Codex.

**@⁠Venom2054** on GitHub · 2026-11-27 10:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/3503) `Venom2054:gpu/one-shadow-range-search` → `main` · +3 −5 · 1 files · 1 commits · open `shadow::find` searches the locked buffer registry backwards for the first range that covers the requested bytes. It then looks up the same key again to access its entry. This change uses the mutable entry returned by the first search. The ordering, 64-entry search limit, lock scope, loose-buffer fallback and lazy allocation are unchanged. **Measurements.** A synthetic CPU comparison over 16,384 registered ranges and 262,144 queries took a median of 116.2 ns per query with the second lookup and 71.7 ns with a single lookup. Separate checks covered 4,053 queries across overlaps, replacement and removal. In a separate instrumented local renderer, one in 256 actual lookups ran both implementations under the same lock, alternating which ran first. Across 468,270 sampled lookups they agreed on every hit, miss, entry and extent. The two measured scene intervals went from 228.63 to 108.81 ns per lookup and from 234.87 to 108.36 ns per lookup. These measure the CPU registry search. The scene runs differed in texture streaming, so there is no matched whole-frame FPS claim. **Verification on Windows, based on `63bcca4`.** All 10 `victory-gpu` shadow tests passed. GPU crate Clippy (`--all-targets --all-features --locked -- -D warnings`) and `cargo xtask policy` passed. The full GPU library suite reported 229 passed, two failed, one ignored on both this revision and the clean base on this machine. The two failures were `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views`. The production diff is confined to the lookup in `crates/victory-gpu/src/shadow.rs`. Implementation and testing were assisted by Codex.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 11:02:01 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-27 11:35 UTC · original

Independent WARP/CPU verification (no physical GPU), Windows 11 build 26200, Rust 1.98.1 debug build, cargo test -p victory-gpu --lib --locked, VICTORY_WARP=1 and VICTORY_D3D12_WARP=1, system WARP 10.0.26100.9818, dxil.dll 10.0.19041.5609 on PATH.

Commits: base 63bcca4 (main), head 4e16f01a.

Diff read (crates/victory-gpu/src/shadow.rs:1012-1024, +3/-5):

  • Lock scope: unchanged. The buffers() guard map is taken before the search and held until the function returns. In the miss branch, drop(map) runs before loose(), as before.
  • Entry identity: range_mut(..=(address, usize::MAX)).rev().take(64).find(|(_, e)| e.end >= end) yields the key and &mut Entry of the same element. The old code copied the key out and called get_mut(&key), which returns that same element, because the lock is held across both steps. key.0 is still the start used for len, the buffer name and Shadow::new.
  • Overlap: iteration order and the 64-entry cap are unchanged, so the innermost covering range still wins (the key is (start, resource)).
  • Replacement and removal: register (insert on the same key replaces the entry) and unregister (remove) are untouched and run under the same mutex, so find never sees a stale entry. The old get_mut(&key)? failure path (None on a missing key) cannot occur and is gone.
  • Lazy allocation: entry.shadow.is_none() and the host()? early return behave as before. The guard drops on return, and the entry is left unset on failure, as before.

Tests, cargo test -p victory-gpu --lib:

passed failed ignored
base 63bcca4 231 0 1
head 4e16f01a 231 0 1

The 11 tests matching shadow also pass on head. The PR's two failures (descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views) did not reproduce here on either side.

Findings: no blockers. One observation: no test calls find directly on overlapping, replaced or removed ranges. The lookup is covered only through the callers' tests, so the PR's 4,053-query check is not in the tree. I did not run clippy or cargo xtask policy, and I did not check the performance numbers, since they need a different setup.

AI-assisted (Claude Code / Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-27 11:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/3503#issuecomment-7217253546) Independent WARP/CPU verification (no physical GPU), Windows 11 build 26200, Rust 1.98.1 debug build, `cargo test -p victory-gpu --lib --locked`, `VICTORY_WARP=1` and `VICTORY_D3D12_WARP=1`, system WARP 10.0.26100.9818, dxil.dll 10.0.19041.5609 on PATH. Commits: base `63bcca4` (main), head `4e16f01a`. **Diff read** (`crates/victory-gpu/src/shadow.rs:1012-1024`, +3/-5): - Lock scope: unchanged. The `buffers()` guard `map` is taken before the search and held until the function returns. In the miss branch, `drop(map)` runs before `loose()`, as before. - Entry identity: `range_mut(..=(address, usize::MAX)).rev().take(64).find(|(_, e)| e.end >= end)` yields the key and `&mut Entry` of the same element. The old code copied the key out and called `get_mut(&key)`, which returns that same element, because the lock is held across both steps. `key.0` is still the start used for `len`, the buffer name and `Shadow::new`. - Overlap: iteration order and the 64-entry cap are unchanged, so the innermost covering range still wins (the key is `(start, resource)`). - Replacement and removal: `register` (`insert` on the same key replaces the entry) and `unregister` (`remove`) are untouched and run under the same mutex, so `find` never sees a stale entry. The old `get_mut(&key)?` failure path (`None` on a missing key) cannot occur and is gone. - Lazy allocation: `entry.shadow.is_none()` and the `host()?` early return behave as before. The guard drops on return, and the entry is left unset on failure, as before. **Tests, `cargo test -p victory-gpu --lib`:** | | passed | failed | ignored | | --- | --- | --- | --- | | base `63bcca4` | 231 | 0 | 1 | | head `4e16f01a` | 231 | 0 | 1 | The 11 tests matching `shadow` also pass on head. The PR's two failures (`descriptor::host::tests::dynamic_slots_are_held_until_released`, `memory::tests::lists_share_a_table_and_copies_keep_their_views`) did not reproduce here on either side. **Findings:** no blockers. One observation: no test calls `find` directly on overlapping, replaced or removed ranges. The lookup is covered only through the callers' tests, so the PR's 4,053-query check is not in the tree. I did not run clippy or `cargo xtask policy`, and I did not check the performance numbers, since they need a different setup. AI-assisted (Claude Code / Claude Sonnet 5.5).
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-27 11:50 UTC · original · approved these changes

LGTM. range_mut(..).rev().take(64).find(..) yields the key and the entry of the element the old get_mut(&key) looked up again under the same lock, so hits, misses, the 64-entry cap and the loose fallback are unchanged. key.0 is still the start used for the length, the name and Shadow::new. CI is green, and oni-swr's full victory-gpu run matches main.

One thing for landing order: #2096 (fuzeofficlal, merge candidate) rewrites these same lines. It drops take(64) for a max_size bound in a new find_buffer, but keeps the second get_mut(&key), so whichever lands second conflicts. If #2096 goes first, the same saving carries over by returning the entry from find_buffer (a sketch, not compiled against #2096):

fn find_buffer(
    map: &mut BTreeMap<(u64, usize), Entry>,
    max_size: u64,
    address: u64,
    end: u64,
) -> Option<(&(u64, usize), &mut Entry)> {
    let lowest = end.saturating_sub(max_size);
    if lowest > address {
        return None;
    }
    map.range_mut((lowest, 0)..=(address, usize::MAX))
        .rev()
        .find(|(_, e)| e.end >= end)
}

with, in find:

    let mut state = buffers();
    let max_size = state.max_size();
    let Some((key, entry)) = find_buffer(&mut state.map, max_size, address, end) else {
        drop(state);
        return loose(address, end);
    };
**@⁠AurumCarlos** on GitHub · 2026-11-27 11:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3503#pullrequestreview-6087693312) · **approved these changes** LGTM. `range_mut(..).rev().take(64).find(..)` yields the key and the entry of the element the old `get_mut(&key)` looked up again under the same lock, so hits, misses, the 64-entry cap and the `loose` fallback are unchanged. `key.0` is still the start used for the length, the name and `Shadow::new`. CI is green, and oni-swr's full `victory-gpu` run matches `main`. One thing for landing order: #2096 (fuzeofficlal, merge candidate) rewrites these same lines. It drops `take(64)` for a `max_size` bound in a new `find_buffer`, but keeps the second `get_mut(&key)`, so whichever lands second conflicts. If #2096 goes first, the same saving carries over by returning the entry from `find_buffer` (a sketch, not compiled against #2096): ```rust fn find_buffer( map: &mut BTreeMap<(u64, usize), Entry>, max_size: u64, address: u64, end: u64, ) -> Option<(&(u64, usize), &mut Entry)> { let lowest = end.saturating_sub(max_size); if lowest > address { return None; } map.range_mut((lowest, 0)..=(address, usize::MAX)) .rev() .find(|(_, e)| e.end >= end) } ``` with, in `find`: ```rust let mut state = buffers(); let max_size = state.max_size(); let Some((key, entry)) = find_buffer(&mut state.map, max_size, address, end) else { drop(state); return loose(address, end); }; ```
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-27 19:23 UTC · original

NVIDIA full-suite run, to go with the reports above by oni-swr, since none of them names a GPU run. Tested exact head 4e16f01 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 866 passed, 0 failed, 10 ignored.
  • No new #[test] functions in the diff.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-27 19:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/3503#issuecomment-7226488532) NVIDIA full-suite run, to go with the reports above by oni-swr, since none of them names a GPU run. Tested exact head `4e16f01` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **866 passed, 0 failed, 10 ignored**. - No new `#[test]` functions in the diff. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
This pull request can be merged automatically.
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 origin '+refs/pull/3503/head:github-bot/gh-3503'
git switch 'github-bot/gh-3503'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3503'
git switch 'github-bot/gh-3503'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3503'
git switch 'github-bot/gh-3503'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3503'
git switch 'main'
git merge --squash 'github-bot/gh-3503'
git switch 'main'
git merge --ff-only 'github-bot/gh-3503'
git switch 'main'
git merge 'github-bot/gh-3503'
git push origin 'main'
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/Marvisak--VIctory!3503
No description provided.