Skip to content

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

Open
0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:gpu/table-entries-looked-up-once
Open

0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:gpu/table-entries-looked-up-once

Conversation

@0xkn0wh3r3

Copy link
Copy Markdown
Contributor

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 Tests: the conformance title's GPU frame, cargo conformance and cargo demo (with a stress scale), on Windows and macOS #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.

Each draw's descriptor tables went through two lookups per entry where
one does, on the per-draw path (`tables`, `readable`):

- `tables` looked every entry's heap and slot up to find tables to make
  afresh, then again to make each entry. Tables are made afresh only past
  a host heap smaller than its title heap (Heap::capped: a host's cap, a
  paged heap's); with no such heap bound, as on most hosts, each entry is
  made directly.
- A constant view's copy was found for its first byte, then again for the
  range up to that copy's end, which finds the same copy (the innermost
  buffer, or the same window of loose memory). One lookup now.

the_copy_holding_a_views_first_byte_holds_it_to_its_end checks the
second: in an inner buffer, in its outer one, past the inner one's end,
and in loose memory.

Measured with `cargo demo` (CrossOver, M5 Max, 720p, no overlay): the
title's recording time fell from 5.14-5.16 to 4.93-4.94 ms a frame at
scale 64 (4,047 draws, -4%); at scale 128 the runs overlap (9.97-10.01
before, 9.87-10.09 after). victory-gpu's draw and descriptor tests give
the same results as before under Wine.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
A draw's table entries are looked up once (as Marvisak#3061 on main): no
fresh-table pass without a capped heap, one copy lookup a constant view.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner

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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Jackster

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants