Shard HostHeap TextureQueue to eliminate heap-wide lock convoys #390

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

@⁠PetrSeifert on GitHub · 2026-11-28 00:25 UTC · original
PetrSeifert:perf/shard-texture-queue → main · +422 −204 · 1 files · 1 commits · open

Summary

Replaces the single heap-wide Mutex<TextureQueue> on HostHeap in crates/victory-gpu/src/descriptor/host.rs with 32 cache-line-padded BTreeMap shards, a cache-line-padded atomic revision, a separate pass mutex for TexturePass / bake certification, and a lock-free seen && queued.is_set(index) fast path in HostHeap::queue_texture.

Motivation

On a 6C/12T Ryzen 5 7600 + RTX 4070 (origin/main 0148258, 500 ms VICTORY_TIMELINE capture in the opening cell scene spanning 11 host Presents), HostHeap::textures was the #1 contended mutex in the process:

  • 180.08 ms total wait time (16.37 ms/frame across 17 threads: victory-list-0..5, victory-heap-sync, victory-records-1..5, victory-views-1..3, victory-aside, and title threads), 122 waits, single stalls up to 11.13 ms (wake-one lock convoys under CPU oversubscription).
  • 159.57 ms (89%) of those waits came from HostHeap::queue_texture < HostHeap::note_buffer < HostHeap::mirror.

Design & Correctness

  • 32 cache-line-padded shards ((index >> 4) & 31): groups 16 adjacent slots per shard so contiguous table/remap walks touch few shards while concurrent workers spread across all 32 shards. Each shard keeps BTreeMap with single-lookup Entry upserts (preserving deterministic diagnostic ordering and avoiding HashMap capacity bloat after world load).
  • Bake certification soundness: written and changed_* advance the atomic revision (SeqCst) while holding the shard lock, before releasing it. For every shard, a concurrent write is either wholly before snapshot()'s visit (so snapshot() sees it) or its revision bump lands after TexturePass::begin() (so finish_bake() rejects certification).
  • queue_texture fast path: adds Queued::is_set(index) (returns false when the bitmap is empty/unknown, unlike conservative Queued::may(index)) so seen == true observations skip the shard lock when the slot is already queued.
  • Deadlock freedom: no path holds more than one shard lock or combines a shard lock with pass.

Measurements (Ryzen 5 7600 6C/12T, RTX 4070 12 GB, 1080p, no RT)

Standalone A/B against origin/main 0148258 with identical switches (6/2/2 threads, VICTORY_PORT_GUARD=1, VICTORY_PORT_GUARD_PLAN=0, VICTORY_PORT_JOURNAL=1, VICTORY_STORE_BESIDE=1, VICTORY_COPY_BESIDE=1, VICTORY_BCPACK_TABLES set):

500 ms VICTORY_TIMELINE (diag + diagnostics, cell at 162s)

Metric origin/main (0148258) perf/shard-texture-queue Delta
TextureQueue waits (500 ms) 180.08 ms (16.37 ms/f, 122 waits, max 11.13 ms) 11.56 ms (1.16 ms/f, 8 waits, max 3.01 ms) -93.6% (-15.2 ms/f)
HostHeap::mirror waits 64.28 ms (5.84 ms/f) 20.12 ms (2.01 ms/f) -65.6%
a long list call (all threads) 67.23 ms/f (longest 14.17 ms) 56.36 ms/f (longest 7.96 ms) -10.87 ms/f (-16.2%)
a landing's job (all threads) 35.93 ms/f (longest 10.65 ms) 30.62 ms/f (longest 5.03 ms) -5.31 ms/f (-14.8%)
victory-list-0..5 CPU run time 139.8 ms/f 127.8 ms/f -12.0 ms/f
woken, not running yet (critical chain) 3.81 ms/f 1.42 ms/f -2.39 ms/f

Release build (release + mimalloc + thin-LTO + cgu=1, full 185s intro + diner + cell video)

  • Diner (50–115s): prep 40.17 ms -> 37.79 ms (-2.38 ms); mean FPS 19.13 -> 18.36 (within scene/streaming variance).
  • Cell (130–182s): prep 32.76 ms -> 32.42 ms (-0.34 ms; copies 11.60 -> 10.92 ms); mean FPS 24.17 (max 24.70) -> 23.77 (max 24.40) — within run-to-run noise on an oversubscribed 6-core CPU, where removing this convoy exposes the next contended locks (placed_texture, guards_shared) and the submit/landing/GPU critical chain.
  • Visuals: Both diag and release full-run recordings inspected across diner and cell; zero visual regressions.

Verification

  • cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated (default and --features diagnostics): clean.
  • cargo test -p victory-gpu --lib -- --test-threads=1: 528/528 passed (default) and 595/595 passed (--features diagnostics), including new live concurrent race test sharded_texture_queue_handles_concurrent_writes_across_shards_and_invalidates_bake. (--test-threads=1 used due to pre-existing parallel test flake in raw_title_copy_keeps_write_watch_and_resends_equal_bytes.)
  • cargo run -p xtask -- policy --staged: passed (1 files ok).
**@⁠PetrSeifert** on GitHub · 2026-11-28 00:25 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/390) `PetrSeifert:perf/shard-texture-queue` → `main` · +422 −204 · 1 files · 1 commits · open ## Summary Replaces the single heap-wide `Mutex<TextureQueue>` on `HostHeap` in `crates/victory-gpu/src/descriptor/host.rs` with 32 cache-line-padded `BTreeMap` shards, a cache-line-padded atomic `revision`, a separate `pass` mutex for `TexturePass` / bake certification, and a lock-free `seen && queued.is_set(index)` fast path in `HostHeap::queue_texture`. ## Motivation On a 6C/12T Ryzen 5 7600 + RTX 4070 (`origin/main` `0148258`, 500 ms `VICTORY_TIMELINE` capture in the opening cell scene spanning 11 host Presents), `HostHeap::textures` was the #1 contended mutex in the process: - **180.08 ms** total wait time (**16.37 ms/frame** across 17 threads: `victory-list-0..5`, `victory-heap-sync`, `victory-records-1..5`, `victory-views-1..3`, `victory-aside`, and title threads), 122 waits, single stalls up to **11.13 ms** (wake-one lock convoys under CPU oversubscription). - 159.57 ms (89%) of those waits came from `HostHeap::queue_texture < HostHeap::note_buffer < HostHeap::mirror`. ## Design & Correctness - **32 cache-line-padded shards** (`(index >> 4) & 31`): groups 16 adjacent slots per shard so contiguous table/remap walks touch few shards while concurrent workers spread across all 32 shards. Each shard keeps `BTreeMap` with single-lookup `Entry` upserts (preserving deterministic diagnostic ordering and avoiding `HashMap` capacity bloat after world load). - **Bake certification soundness**: `written` and `changed_*` advance the atomic `revision` (`SeqCst`) *while holding the shard lock*, before releasing it. For every shard, a concurrent write is either wholly before `snapshot()`'s visit (so `snapshot()` sees it) or its revision bump lands after `TexturePass::begin()` (so `finish_bake()` rejects certification). - **`queue_texture` fast path**: adds `Queued::is_set(index)` (returns `false` when the bitmap is empty/unknown, unlike conservative `Queued::may(index)`) so `seen == true` observations skip the shard lock when the slot is already queued. - **Deadlock freedom**: no path holds more than one shard lock or combines a shard lock with `pass`. ## Measurements (Ryzen 5 7600 6C/12T, RTX 4070 12 GB, 1080p, no RT) Standalone A/B against `origin/main` `0148258` with identical switches (`6/2/2` threads, `VICTORY_PORT_GUARD=1`, `VICTORY_PORT_GUARD_PLAN=0`, `VICTORY_PORT_JOURNAL=1`, `VICTORY_STORE_BESIDE=1`, `VICTORY_COPY_BESIDE=1`, `VICTORY_BCPACK_TABLES` set): ### 500 ms `VICTORY_TIMELINE` (`diag + diagnostics`, cell at 162s) | Metric | `origin/main` (`0148258`) | `perf/shard-texture-queue` | Delta | |---|---:|---:|---:| | `TextureQueue` waits (500 ms) | 180.08 ms (`16.37 ms/f`, 122 waits, max 11.13 ms) | **11.56 ms** (`1.16 ms/f`, 8 waits, max 3.01 ms) | **-93.6%** (-15.2 ms/f) | | `HostHeap::mirror` waits | 64.28 ms (`5.84 ms/f`) | **20.12 ms** (`2.01 ms/f`) | **-65.6%** | | `a long list call` (all threads) | 67.23 ms/f (longest 14.17 ms) | **56.36 ms/f** (longest 7.96 ms) | **-10.87 ms/f (-16.2%)** | | `a landing's job` (all threads) | 35.93 ms/f (longest 10.65 ms) | **30.62 ms/f** (longest 5.03 ms) | **-5.31 ms/f (-14.8%)** | | `victory-list-0..5` CPU run time | 139.8 ms/f | **127.8 ms/f** | **-12.0 ms/f** | | `woken, not running yet` (critical chain) | 3.81 ms/f | **1.42 ms/f** | **-2.39 ms/f** | ### Release build (`release + mimalloc + thin-LTO + cgu=1`, full 185s intro + diner + cell video) - **Diner (50–115s)**: `prep` 40.17 ms -> **37.79 ms** (-2.38 ms); mean FPS 19.13 -> 18.36 (within scene/streaming variance). - **Cell (130–182s)**: `prep` 32.76 ms -> **32.42 ms** (-0.34 ms; `copies` 11.60 -> 10.92 ms); mean FPS 24.17 (max 24.70) -> 23.77 (max 24.40) — within run-to-run noise on an oversubscribed 6-core CPU, where removing this convoy exposes the next contended locks (`placed_texture`, `guards_shared`) and the submit/landing/GPU critical chain. - **Visuals**: Both `diag` and `release` full-run recordings inspected across diner and cell; zero visual regressions. ## Verification - `cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated` (default and `--features diagnostics`): clean. - `cargo test -p victory-gpu --lib -- --test-threads=1`: **528/528 passed** (default) and **595/595 passed** (`--features diagnostics`), including new live concurrent race test `sharded_texture_queue_handles_concurrent_writes_across_shards_and_invalidates_bake`. (`--test-threads=1` used due to pre-existing parallel test flake in `raw_title_copy_keeps_write_watch_and_resends_equal_bytes`.) - `cargo run -p xtask -- policy --staged`: passed (`1 files ok`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 00:26:57 +00:00
Author
Owner

@⁠akimfomin on GitHub · 2026-11-28 00:53 UTC · original

PortBridge checked 59563b9 against Vice-Tay main fe7c6e5 and with the current #388 and #357 heads. Those trees are Git-clean. One compatibility test is especially useful for the new publication scheme: AnyXboxX #517 explicitly treats its early heap bake as advisory and still repeats a mandatory pass. In your concurrent shard fixture, can a descriptor rewrite between the early snapshot and final bake prove that the final submitted descriptor is the newer one, even when it lands on a different shard? This complements the enqueue race control with a producing-versus-consuming revision check.

Exact comparisons and controls. If it helps this workflow, your team or agent can submit another public PR URL, target SHA and behavior to preserve via PortBridge. This is an optional, no-install check.

**@⁠akimfomin** on GitHub · 2026-11-28 00:53 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/390#issuecomment-7231272072) PortBridge checked `59563b9` against Vice-Tay main `fe7c6e5` and with the current #388 and #357 heads. Those trees are Git-clean. One compatibility test is especially useful for the new publication scheme: [AnyXboxX #517](https://github.com/LooperSalty/AnyXboxX/pull/517) explicitly treats its early heap bake as advisory and still repeats a mandatory pass. In your concurrent shard fixture, can a descriptor rewrite between the early snapshot and final bake prove that the final submitted descriptor is the newer one, even when it lands on a different shard? This complements the enqueue race control with a producing-versus-consuming revision check. [Exact comparisons and controls](https://github.com/akimfomin/PortBridge/tree/2b57ac90d61e3385a521dd70c1b7803bdb65fadc/audit/evidence/decision-followthrough/parallel-ops-wave-two-2026-11-28). If it helps this workflow, your team or agent can submit another public PR URL, target SHA and behavior to preserve via [PortBridge](https://github.com/akimfomin/PortBridge/issues/98). This is an optional, no-install check.
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/390/head:github-bot/gh-390'
git switch 'github-bot/gh-390'

Merge

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