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

Closed
mint17 wants to merge 533 commits from upstream-pr/245 into main
mint17 commented 2026-11-27 11:55:15 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by mint17 on 2026-11-27T11:55:15Z.


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).
<!-- greyfinch-sync:TaynanGT/Vice-Tay:issue:5526189530 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/245). Originally posted by [mint17](https://github.com/mint17) on 2026-11-27T11:55:15Z. --- 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).
TaynanGT commented 2026-11-27 11:58:17 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T11:58:17Z.


@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.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7217685850 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/245#issuecomment-7217685850). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T11:58:17Z. --- @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 commented 2026-11-30 17:15:12 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-30T17:15:12Z.


main has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current main and open a new PR; I'll review it there.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7285965864 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/245#issuecomment-7285965864). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-30T17:15:12Z. --- `main` has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current `main` and open a new PR; I'll review it there.

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
TaynanGT/Vice-Tay!245
No description provided.