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

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

@⁠KainanP on GitHub · 2026-11-26 18:53 UTC · original
KainanP:perf/texture-read-locks → main · +379 −17 · 3 files · 1 commits · open

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

**@⁠KainanP** on GitHub · 2026-11-26 18:53 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/444) `KainanP:perf/texture-read-locks` → `main` · +379 −17 · 3 files · 1 commits · open > **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)
A draw's read of a texture took up to four process-wide mutexes that
every recording thread shares:

- PLACED_TEXTURES (resource::texture_at), for every view the title
  places in its memory without a resource object (its streamed
  textures): a BTreeMap lookup keyed by address and 14 words;
- TARGETS_AT (resource::alias_of), for every placed texture the title
  fills, to find a target at its memory;
- HOST_WRITTEN (texture::upload_from), for every texture the title
  fills;
- STAGING (texture::staged_from in upload_from), for every such read
  after the list's first.

Now:

- each thread keeps the placed textures it found (256 slots by address
  and description as asked). A placed texture stays in the table for
  the run; only tests take one out, and that bumps a count that drops
  every kept answer;
- a texture keeps alias_of's "no target at its memory" answer with the
  change count of its memory's bucket (1,024 counters by 64 KiB block,
  bumped under the table's lock when a target enters it). While the
  count is unchanged the answer stands without the lock; a target
  leaving the table cannot make "none" wrong. A texture with a target
  is looked up as before;
- a texture keeps whether the host wrote it (set with the set entry)
  and the first level its staging buffer holds (written under the
  staging table's lock wherever the table gains or loses the entry),
  read without the locks.

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

Tests (new texture/read_marks_tests.rs): the staged level follows the
staging buffer (made, let go), a host-written texture is marked and not
uploaded, a placed texture finds a target made at its memory after a
"none" was kept (and none once it is gone), and placed textures found
again are the table's (a new one after a test took the old one out).
Each fails with its bump or reset removed.

List bench, RTX 4060 Laptop, release, median of 7 interleaved runs,
draw sequence on 5 threads, ns a draw: alone 8357 -> 6932 (resource
views), 9538 -> 7416 (placed views); with the one-lookup change too
(and the indirect PR, which no draw goes through) 6227 -> 4450 and
7109 -> 4884.

Source: Marvisak/VIctory#3274 (the change-count idea, applied to this
tree's tables)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 18:56:44 +00:00
github-bot force-pushed github-bot/gh-444 from e1c09d8609 to 0ff5f10084 2026-11-26 20:24:52 +00:00 Compare
Author
Owner

@⁠KainanP on GitHub · 2026-11-26 20:21 UTC · original

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** on GitHub · 2026-11-26 20:21 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/444#issuecomment-7202588186) 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)._
github-bot force-pushed github-bot/gh-444 from 0ff5f10084 to 4ede49b37e 2026-11-26 20:51:01 +00:00 Compare
Author
Owner

@⁠KainanP on GitHub · 2026-11-26 20:46 UTC · original

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

**@⁠KainanP** on GitHub · 2026-11-26 20:46 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/444#issuecomment-7203054037) 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)._
github-bot force-pushed github-bot/gh-444 from 4ede49b37e to e6e7c9e983 2026-11-27 00:37:51 +00:00 Compare
Author
Owner

@⁠KainanP on GitHub · 2026-11-26 23:48 UTC · original

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

**@⁠KainanP** on GitHub · 2026-11-26 23:48 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/444#issuecomment-7205880240) 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)._
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/444/head:github-bot/gh-444'
git switch 'github-bot/gh-444'

Merge

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