Host retirement: 64 cache-line-aligned shards with lock-free watermark skip (VICTORY_LOCK_TRIM) #433

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

@⁠Pototoooo on GitHub · 2026-12-02 09:35 UTC · original
Pototoooo:perf/sharded-retired-queues → main · +427 −228 · 2 files · 1 commits · open

Summary

Completes RFC #413 patch 09 (part 2, MIT-0) after 618da3c (VICTORY_LOCK_TRIM in watch.rs) and release_dynamic_batch in descriptor.rs:

  • Extracts deferred host retirement (RETIRED, retire, retire_resource, retire_resources, bury, bury_lag, bury_timed) out of crates/victory-gpu/src/host.rs into crates/victory-gpu/src/host/retire.rs (also reducing host.rs by ~10.5 KiB).
  • Replaces the single global RETIRED: Mutex<VecDeque<(u64, Retired)>> with 64 cache-line-aligned (#[repr(align(64))]) per-thread shards (Shard) so concurrent thin-recording, staging, descriptor, and submission threads do not contend or false-share when retiring resources.
  • Tracks each shard's front submission fence in an AtomicU64 watermark updated under the shard lock on push-into-empty and on every drain in bury(), allowing bury() to skip empty or unready shards without taking any lock (VICTORY_LOCK_TRIM=0 disables the watermark skip and checks every shard under its lock, matching watch.rs).

Invariant check

drain_up_to(finished) drains strictly from the front (take_while(|(at, _)| *at <= finished)), so a shard can yield items if and only if its queue is non-empty and front.0 <= finished. Because oldest is updated to q.front().map_or(u64::MAX, |(at, _)| *at) under shard.queue's lock on push-into-empty and after every drain, oldest == u64::MAX || oldest > finished skips locking if and only if take_while would drain zero items.

Verification

  • New unit tests in host::retire::tests:
    • shard_watermark_tracks_front_through_pushes_and_drains (empty, ordered push, overtaking push, partial drain, full drain)
    • shard_alignment_prevents_false_sharing
    • bury_drains_across_multiple_threads_and_shards (8 concurrent producer threads across shards + lag boundary check + async drop)
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test -p victory-gpu -p victory-hle -p victory-core -p victory-pipeline -p victory-loader --locked --lib: all passing (victory-gpu 622, victory-hle 196, victory-loader 60, victory-core 13, victory-pipeline 7; 0 failed)
  • Note: xtask asset-policy check passes on host/retire.rs (17,434 bytes); host.rs shrinks from 717,220 to 706,735 bytes (pre-existing size over the 640 KiB limit on main is unrelated).

After procotos-art's RFC #413 patch 09, part 2 (MIT-0).

**@⁠Pototoooo** on GitHub · 2026-12-02 09:35 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/433) `Pototoooo:perf/sharded-retired-queues` → `main` · +427 −228 · 2 files · 1 commits · open ## Summary Completes RFC #413 patch 09 (part 2, MIT-0) after `618da3c` (`VICTORY_LOCK_TRIM` in `watch.rs`) and `release_dynamic_batch` in `descriptor.rs`: - Extracts deferred host retirement (`RETIRED`, `retire`, `retire_resource`, `retire_resources`, `bury`, `bury_lag`, `bury_timed`) out of `crates/victory-gpu/src/host.rs` into `crates/victory-gpu/src/host/retire.rs` (also reducing `host.rs` by ~10.5 KiB). - Replaces the single global `RETIRED: Mutex<VecDeque<(u64, Retired)>>` with 64 cache-line-aligned (`#[repr(align(64))]`) per-thread shards (`Shard`) so concurrent thin-recording, staging, descriptor, and submission threads do not contend or false-share when retiring resources. - Tracks each shard's front submission fence in an `AtomicU64` watermark updated under the shard lock on push-into-empty and on every drain in `bury()`, allowing `bury()` to skip empty or unready shards without taking any lock (`VICTORY_LOCK_TRIM=0` disables the watermark skip and checks every shard under its lock, matching `watch.rs`). ## Invariant check `drain_up_to(finished)` drains strictly from the front (`take_while(|(at, _)| *at <= finished)`), so a shard can yield items if and only if its queue is non-empty and `front.0 <= finished`. Because `oldest` is updated to `q.front().map_or(u64::MAX, |(at, _)| *at)` under `shard.queue`'s lock on push-into-empty and after every drain, `oldest == u64::MAX || oldest > finished` skips locking if and only if `take_while` would drain zero items. ## Verification - New unit tests in `host::retire::tests`: - `shard_watermark_tracks_front_through_pushes_and_drains` (empty, ordered push, overtaking push, partial drain, full drain) - `shard_alignment_prevents_false_sharing` - `bury_drains_across_multiple_threads_and_shards` (8 concurrent producer threads across shards + lag boundary check + async drop) - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test -p victory-gpu -p victory-hle -p victory-core -p victory-pipeline -p victory-loader --locked --lib`: all passing (`victory-gpu` 622, `victory-hle` 196, `victory-loader` 60, `victory-core` 13, `victory-pipeline` 7; 0 failed) - Note: `xtask` asset-policy check passes on `host/retire.rs` (`17,434` bytes); `host.rs` shrinks from `717,220` to `706,735` bytes (pre-existing size over the 640 KiB limit on `main` is unrelated). After procotos-art's RFC #413 patch 09, part 2 (MIT-0).
Move deferred retirement (RETIRED, retire*, bury, bury_lag, bury_timed) out of host.rs into host/retire.rs and replace the single global Mutex<VecDeque> with 64 cache-line-aligned shards assigned per thread. Each shard tracks the submission number of its front item in an AtomicU64 updated under the shard lock on push-into-empty and drain, so bury() skips empty or unready shards without locking (VICTORY_LOCK_TRIM=0 checks every shard under its lock).

Tests: shard_watermark_tracks_front_through_pushes_and_drains, shard_alignment_prevents_false_sharing, bury_drains_across_multiple_threads_and_shards; full host:: suite (162 passed), clippy default and --all-features clean.

After procotos-art's RFC #413 patch 09, part 2 (MIT-0).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 09:35:27 +00:00
Author
Owner

@⁠GValdezN on GitHub · 2026-12-02 16:51 UTC · original

Good refactor — extracting retirement into its own module makes the code much more navigable, and the per-shard watermark skip is the right pattern (same philosophy as our summary bitmap in #472 — check a cheap atomic before grabbing the lock).

The #[repr(align(64))] on each shard prevents false sharing, which is important since bury() iterates all 64 on the submitting thread. No conflict with our journal sharding or barriers work — different data structures, different files.

One thought: the watermark front_fence skip means bury() only locks shards whose front item might be ready. For the common case where GPU is 2-3 frames ahead, most shards will be skipped — nice.

**@⁠GValdezN** on GitHub · 2026-12-02 16:51 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/433#issuecomment-7352546355) Good refactor — extracting retirement into its own module makes the code much more navigable, and the per-shard watermark skip is the right pattern (same philosophy as our summary bitmap in #472 — check a cheap atomic before grabbing the lock). The `#[repr(align(64))]` on each shard prevents false sharing, which is important since `bury()` iterates all 64 on the submitting thread. No conflict with our journal sharding or barriers work — different data structures, different files. One thought: the watermark `front_fence` skip means `bury()` only locks shards whose front item might be ready. For the common case where GPU is 2-3 frames ahead, most shards will be skipped — nice.
Author
Owner

@⁠Pototoooo on GitHub · 2026-12-03 03:09 UTC · original

Thanks for taking a look, @⁠GValdezN! Agreed — the front_fence atomic watermark keeps bury() lock-free for shards whose front submission isn't ready yet, and isolating retirement into host/retire.rs keeps it cleanly decoupled from the journal and barrier paths.

**@⁠Pototoooo** on GitHub · 2026-12-03 03:09 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/433#issuecomment-7366265619) Thanks for taking a look, @⁠GValdezN! Agreed — the `front_fence` atomic watermark keeps `bury()` lock-free for shards whose front submission isn't ready yet, and isolating retirement into `host/retire.rs` keeps it cleanly decoupled from the journal and barrier paths.
Author
Owner

@⁠tillo88 on GitHub · 2026-12-03 03:44 UTC · original

Replay-bench measurement: the earlier ~3% slowdown did not repeat; no measurable difference on v0.8.3.3

Measured: PR head 4c227c3 merged onto release v0.8.3.3 (same code as main 7dbcdcb), 4 measured runs in two separate jobs (each job after a warm-up run that is not counted), against 8 runs of v0.8.3.3 without the PR, same route (~6.5 min). The PR's default (VICTORY_LOCK_TRIM not set) was used.

run fps CPU prep ms median ms p95 ms p99 ms frames > 100 ms
release (8 runs, mean; fps range 36.15-37.12) 36.60 19.57 28.45 39.70 47.35 14.9
PR #433 run 1 (job 1) 35.21 19.76 30.10 41.18 49.96 20
PR #433 run 2 (job 1) 35.51 19.88 29.35 39.62 46.87 15
PR #433 run 3 (job 2) 36.86 18.70 28.30 38.62 45.55 13
PR #433 run 4 (job 2) 36.47 19.77 28.60 38.52 45.08 10

The first job's two runs were both below the slowest release run (36.15 fps); the repeat job's two runs are inside the release's range. Mean over all 4 PR runs: 36.01 fps vs 36.60 (-1.6%), with 2 of 4 runs inside the release range. That is not enough to call a slowdown, so: no measurable difference on this route. The first job's lower numbers remain unexplained.

Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost).

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default play-foxhound.ps1 settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap.

Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.

**@⁠tillo88** on GitHub · 2026-12-03 03:44 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/433#issuecomment-7366888689) **Replay-bench measurement: the earlier ~3% slowdown did not repeat; no measurable difference on v0.8.3.3** Measured: PR head `4c227c3` merged onto release **v0.8.3.3** (same code as main `7dbcdcb`), 4 measured runs in two separate jobs (each job after a warm-up run that is not counted), against 8 runs of v0.8.3.3 without the PR, same route (~6.5 min). The PR's default (`VICTORY_LOCK_TRIM` not set) was used. | run | fps | CPU prep ms | median ms | p95 ms | p99 ms | frames > 100 ms | |---|---|---|---|---|---|---| | release (8 runs, mean; fps range 36.15-37.12) | 36.60 | 19.57 | 28.45 | 39.70 | 47.35 | 14.9 | | PR #433 run 1 (job 1) | 35.21 | 19.76 | 30.10 | 41.18 | 49.96 | 20 | | PR #433 run 2 (job 1) | 35.51 | 19.88 | 29.35 | 39.62 | 46.87 | 15 | | PR #433 run 3 (job 2) | 36.86 | 18.70 | 28.30 | 38.62 | 45.55 | 13 | | PR #433 run 4 (job 2) | 36.47 | 19.77 | 28.60 | 38.52 | 45.08 | 10 | The first job's two runs were both below the slowest release run (36.15 fps); the repeat job's two runs are inside the release's range. Mean over all 4 PR runs: 36.01 fps vs 36.60 (-1.6%), with 2 of 4 runs inside the release range. That is not enough to call a slowdown, so: **no measurable difference** on this route. The first job's lower numbers remain unexplained. Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost). Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default `play-foxhound.ps1` settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap. Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
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/433/head:github-bot/gh-433'
git switch 'github-bot/gh-433'

Merge

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