GPU: a draw's table entries are looked up once #3059

Open
0xkn0wh3r3 wants to merge 1 commit from upstream-pr/3061 into main
0xkn0wh3r3 commented 2026-11-26 05:52:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-26T05:52:05Z.


Each draw's descriptor tables did two lookups per entry where one is enough. This is on the per-draw path (tables and readable in draw.rs).

Changes

  • tables: it looked up every entry's heap and slot to find tables that need making afresh, then looked them up again to make each entry. Tables are only made afresh for slots past a host heap that's smaller than its title heap (a host's cap, or a paged heap's). The new Heap::capped() says whether a heap is in that state. With no such heap bound, which is the usual case, each entry is made directly.
  • readable: a constant view's copy was found for its first byte, then found again for the range up to that copy's end. The second lookup returns the same copy: the innermost buffer, or the same 4 MiB window of loose memory. Now it's one lookup.

Testing

  • New test the_copy_holding_a_views_first_byte_holds_it_to_its_end, for the invariant the second change relies on. It checks an inner buffer, its outer buffer, a range running past the inner buffer's end, and loose memory.

  • The new test passes, and victory-gpu's draw:: and descriptor:: tests give the same results with and without this change. Both were cross-compiled for Windows in release and run under Wine and CrossOver on macOS. Not run on Windows.

  • Measured with the conformance demo from #3021, on the fork's demo branch (CrossOver, 720p, no overlay):

    • scale 64 (4,047 draws a frame): the title's recording time fell from 5.14–5.16 ms to 4.93–4.94 ms a frame, about 4%;
    • scale 128 (8,143 draws): the before and after runs overlap.

    It's a small win. Most of what's left per draw is making each host view (HostHeap::mirror) and queueing its refresh.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5504798728 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3061). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-26T05:52:05Z. --- Each draw's descriptor tables did two lookups per entry where one is enough. This is on the per-draw path (`tables` and `readable` in `draw.rs`). ## Changes - **`tables`:** it looked up every entry's heap and slot to find tables that need making afresh, then looked them up again to make each entry. Tables are only made afresh for slots past a host heap that's smaller than its title heap (a host's cap, or a paged heap's). The new `Heap::capped()` says whether a heap is in that state. With no such heap bound, which is the usual case, each entry is made directly. - **`readable`:** a constant view's copy was found for its first byte, then found again for the range up to that copy's end. The second lookup returns the same copy: the innermost buffer, or the same 4 MiB window of loose memory. Now it's one lookup. ## Testing - New test `the_copy_holding_a_views_first_byte_holds_it_to_its_end`, for the invariant the second change relies on. It checks an inner buffer, its outer buffer, a range running past the inner buffer's end, and loose memory. - The new test passes, and `victory-gpu`'s `draw::` and `descriptor::` tests give the same results with and without this change. Both were cross-compiled for Windows in release and run under Wine and CrossOver on macOS. Not run on Windows. - Measured with the conformance demo from #3021, on the fork's `demo` branch (CrossOver, 720p, no overlay): - scale 64 (4,047 draws a frame): the title's recording time fell from 5.14–5.16 ms to 4.93–4.94 ms a frame, about 4%; - scale 128 (8,143 draws): the before and after runs overlap. It's a small win. Most of what's left per draw is making each host view (`HostHeap::mirror`) and queueing its refresh.
fuzeofficlal commented 2026-11-26 05:55:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T05:55:36Z.

Review: APPROVED


Reviewed head d427676c line-by-line (CI green):

  • In draw::State::tables, fresh is only ever populated when index >= host.count(), which requires a bound heap with host.count() < desc.count (Heap::capped()). Checking capped up front avoids the redundant table-entry pass when no bound heap is capped.
  • In readable (View::Constant), shadow::find(address, 1) returns the innermost registered buffer (or loose-memory window) containing address, and len = size.min(s.left(address)) stays inside that copy, so reusing s eliminates the second shadow::find lookup with identical semantics.
  • the_copy_holding_a_views_first_byte_holds_it_to_its_end verifies inner-inside-outer buffers, outer buffers, and loose-memory windows.

Clean hot-path optimization.

<!-- greyfinch-sync:Marvisak/VIctory:review:6069202582 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3061#pullrequestreview-6069202582). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T05:55:36Z. **Review:** APPROVED --- Reviewed head `d427676c` line-by-line (CI green): - In `draw::State::tables`, `fresh` is only ever populated when `index >= host.count()`, which requires a bound heap with `host.count() < desc.count` (`Heap::capped()`). Checking `capped` up front avoids the redundant table-entry pass when no bound heap is capped. - In `readable` (`View::Constant`), `shadow::find(address, 1)` returns the innermost registered buffer (or loose-memory window) containing `address`, and `len = size.min(s.left(address))` stays inside that copy, so reusing `s` eliminates the second `shadow::find` lookup with identical semantics. - `the_copy_holding_a_views_first_byte_holds_it_to_its_end` verifies inner-inside-outer buffers, outer buffers, and loose-memory windows. Clean hot-path optimization.
kotekompotek commented 2026-11-26 06:01:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T06:01:08Z.

Review: APPROVED


Verified both halves: (1) the capped-heap gate skips the fresh-table path unless a bound heap's host is actually smaller than its title's — the only case tables must be made afresh, so uncapped draws do one lookup per entry as before. (2) The single find is provably equivalent: len = min(size, s.left(address)) keeps the range inside the innermost copy holding the first byte, so the second lookup could only have returned it; the test covers nested inner/outer buffers and loose memory.

<!-- greyfinch-sync:Marvisak/VIctory:review:6069246693 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3061#pullrequestreview-6069246693). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T06:01:08Z. **Review:** APPROVED --- Verified both halves: (1) the capped-heap gate skips the fresh-table path unless a bound heap's host is actually smaller than its title's — the only case tables must be made afresh, so uncapped draws do one lookup per entry as before. (2) The single `find` is provably equivalent: `len = min(size, s.left(address))` keeps the range inside the innermost copy holding the first byte, so the second lookup could only have returned it; the test covers nested inner/outer buffers and loose memory.
Dukestar69 commented 2026-11-26 06:29:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:29:52Z.

Review: APPROVED


Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • Diff review: Short-circuiting State::tables when no bound heap is capped (avoiding double table-entry lookups) and avoiding the redundant second shadow::find call in readable for View::Constant cleanly trims hot-path draw overhead without changing behavior.
  • cargo test -p victory-gpu draw::tests -- --test-threads=1 (RTX 5090 & WARP): 20 passed, 0 failed on both.
  • cargo test -p victory-gpu shadow::tests -- --test-threads=1 (RTX 5090): 10 passed, 0 failed (including the_copy_holding_a_views_first_byte_holds_it_to_its_end).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6069513981 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3061#pullrequestreview-6069513981). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T06:29:52Z. **Review:** APPROVED --- Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP): - Diff review: Short-circuiting `State::tables` when no bound heap is capped (avoiding double table-entry lookups) and avoiding the redundant second `shadow::find` call in `readable` for `View::Constant` cleanly trims hot-path draw overhead without changing behavior. - `cargo test -p victory-gpu draw::tests -- --test-threads=1` (RTX 5090 & WARP): 20 passed, 0 failed on both. - `cargo test -p victory-gpu shadow::tests -- --test-threads=1` (RTX 5090): 10 passed, 0 failed (including `the_copy_holding_a_views_first_byte_holds_it_to_its_end`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Marvisak commented 2026-11-26 07:01:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:17:02Z.


Automated triage for the maintainers.

Merge candidate. This is a small per-draw speed-up with no change in behaviour, as far as I can tell from reading it.

  • State::tables (crates/victory-gpu/src/draw.rs) now skips the first pass when no bound heap is capped(). capped() is a new method on Heap in descriptor.rs: host slot count below the title's. On main an entry only goes to fresh when its index is at or past host.count(), and the index is always below desc.count. So when no heap is capped, fresh would be empty and every entry would go to table_entry anyway. The capped case keeps the old path unchanged.
  • readable no longer looks up a constant view a second time. shadow::find returns the innermost entry (largest start) whose end covers the range. The second lookup asks for a range ending at or before that copy's end, so it cannot return a different, more inner entry. Loose memory stays in the same window. The new test in shadow.rs checks both cases.

CI: check passing. The measured gain in the description is about 4%, on one configuration.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184276063 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3061#issuecomment-7184276063). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T06:17:02Z. --- <!-- victory-triage v1 head=d427676ccc08d36c38f09e19fea2bcc6c0f10fe9 verdict=merge-candidate at=2026-11-26T06:18:10Z --> Automated triage for the maintainers. **Merge candidate.** This is a small per-draw speed-up with no change in behaviour, as far as I can tell from reading it. - `State::tables` (`crates/victory-gpu/src/draw.rs`) now skips the first pass when no bound heap is `capped()`. `capped()` is a new method on `Heap` in `descriptor.rs`: host slot count below the title's. On `main` an entry only goes to `fresh` when its index is at or past `host.count()`, and the index is always below `desc.count`. So when no heap is capped, `fresh` would be empty and every entry would go to `table_entry` anyway. The capped case keeps the old path unchanged. - `readable` no longer looks up a constant view a second time. `shadow::find` returns the innermost entry (largest start) whose end covers the range. The second lookup asks for a range ending at or before that copy's end, so it cannot return a different, more inner entry. Loose memory stays in the same window. The new test in `shadow.rs` checks both cases. CI: `check` passing. The measured gain in the description is about 4%, on one configuration. --- _Generated by [Claude Code](https://claude.ai/code)_
Jackster commented 2026-11-26 16:24:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T16:24:48Z.


Windows 11, 63bcca4 + this PR, the CI job's own commands:

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test --workspace --locked: 872 passed, 0 failed, across 30 suites
  • victory-gpu alone: 232 passed / 0 failed / 1 ignored, the_copy_holding_a_views_first_byte_holds_it_to_its_end included.

Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7197612309 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3061#issuecomment-7197612309). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T16:24:48Z. --- Windows 11, `63bcca4` + this PR, the CI job's own commands: - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test --workspace --locked`: **872 passed, 0 failed**, across 30 suites - `victory-gpu` alone: 232 passed / 0 failed / 1 ignored, `the_copy_holding_a_views_first_byte_holds_it_to_its_end` included. Synthetic only. AI-assisted.
pes7 commented 2026-11-27 15:50:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pes7 on 2026-11-27T15:50:04Z.


A test report: on Windows 11 (NVIDIA), the new test the_copy_holding_a_views_first_byte_holds_it_to_its_end sometimes hangs cargo test -p victory-gpu --lib.

Repeated runs with a 60 s limit (a normal run finishes in about 3 s):

tree runs hung
main 63bcca4 20 0
main + this PR (head d427676) 15 1
main + this PR, --skip the_copy_holding_a_views_first_byte_holds_it_to_its_end 20 0
a larger set with this PR (#3409's branch) 3 1

When it hangs, the tests left running are always among memory::tests::lists_share_a_table_and_copies_keep_their_views, memory::tests::a_submission_that_missed_leaves_the_copies_at_once, memory::tests::bitmap_pool_reuses_and_zeroes_across_resets, resource::tests::*, queue::tests::aliased_present_planes_preserve_the_host_texture_state and watch::tests::concurrent_writes_and_takes_lose_no_write, using all cores. My guess, not verified: the test registers resources with made-up ids (base | 1, base | 2) and loose-memory copies in the process-wide shadow registry while those tests run in parallel, and frees the memory afterwards, so the other tests can meet those entries.

The runtime change itself looked fine: without that test, 20 of 20 runs are clean. I left this PR out of #3409 for now only because of the hang.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7222537645 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3061#issuecomment-7222537645). Originally posted by [pes7](https://github.com/pes7) on 2026-11-27T15:50:04Z. --- A test report: on Windows 11 (NVIDIA), the new test `the_copy_holding_a_views_first_byte_holds_it_to_its_end` sometimes hangs `cargo test -p victory-gpu --lib`. Repeated runs with a 60 s limit (a normal run finishes in about 3 s): | tree | runs | hung | |---|---|---| | `main` `63bcca4` | 20 | 0 | | `main` + this PR (head `d427676`) | 15 | 1 | | `main` + this PR, `--skip the_copy_holding_a_views_first_byte_holds_it_to_its_end` | 20 | 0 | | a larger set with this PR (#3409's branch) | 3 | 1 | When it hangs, the tests left running are always among `memory::tests::lists_share_a_table_and_copies_keep_their_views`, `memory::tests::a_submission_that_missed_leaves_the_copies_at_once`, `memory::tests::bitmap_pool_reuses_and_zeroes_across_resets`, `resource::tests::*`, `queue::tests::aliased_present_planes_preserve_the_host_texture_state` and `watch::tests::concurrent_writes_and_takes_lose_no_write`, using all cores. My guess, not verified: the test registers resources with made-up ids (`base | 1`, `base | 2`) and loose-memory copies in the process-wide shadow registry while those tests run in parallel, and frees the memory afterwards, so the other tests can meet those entries. The runtime change itself looked fine: without that test, 20 of 20 runs are clean. I left this PR out of #3409 for now only because of the hang.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3061:upstream-pr/3061'
git switch 'upstream-pr/3061'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3061'
git switch 'upstream-pr/3061'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3061'
git switch 'upstream-pr/3061'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3061'
git switch 'main'
git merge --squash 'upstream-pr/3061'
git switch 'main'
git merge --ff-only 'upstream-pr/3061'
git switch 'main'
git merge 'upstream-pr/3061'
git push origin 'main'
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
Marvisak/VIctory!3059
No description provided.