gpu/descriptor: lock-free ShapeArena and fast-hashed Views registry #245

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

@⁠mint17 on GitHub · 2026-11-27 11:55 UTC · original
mint17:perf/views-lockfree-shape-arena → main · +1578 −180 · 2 files · 1 commits · open

Eliminates global VIEWS RwLock read contention on placed-view decodes (view_at(PLACED)) and BTreeMap/SipHash overhead on view and shape lookups in crates/victory-gpu/src/descriptor.rs (zero merge conflict with #234).

Changes

  • Wait-free ShapeArenaCore (2048 × 2048 = 4,194,304 shapes): Stores append-only placed-view Shape entries in heap-allocated [UnsafeCell<MaybeUninit<Shape>>; 2048] slabs with a cache-line-isolated AtomicU32 watermark and direct raw-pointer UnsafeCell::raw_get slot reads/writes. view_at(PLACED) now performs zero locks and zero atomic writes.
  • Collision-safe, zero-duplication ShapeKey hashing: Replaces SipHash-1-3 + BTreeMap<u64, u32> with a fast field-level word hash (ShapeKey + IdentityHash) and a cold shape_collisions overflow map (zero heap bytes unless a 64-bit collision occurs, zero 116-byte Shape key duplication).
  • Fast-hashed Views maps: Replaces BTreeMap in index and by_resource with HashMap backed by a word-chunked cross-platform ViewHash, pre-hashes ShapeKey outside VIEWS.state lock, and removes the redundant contains_key lookup in entry_kept.

Benchmarks (cargo test --release --locked -p victory-gpu views_registry_bench -- --ignored --nocapture)

Regime Baseline (RwLock<Vec<Shape>> + SipHash + BTreeMap) New (ViewRegistry) Speedup
1T view_at(PLACED) 31.51 ns/op 22.44 ns/op 1.40×
8T contended view_at(PLACED) (7R + 1W) 1,119.12 ns/op (583.8 ms wall) 32.18 ns/op (17.8 ms wall) 34.78×
known_shape hit (65K shapes) 314.88 ns/op 133.47 ns/op 2.36×
entry_of hit (32K views) 231.60 ns/op 98.86 ns/op 2.34×

Verification

  • cargo test --locked -p victory-gpu descriptor:: -- --test-threads=1 (68 passed, 0 failed — includes 50,000-op BTreeMap oracle parity test, forced-collision & capacity-ceiling test across 4 slabs, hash avalanche test, and 8-thread concurrent stress test in descriptor/views_tests.rs).
  • cargo test --locked -p victory-gpu --all-features descriptor::tests -- --test-threads=1 (14 passed, 0 failed).
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default + --all-features) and cargo test --locked -p xtask passed.
  • Verified with synthetic tests, concurrency stress tests, and microbenchmarks only (no title files locally).
**@⁠mint17** on GitHub · 2026-11-27 11:55 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/245) `mint17:perf/views-lockfree-shape-arena` → `main` · +1578 −180 · 2 files · 1 commits · open Eliminates global `VIEWS` `RwLock` read contention on placed-view decodes (`view_at(PLACED)`) and `BTreeMap`/SipHash overhead on view and shape lookups in `crates/victory-gpu/src/descriptor.rs` (zero merge conflict with #234). ### Changes - **Wait-free `ShapeArenaCore` (`2048 × 2048 = 4,194,304` shapes):** Stores append-only placed-view `Shape` entries in heap-allocated `[UnsafeCell<MaybeUninit<Shape>>; 2048]` slabs with a cache-line-isolated `AtomicU32` watermark and direct raw-pointer `UnsafeCell::raw_get` slot reads/writes. `view_at(PLACED)` now performs zero locks and zero atomic writes. - **Collision-safe, zero-duplication `ShapeKey` hashing:** Replaces SipHash-1-3 + `BTreeMap<u64, u32>` with a fast field-level word hash (`ShapeKey` + `IdentityHash`) and a cold `shape_collisions` overflow map (zero heap bytes unless a 64-bit collision occurs, zero 116-byte `Shape` key duplication). - **Fast-hashed `Views` maps:** Replaces `BTreeMap` in `index` and `by_resource` with `HashMap` backed by a word-chunked cross-platform `ViewHash`, pre-hashes `ShapeKey` outside `VIEWS.state` lock, and removes the redundant `contains_key` lookup in `entry_kept`. ### Benchmarks (`cargo test --release --locked -p victory-gpu views_registry_bench -- --ignored --nocapture`) | Regime | Baseline (`RwLock<Vec<Shape>>` + SipHash + `BTreeMap`) | New (`ViewRegistry`) | Speedup | | --- | ---: | ---: | ---: | | 1T `view_at(PLACED)` | 31.51 ns/op | 22.44 ns/op | **1.40×** | | 8T contended `view_at(PLACED)` (7R + 1W) | 1,119.12 ns/op (583.8 ms wall) | 32.18 ns/op (17.8 ms wall) | **34.78×** | | `known_shape` hit (65K shapes) | 314.88 ns/op | 133.47 ns/op | **2.36×** | | `entry_of` hit (32K views) | 231.60 ns/op | 98.86 ns/op | **2.34×** | ### Verification - `cargo test --locked -p victory-gpu descriptor:: -- --test-threads=1` (68 passed, 0 failed — includes 50,000-op `BTreeMap` oracle parity test, forced-collision & capacity-ceiling test across 4 slabs, hash avalanche test, and 8-thread concurrent stress test in `descriptor/views_tests.rs`). - `cargo test --locked -p victory-gpu --all-features descriptor::tests -- --test-threads=1` (14 passed, 0 failed). - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default + `--all-features`) and `cargo test --locked -p xtask` passed. - Verified with synthetic tests, concurrency stress tests, and microbenchmarks only (no title files locally).
Eliminate global VIEWS RwLock read contention on placed-view decodes and
BTreeMap/SipHash overhead on view & shape lookups in descriptor.rs:

- Store append-only placed-view Shapes in a wait-free segmented slab arena
  (ShapeArenaCore<11, 2048, 2048>, UnsafeCell<MaybeUninit<Shape>> slabs
  allocated directly on the heap, cache-line-isolated AtomicU32 published
  watermark, raw-pointer UnsafeCell::raw_get slot access) so view_at(PLACED)
  runs with zero locks and zero atomic writes.
- Replace SipHash-1-3 + BTreeMap<u64, u32> shape_index with a fast
  field-level word hash (ShapeKey + IdentityHash) plus a lossless
  shape_collisions overflow map (zero Shape key duplication).
- Replace BTreeMap<View, u32> index and by_resource with HashMaps using a
  word-chunked cross-platform ViewHash, pre-hash ShapeKey outside the lock,
  and remove the redundant contains_key probe in entry_kept.
- Add 50,000-op BTree oracle parity tests, forced collision/capacity-ceiling
  tests, hash avalanche tests, 8-thread concurrent stress test, and an opt-in
  release microbenchmark (34.8x faster 8T contended view_at(PLACED), 2.36x
  faster known_shape, 2.34x faster entry_of).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 11:56:49 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 11:58 UTC · original

@⁠mint17 The 8-thread benchmark isolates contention in placed-view lookups. The first correctness gate for the arena is publication: a reader observing the new watermark must also see the initialised slab and shape. I’ll check that invariant at the slab boundary and keep the benchmark numbers scoped to registry operations.

**@⁠TaynanGT** on GitHub · 2026-11-27 11:58 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/245#issuecomment-7217685850) @⁠mint17 The 8-thread benchmark isolates contention in placed-view lookups. The first correctness gate for the arena is publication: a reader observing the new watermark must also see the initialised slab and shape. I’ll check that invariant at the slab boundary and keep the benchmark numbers scoped to registry operations.
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 origin '+refs/pull/245/head:github-bot/gh-245'
git switch 'github-bot/gh-245'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-245'
git switch 'github-bot/gh-245'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-245'
git switch 'github-bot/gh-245'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-245'
git switch 'main'
git merge --squash 'github-bot/gh-245'
git switch 'main'
git merge --ff-only 'github-bot/gh-245'
git switch 'main'
git merge 'github-bot/gh-245'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.