perf(gpu): use FxHash HashMap in descriptor VIEWS table #506

Manually merged
github merged 1 commit from github-bot/gh-506 into main 2026-12-05 17:30:18 +00:00 AGit
Owner

@⁠Not4Eric on GitHub · 2026-12-04 20:24 UTC · original
Not4Eric:perf/descriptor-fxhash-views → main · +44 −8 · 1 files · 1 commits · merged 2026-12-05 17:28 UTC

What this does

Replaces the BTreeMap indices (index, by_resource, shape_index) inside victory_gpu::descriptor::Views with a HashMap using a small non-cryptographic FxHash-style hasher (ViewHasher, zero new crate dependencies) and derives Hash on View. Lookup and insertion in VIEWS are now O(1) instead of O(log N) tree traversals under the VIEWS RwLock.

Checks

  • N/A: pure performance optimization preserving identical view/shape deduplication and lookup semantics.
  • N/A: no switches added, changed, or removed.
  • No unrelated cleanup: diff is limited to View, ViewHasher, and Views in crates/victory-gpu/src/descriptor.rs.
  • N/A: existing descriptor:: unit tests cover view deduplication, lookup, and resource teardown; no behavioural bug existed before to fail a test.
  • Checked open PR queue: independent of #503 and of perf/descriptor-tls-hit (separate regions of descriptor.rs).
  • cargo xtask policy passed (359 files ok); no fork-only files, configs, or build outputs.

Verified

  • cargo xtask policy — 359 files ok
  • PKG=victory-gpu ./build.sh test --lib -- descriptor:: --skip views_die_with_their_resource --test-threads=1 (Proton Wine on Linux, RX 9070 XT) — 40 passed; 0 failed, 0 warnings
  • Also exercised as part of a combined gameplay run on GTA 6 Series S (Ryzen 5 7600X, RX 9070 XT, CachyOS + Proton); not benchmarked in isolation.

Cost

Negligible: HashMap bucket overhead vs BTreeMap node allocations for VIEWS entries; 0 new warnings.

AI-assisted: yes

**@⁠Not4Eric** on GitHub · 2026-12-04 20:24 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/506) `Not4Eric:perf/descriptor-fxhash-views` → `main` · +44 −8 · 1 files · 1 commits · merged 2026-12-05 17:28 UTC ## What this does Replaces the `BTreeMap` indices (`index`, `by_resource`, `shape_index`) inside `victory_gpu::descriptor::Views` with a `HashMap` using a small non-cryptographic `FxHash`-style hasher (`ViewHasher`, zero new crate dependencies) and derives `Hash` on `View`. Lookup and insertion in `VIEWS` are now O(1) instead of O(log N) tree traversals under the `VIEWS` `RwLock`. ## Checks - N/A: pure performance optimization preserving identical view/shape deduplication and lookup semantics. - N/A: no switches added, changed, or removed. - [x] No unrelated cleanup: diff is limited to `View`, `ViewHasher`, and `Views` in `crates/victory-gpu/src/descriptor.rs`. - N/A: existing `descriptor::` unit tests cover view deduplication, lookup, and resource teardown; no behavioural bug existed before to fail a test. - [x] Checked open PR queue: independent of #503 and of `perf/descriptor-tls-hit` (separate regions of `descriptor.rs`). - [x] `cargo xtask policy` passed (`359 files ok`); no fork-only files, configs, or build outputs. ## Verified - `cargo xtask policy` — `359 files ok` - `PKG=victory-gpu ./build.sh test --lib -- descriptor:: --skip views_die_with_their_resource --test-threads=1` (Proton Wine on Linux, RX 9070 XT) — `40 passed; 0 failed`, 0 warnings - Also exercised as part of a combined gameplay run on GTA 6 Series S (Ryzen 5 7600X, RX 9070 XT, CachyOS + Proton); not benchmarked in isolation. ## Cost Negligible: `HashMap` bucket overhead vs `BTreeMap` node allocations for `VIEWS` entries; 0 new warnings. AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 20:27:20 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 22:34 UTC · original

Checked 9fb6941 on native Windows (didn't run the game). Merges clean on current main and alongside #505. victory-gpu lib tests show the same failures as main (indirect host_list x2, pipeline library loads x2, and the flaky watch test that also fails on main when run alone) and clippy with and without diagnostics adds nothing. No new dependency, and nothing iterates index, by_resource or shape_index, so the order change doesn't matter.

One thing worth a look. finish() (descriptor.rs:604) returns the raw multiply, so a key whose low bits are zero keeps zero low bits in the hash, and those are the bits HashMap picks the bucket with. That hits by_resource, whose keys are Resource addresses. A throwaway test allocating 20k blocks of the Resource layout under the crate's allocator gave about 90% of them 256-byte aligned (fresh and mostly sequential, so if anything kinder than a long game session). In a small standalone bench on those addresses, 20k keys, lookup went 27 ns with the BTreeMap on main, 11 ns with this PR, 6.5 ns with plain std SipHash, and 1.5 ns with the same hasher plus rotate_left(26) in finish, which is what rustc-hash 2 does for exactly this reason. With synthetic 4 KiB aligned keys it gets worse than the tree (133 vs 50 ns). So it's still a win over main as is, but a rotate in finish is one line and removes the cliff. index is less affected since the enum tag gets mixed in first, though Shader(r, None) at 20k was still 25 ns vs 14 ns for Shader(r, Some(..)).

Two small things. ViewHasher was inserted between the long "Every distinct view in use" comment and struct Views, so that doc now sits on ViewHasher (descriptor.rs:593) and Views has none. And views_kept still estimates the index as a tree ("a tree node" and the 3/2 factor at descriptor.rs:940 and 963-965), diagnostics only.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 22:34 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/506#issuecomment-7426088652) Checked 9fb6941 on native Windows (didn't run the game). Merges clean on current main and alongside #505. victory-gpu lib tests show the same failures as main (indirect host_list x2, pipeline library loads x2, and the flaky watch test that also fails on main when run alone) and clippy with and without diagnostics adds nothing. No new dependency, and nothing iterates index, by_resource or shape_index, so the order change doesn't matter. One thing worth a look. finish() (descriptor.rs:604) returns the raw multiply, so a key whose low bits are zero keeps zero low bits in the hash, and those are the bits HashMap picks the bucket with. That hits by_resource, whose keys are Resource addresses. A throwaway test allocating 20k blocks of the Resource layout under the crate's allocator gave about 90% of them 256-byte aligned (fresh and mostly sequential, so if anything kinder than a long game session). In a small standalone bench on those addresses, 20k keys, lookup went 27 ns with the BTreeMap on main, 11 ns with this PR, 6.5 ns with plain std SipHash, and 1.5 ns with the same hasher plus rotate_left(26) in finish, which is what rustc-hash 2 does for exactly this reason. With synthetic 4 KiB aligned keys it gets worse than the tree (133 vs 50 ns). So it's still a win over main as is, but a rotate in finish is one line and removes the cliff. index is less affected since the enum tag gets mixed in first, though Shader(r, None) at 20k was still 25 ns vs 14 ns for Shader(r, Some(..)). Two small things. ViewHasher was inserted between the long "Every distinct view in use" comment and struct Views, so that doc now sits on ViewHasher (descriptor.rs:593) and Views has none. And views_kept still estimates the index as a tree ("a tree node" and the 3/2 factor at descriptor.rs:940 and 963-965), diagnostics only. AI-assisted: yes
github-bot force-pushed github-bot/gh-506 from 9fb6941e8e to 0ae5ab4435 2026-12-05 00:05:35 +00:00 Compare
Author
Owner

@⁠Not4Eric on GitHub · 2026-12-05 00:00 UTC · original

Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this

Pushed updates addressing your review:

  • Returned self.0.rotate_left(26) from ViewHasher::finish().
  • Moved ViewHasher and ViewMap above struct Views's doc comment block so the comment stays attached to Views.
  • Updated views_kept()'s comment from "a tree node" to "a hash table bucket".
**@⁠Not4Eric** on GitHub · 2026-12-05 00:00 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/506#issuecomment-7427747975) Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this Pushed updates addressing your review: - Returned `self.0.rotate_left(26)` from `ViewHasher::finish()`. - Moved `ViewHasher` and `ViewMap` above `struct Views`'s doc comment block so the comment stays attached to `Views`. - Updated `views_kept()`'s comment from "a tree node" to "a hash table bucket".
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 00:48 UTC · original

Re-checked 0ae5ab4 on native Windows (didn't run the game).

All three are fixed. finish() now rotates (descriptor.rs:584), the Views doc is back on struct Views (608-629), and the views_kept comment says hash table bucket. In a standalone bench with 20k synthetic evenly spaced keys, lookups went from 6.0 to 1.7 ns at 256 byte spacing and from 146 to 2.8 ns at 4 KiB, so the cliff is gone. The * 3 / 2 factor at descriptor.rs:963 and 965 is still there, diagnostics only.

Full victory-gpu lib is 352 passed / 5 failed, the same known native failures as before, and clippy with and without diagnostics shows nothing new.

Looks good to me.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 00:48 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/506#issuecomment-7428648724) Re-checked 0ae5ab4 on native Windows (didn't run the game). All three are fixed. finish() now rotates (descriptor.rs:584), the Views doc is back on struct Views (608-629), and the views_kept comment says hash table bucket. In a standalone bench with 20k synthetic evenly spaced keys, lookups went from 6.0 to 1.7 ns at 256 byte spacing and from 146 to 2.8 ns at 4 KiB, so the cliff is gone. The `* 3 / 2` factor at descriptor.rs:963 and 965 is still there, diagnostics only. Full victory-gpu lib is 352 passed / 5 failed, the same known native failures as before, and clippy with and without diagnostics shows nothing new. Looks good to me. AI-assisted: yes
github manually merged commit cd0cba9f35 into main 2026-12-05 17:30:18 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.