perf: texture reads ask the texture, not process-wide tables under a lock #444

Merged
KainanP merged 1 commit from upstream-pr/444 into main 2026-11-27 15:42:39 +00:00
KainanP commented 2026-11-26 18:53:41 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-26T18:53:41Z.


Re-ported onto the Vice-Tay base (#454), on main b6d86cf8, with numbers measured there. The Neptune-base version is kept on KainanP/AnyXboxX:perf/texture-read-locks-neptune, the combined-base one on perf/texture-read-locks-combined.

Port of the idea of Marvisak/VIctory#3274 (mine) to this tree. There the contended lock was TEXTURES_AT in alias_source; on Vice-Tay a draw's texture read still takes up to four process-wide mutexes that every recording thread shares (and, with the launcher's VICTORY_THIN=1, all eight translating threads), so I applied the same "ask the texture, not the shared table" approach to each.

The problem

Per texture a draw reads through a view (draw::readable):

lock where when
PLACED_TEXTURES resource::texture_at (via descriptor::host::placed_texture) every view the title places in its memory without a resource object (device 0x230: its streamed textures); a BTreeMap lookup keyed by address and 14 words
TARGETS_AT resource::alias_of every placed texture the title fills
HOST_WRITTEN texture::upload_from every texture read
STAGING texture::staged_from in upload_from every read of a texture the list already used

The change

  • Placed views: each thread keeps the placed textures it found (256 slots, by address and description as asked). Nothing takes a texture out of PLACED_TEXTURES on this base, so what a thread found holds.
  • alias_of: a texture keeps its "no target at my memory" answer together with the change count of its memory's bucket (1,024 counters by 64 KiB block, bumped under TARGETS_AT's lock when a target enters the table). While the count is unchanged the answer stands without the lock; a target leaving the table can't make "none" wrong. A texture that has a target is looked up as before.
  • Host-written and staged level: each texture keeps whether the host wrote it (set together with the set entry) and the first level its staging buffer holds (written under STAGING's lock wherever the table gains or loses the entry: staging, unstage, the idle sweep; forget runs as the texture goes). upload_from reads them without the locks.
  • texture_at kept its raw description only in diagnostics builds; it is now always kept (the per-thread slots are keyed by it).

Same answers as the locked lookups; a change racing a read is the same race as one landing right after the locked lookup returned. The tables stay for every other reader.

Measured

List bench of #441, draw sequence (root CBV, 4 root constants, index buffer, indexed draw reading six textures), 1 and 5 threads at once, each thread its own list, all reading the same textures. Best of 5 × 20,000 draws per thread; 7 runs per build interleaved, median [min–max]. RTX 4060 Laptop GPU, Core i7-14650HX, Windows 11, release profile. Quiet machine: no game running, Windows reported 0–31 % CPU load around the runs (mostly under 16 %).

ns a draw, per thread:

tree resource views, 1 / 5 threads placed views, 1 / 5 threads
main 1340 / 3267 [3097–3358] 1565 / 4315 [4008–4472]
main + this PR 1273 / 2689 [2604–2797] 1530 / 2796 [2607–2849]
main + #443 1134 / 2629 [2456–2731] 1384 / 3627 [3572–3664]
main + #443 + this PR 1089 / 2156 [2052–2292] 1298 / 2347 [2290–2488]

So on five threads: −18 % alone (−35 % with placed views), −18 % on top of #443 (−35 % with placed views). Five threads now cost about 2.0× one thread (1.8× with placed views), against 2.4× (2.8×) on main.

Tests

New, texture/read_marks_tests.rs:

  • a_textures_staged_level_follows_its_staging_buffer: made by the first upload, gone when the buffer is let go, equal to the table's answer each time.
  • a_texture_the_host_wrote_is_marked_and_not_uploaded.
  • a_texture_read_again_takes_neither_tables_lock: a read again in a list, from another first level, finishes while another thread holds HOST_WRITTEN and STAGING.
  • a_placed_texture_finds_a_target_made_at_its_memory_later: "none" kept (and given while another thread holds TARGETS_AT), then a render target made at the same memory is found, then "none" once it is released.
  • placed_textures_found_again_are_the_tables: same resource each time (filler words ignored as before), found again while another thread holds PLACED_TEXTURES.

Checked against mutations on this base: without the bump on a target's entry, without the reset in unstage, or with upload_from asking STAGING again, the matching test fails (3 of 5 fail in that build; the other two pin behaviour).

cargo test -p victory-gpu --release --lib, one thread: RTX 4060 Laptop 426 passed (main 421); with #441/#443 too, Intel UHD (a78b, VICTORY_D3D12_ADAPTER=Intel) 428 passed (main 421) and WARP (--features diagnostics, VICTORY_D3D12_WARP=1) the same single failure as main (timeline::report::tests::real_waits_are_recorded_with_who_ended_them). Clippy (--all-targets, with and without diagnostics): only main's warning. cargo xtask policy ok, ruff clean.

What this does not show

No in-game numbers: I don't run the title here. How much it matters depends on how many threads translate at once and how many placed, title-filled textures each draw reads; in the bench all five threads read the same six. Please A/B it in game, ideally with #443 and VICTORY_THIN=1 (eight translating threads).

Memory: about 18 KB per thread that reads placed views (256 slots), 4 KB of counters, 12 bytes per title resource.

Overlaps

git merge-tree against the open PRs that touch third_party/victory and merge with main: no conflict. #441 and #443 merge with this in any order (built and tested together).

Ideas credited: caching lookups that title threads repeat was also the approach of wbkepler's Marvisak/VIctory#1356 (per-HostList VA/view caches), and moving title-thread lookups off shared mutexes the aim of 0xkn0wh3r3's Marvisak/VIctory#2821. No code is taken from either.

AI use: Claude Code (Claude Opus 5.5) ported the change, wrote the tests, ran the measurements and drafted this description.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5515583028 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/444). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-26T18:53:41Z. --- > **Re-ported onto the Vice-Tay base (#454)**, on main b6d86cf8, with numbers measured there. The Neptune-base version is kept on [`KainanP/AnyXboxX:perf/texture-read-locks-neptune`](https://github.com/KainanP/AnyXboxX/tree/perf/texture-read-locks-neptune), the combined-base one on [`perf/texture-read-locks-combined`](https://github.com/KainanP/AnyXboxX/tree/perf/texture-read-locks-combined). Port of the idea of Marvisak/VIctory#3274 (mine) to this tree. There the contended lock was `TEXTURES_AT` in `alias_source`; on Vice-Tay a draw's texture read still takes up to **four** process-wide mutexes that every recording thread shares (and, with the launcher's `VICTORY_THIN=1`, all eight translating threads), so I applied the same "ask the texture, not the shared table" approach to each. ## The problem Per texture a draw reads through a view (`draw::readable`): | lock | where | when | |---|---|---| | `PLACED_TEXTURES` | `resource::texture_at` (via `descriptor::host::placed_texture`) | every view the title places in its memory without a resource object (device 0x230: its streamed textures); a `BTreeMap` lookup keyed by address and 14 words | | `TARGETS_AT` | `resource::alias_of` | every placed texture the title fills | | `HOST_WRITTEN` | `texture::upload_from` | every texture read | | `STAGING` | `texture::staged_from` in `upload_from` | every read of a texture the list already used | ## The change - **Placed views**: each thread keeps the placed textures it found (256 slots, by address and description as asked). Nothing takes a texture out of `PLACED_TEXTURES` on this base, so what a thread found holds. - **`alias_of`**: a texture keeps its "no target at my memory" answer together with the change count of its memory's bucket (1,024 counters by 64 KiB block, bumped under `TARGETS_AT`'s lock when a target enters the table). While the count is unchanged the answer stands without the lock; a target leaving the table can't make "none" wrong. A texture that has a target is looked up as before. - **Host-written and staged level**: each texture keeps whether the host wrote it (set together with the set entry) and the first level its staging buffer holds (written under `STAGING`'s lock wherever the table gains or loses the entry: `staging`, `unstage`, the idle sweep; `forget` runs as the texture goes). `upload_from` reads them without the locks. - `texture_at` kept its `raw` description only in diagnostics builds; it is now always kept (the per-thread slots are keyed by it). Same answers as the locked lookups; a change racing a read is the same race as one landing right after the locked lookup returned. The tables stay for every other reader. ## Measured List bench of #441, draw sequence (root CBV, 4 root constants, index buffer, indexed draw reading six textures), 1 and 5 threads at once, each thread its own list, all reading the same textures. Best of 5 × 20,000 draws per thread; 7 runs per build interleaved, median [min–max]. RTX 4060 Laptop GPU, Core i7-14650HX, Windows 11, release profile. Quiet machine: no game running, Windows reported 0–31 % CPU load around the runs (mostly under 16 %). ns a draw, per thread: | tree | resource views, 1 / 5 threads | placed views, 1 / 5 threads | |---|---:|---:| | main | 1340 / 3267 [3097–3358] | 1565 / 4315 [4008–4472] | | main + this PR | 1273 / **2689** [2604–2797] | 1530 / **2796** [2607–2849] | | main + #443 | 1134 / 2629 [2456–2731] | 1384 / 3627 [3572–3664] | | main + #443 + this PR | 1089 / **2156** [2052–2292] | 1298 / **2347** [2290–2488] | So on five threads: −18 % alone (−35 % with placed views), −18 % on top of #443 (−35 % with placed views). Five threads now cost about 2.0× one thread (1.8× with placed views), against 2.4× (2.8×) on main. ## Tests New, `texture/read_marks_tests.rs`: - `a_textures_staged_level_follows_its_staging_buffer`: made by the first upload, gone when the buffer is let go, equal to the table's answer each time. - `a_texture_the_host_wrote_is_marked_and_not_uploaded`. - `a_texture_read_again_takes_neither_tables_lock`: a read again in a list, from another first level, finishes while another thread holds `HOST_WRITTEN` and `STAGING`. - `a_placed_texture_finds_a_target_made_at_its_memory_later`: "none" kept (and given while another thread holds `TARGETS_AT`), then a render target made at the same memory is found, then "none" once it is released. - `placed_textures_found_again_are_the_tables`: same resource each time (filler words ignored as before), found again while another thread holds `PLACED_TEXTURES`. Checked against mutations on this base: without the bump on a target's entry, without the reset in `unstage`, or with `upload_from` asking `STAGING` again, the matching test fails (3 of 5 fail in that build; the other two pin behaviour). `cargo test -p victory-gpu --release --lib`, one thread: RTX 4060 Laptop 426 passed (main 421); with #441/#443 too, Intel UHD (a78b, `VICTORY_D3D12_ADAPTER=Intel`) 428 passed (main 421) and WARP (`--features diagnostics`, `VICTORY_D3D12_WARP=1`) the same single failure as main (`timeline::report::tests::real_waits_are_recorded_with_who_ended_them`). Clippy (`--all-targets`, with and without `diagnostics`): only main's warning. `cargo xtask policy` ok, ruff clean. ## What this does not show No in-game numbers: I don't run the title here. How much it matters depends on how many threads translate at once and how many placed, title-filled textures each draw reads; in the bench all five threads read the same six. **Please A/B it in game**, ideally with #443 and `VICTORY_THIN=1` (eight translating threads). Memory: about 18 KB per thread that reads placed views (256 slots), 4 KB of counters, 12 bytes per title resource. ## Overlaps `git merge-tree` against the open PRs that touch `third_party/victory` and merge with main: no conflict. #441 and #443 merge with this in any order (built and tested together). Ideas credited: caching lookups that title threads repeat was also the approach of wbkepler's Marvisak/VIctory#1356 (per-HostList VA/view caches), and moving title-thread lookups off shared mutexes the aim of 0xkn0wh3r3's Marvisak/VIctory#2821. No code is taken from either. _AI use: Claude Code (Claude Opus 5.5) ported the change, wrote the tests, ran the measurements and drafted this description._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
KainanP commented 2026-11-26 20:21:55 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-26T20:21:55Z.


Re-ported onto the combined base (#409) and force-pushed: the same four shared locks are on a texture read there. List bench of #441, median of 7 interleaved runs, RTX 4060 Laptop, draw sequence on 5 threads: 5597 → 4919 ns alone (placed views 5192 → 3345, tuned set 3306 → 2704), and with #443 3955 ns (placed 2805, tuned 2125). The new tests hold each table's lock on one thread and read on another; with main's locked lookups they fail. Description updated.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7202588186 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/444#issuecomment-7202588186). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-26T20:21:55Z. --- Re-ported onto the combined base (#409) and force-pushed: the same four shared locks are on a texture read there. List bench of #441, median of 7 interleaved runs, RTX 4060 Laptop, draw sequence on 5 threads: 5597 → 4919 ns alone (placed views 5192 → 3345, tuned set 3306 → 2704), and with #443 3955 ns (placed 2805, tuned 2125). The new tests hold each table's lock on one thread and read on another; with main's locked lookups they fail. Description updated. _AI use: Claude Code (Claude Opus 5.5)._
KainanP commented 2026-11-26 20:46:12 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-26T20:46:12Z.


Main reverted #409 (#452), so this is back to the Neptune-base version measured in the original body, rebased onto 15024d5 (RTX: its tests pass, and the suite gives the same results as before). The combined-base port is kept on branch perf/texture-read-locks-combined for when it returns.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7203054037 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/444#issuecomment-7203054037). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-26T20:46:12Z. --- Main reverted #409 (#452), so this is back to the Neptune-base version measured in the original body, rebased onto 15024d5 (RTX: its tests pass, and the suite gives the same results as before). The combined-base port is kept on branch [`perf/texture-read-locks-combined`](https://github.com/KainanP/AnyXboxX/tree/perf/texture-read-locks-combined) for when it returns. _AI use: Claude Code (Claude Opus 5.5)._
KainanP commented 2026-11-26 23:48:36 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-26T23:48:36Z.


Re-ported onto the Vice-Tay base (#454): all four shared locks of a texture read are still there on that base. On b6d86cf8 (list bench of #441, quiet machine), draw sequence on five threads 3267 → 2689 ns alone (placed views 4315 → 2796), 2156 (2347) with #443. Suites on RTX, Intel UHD and WARP: same results as main; the new tests fail under the matching mutations. Description updated; the Neptune version is on perf/texture-read-locks-neptune.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7205880240 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/444#issuecomment-7205880240). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-26T23:48:36Z. --- Re-ported onto the Vice-Tay base (#454): all four shared locks of a texture read are still there on that base. On b6d86cf8 (list bench of #441, quiet machine), draw sequence on five threads 3267 → 2689 ns alone (placed views 4315 → 2796), 2156 (2347) with #443. Suites on RTX, Intel UHD and WARP: same results as main; the new tests fail under the matching mutations. Description updated; the Neptune version is on [`perf/texture-read-locks-neptune`](https://github.com/KainanP/AnyXboxX/tree/perf/texture-read-locks-neptune). _AI use: Claude Code (Claude Opus 5.5)._
pepezin-ai commented 2026-11-27 02:28:06 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by pepezin-ai on 2026-11-27T02:28:06Z.


A combined data point that may help with merging these, measured on a machine at the 16-thread threshold.

Machine: Ryzen 7 5700X 8C/16T, 32 GB RAM, RTX 5070 12 GB, Windows 11. anyxboxx run with its default settings on main b62053f, VICTORY_OUTPUT=1920x1080, Series X, warm cache, same save and the same opening scene of it (letterboxed, ~7,000 draws a frame), 90 s after pipelines reached 0, one run each:

Build fps mean / median min
main b62053f 7.2 / 7.4 0.9
main + #444 #446 #451 #456 #457 #458 #465 #466 12.1 / 12.9 8.0

All eight merged cleanly on top of b62053f (only README conflicts on our side). No device loss, no new picture faults spotted in screenshots. I cannot say which PR carries most of the gain; happy to run single-PR A/B runs if that helps.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7208149994 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/444#issuecomment-7208149994). Originally posted by [pepezin-ai](https://github.com/pepezin-ai) on 2026-11-27T02:28:06Z. --- A combined data point that may help with merging these, measured on a machine at the 16-thread threshold. **Machine:** Ryzen 7 5700X 8C/16T, 32 GB RAM, RTX 5070 12 GB, Windows 11. `anyxboxx run` with its default settings on `main` b62053f, `VICTORY_OUTPUT=1920x1080`, Series X, warm cache, same save and the same opening scene of it (letterboxed, ~7,000 draws a frame), 90 s after pipelines reached 0, one run each: | Build | fps mean / median | min | |---|---|---| | `main` b62053f | 7.2 / 7.4 | 0.9 | | `main` + #444 #446 #451 #456 #457 #458 #465 #466 | **12.1 / 12.9** | **8.0** | All eight merged cleanly on top of b62053f (only README conflicts on our side). No device loss, no new picture faults spotted in screenshots. I cannot say which PR carries most of the gain; happy to run single-PR A/B runs if that helps.
KainanP commented 2026-11-27 10:20:25 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-27T10:20:25Z.


Thanks @pepezin-ai, that's a very useful data point (7.4 → 12.9 fps median, min 0.9 → 8.0 with the eight PRs together, on a 16-thread CPU). Yes, single-PR runs would help the maintainer pick: if you have time, main + #444 alone, and main + #443 + #444 (#443 is the other half of the texture-read path; both merge cleanly together per akimfomin's check on #441). The synthetic bench predicts the texture-read pair mostly helps the multi-threaded recording (−29 to −46 % per draw on 5 threads), so the gain should show more on CPU-bound scenes like yours.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7215766562 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/444#issuecomment-7215766562). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-27T10:20:25Z. --- Thanks @pepezin-ai, that's a very useful data point (7.4 → 12.9 fps median, min 0.9 → 8.0 with the eight PRs together, on a 16-thread CPU). Yes, single-PR runs would help the maintainer pick: if you have time, `main` + #444 alone, and `main` + #443 + #444 (#443 is the other half of the texture-read path; both merge cleanly together per akimfomin's check on #441). The synthetic bench predicts the texture-read pair mostly helps the multi-threaded recording (−29 to −46 % per draw on 5 threads), so the gain should show more on CPU-bound scenes like yours. _AI use: Claude Code (Claude Opus 5.5)._
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
LooperSalty/AnyXboxX!444
No description provided.