GPU: a draw reads the swizzle it already has, and the release frame line costs nothing #3238

Open
github-bot wants to merge 2 commits from github-bot/gh-3238 into main AGit
Owner

@⁠borosf on GitHub · 2026-11-26 14:14 UTC · original
borosf:vibe/frame-waste-cluster-842ca5 → main · +161 −27 · 3 files · 2 commits · open

Four pieces of per-draw waste found by a sweep of the world frame, all of them work that is computed and then thrown away. No behaviour change: the same values, read instead of rebuilt.

What each one cost

A placed texture's swizzle was derived by building the whole console layout. texture_layout builds a texture::Texture — three heap allocations plus one Mip per level — to answer with a single u32. Two per-draw paths did it:

  • memory_key, which current() reaches for every placed texture a heap slot reads (texture_at → current → memory_key) and shared_texture reaches for every title texture of one memory;
  • alias_source's bytes closure, reached from draw.rs's readable for every texture view of every draw, and again for every candidate it compares against.

footprint already called texture_layout once at creation and threw the result away. The resource now keeps the swizzle from that point and both paths read the word.

Deferred's two dedup sets were hash sets. A list asks defer_refresh a few dozen times; SipHash over a 24-byte tuple plus a set probe is ~20 ns where a linear scan of three words is under 5. refreshed and textures are now lists, with first_use and both defer_refresh/defer_heap keeping their answers.

memory::note_use filled a diagnostics-only map in release builds. Stats::uses (a BTreeMap<String, u64>) is read by exactly one place: the once-a-frame line that names the busiest shaders. Filling it cost a global mutex, a String allocation and a BTreeMap descent per draw per memory-reading stage, in every build. It is now #[cfg(feature = "diagnostics")], and an AtomicU64 (USES) carries the count the release line prints, so the line reads the same.

memory::log_frame built its output before asking whether to write it. It drained the stats, sorted the map and formatted up to 32 Strings, then called tracing::info!. It now checks tracing::enabled!(Level::INFO) first.

Measured

Intel Arc B580, Windows 11, --release:

cargo test --release -p victory-gpu --lib --locked -- --exact --ignored --nocapture \
  resource::tests::a_swizzle_costs_no_layout_per_placed_texture

2048x2048, 12 mips: 290 ns to derive it, 0.00 ns to read the kept one
1920x1080 render target: 263 ns to derive it, 0.00 ns to read the kept one

Synthetic descriptions only. The per-frame total is that cost times the number of placed-texture reads and texture views a world frame makes, which this PR does not attempt to count — the counters for it belong to the follow-ups claimed for the same sweep.

Verification

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings — clean.
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings — clean.
  • cargo test -p victory-gpu --lib --locked --no-fail-fast — 229 passed, 2 failed. Both failures are this card's known baseline failures that pristine main also fails: descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views (#1744/#1829, the Intel view-heap cap).
  • rustfmt --edition 2024 --check per file, hunk counts against main: resource.rs 64/64, host.rs 85/85, memory.rs 30/30 — no new formatting hunks (the tree is deliberately not rustfmt-clean).

Synthetic tests only; no game data was used, and none enters the repo.

Part of the fifteen gaps claimed for this sweep on #1633. AI-assisted.

**@⁠borosf** on GitHub · 2026-11-26 14:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/3238) `borosf:vibe/frame-waste-cluster-842ca5` → `main` · +161 −27 · 3 files · 2 commits · open Four pieces of per-draw waste found by a sweep of the world frame, all of them work that is computed and then thrown away. No behaviour change: the same values, read instead of rebuilt. ## What each one cost **A placed texture's swizzle was derived by building the whole console layout.** `texture_layout` builds a `texture::Texture` — three heap allocations plus one `Mip` per level — to answer with a single `u32`. Two per-draw paths did it: - `memory_key`, which `current()` reaches for every placed texture a heap slot reads (`texture_at` → `current` → `memory_key`) and `shared_texture` reaches for every title texture of one memory; - `alias_source`'s `bytes` closure, reached from `draw.rs`'s `readable` for every texture view of every draw, and again for every candidate it compares against. `footprint` already called `texture_layout` once at creation and threw the result away. The resource now keeps the swizzle from that point and both paths read the word. **`Deferred`'s two dedup sets were hash sets.** A list asks `defer_refresh` a few dozen times; SipHash over a 24-byte tuple plus a set probe is ~20 ns where a linear scan of three words is under 5. `refreshed` and `textures` are now lists, with `first_use` and both `defer_refresh`/`defer_heap` keeping their answers. **`memory::note_use` filled a diagnostics-only map in release builds.** `Stats::uses` (a `BTreeMap<String, u64>`) is read by exactly one place: the once-a-frame line that names the busiest shaders. Filling it cost a global mutex, a `String` allocation and a `BTreeMap` descent per draw per memory-reading stage, in every build. It is now `#[cfg(feature = "diagnostics")]`, and an `AtomicU64` (`USES`) carries the count the release line prints, so the line reads the same. **`memory::log_frame` built its output before asking whether to write it.** It drained the stats, sorted the map and formatted up to 32 `String`s, then called `tracing::info!`. It now checks `tracing::enabled!(Level::INFO)` first. ## Measured Intel Arc B580, Windows 11, `--release`: ``` cargo test --release -p victory-gpu --lib --locked -- --exact --ignored --nocapture \ resource::tests::a_swizzle_costs_no_layout_per_placed_texture 2048x2048, 12 mips: 290 ns to derive it, 0.00 ns to read the kept one 1920x1080 render target: 263 ns to derive it, 0.00 ns to read the kept one ``` Synthetic descriptions only. The per-frame total is that cost times the number of placed-texture reads and texture views a world frame makes, which this PR does not attempt to count — the counters for it belong to the follow-ups claimed for the same sweep. ## Verification - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` — clean. - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings` — clean. - `cargo test -p victory-gpu --lib --locked --no-fail-fast` — 229 passed, 2 failed. Both failures are this card's known baseline failures that pristine `main` also fails: `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views` (#1744/#1829, the Intel view-heap cap). - `rustfmt --edition 2024 --check` per file, hunk counts against `main`: `resource.rs` 64/64, `host.rs` 85/85, `memory.rs` 30/30 — no new formatting hunks (the tree is deliberately not `rustfmt`-clean). Synthetic tests only; no game data was used, and none enters the repo. Part of the fifteen gaps claimed for this sweep on #1633. AI-assisted.
A placed-texture read asked a texture for its swizzle by building the
whole console layout: three allocations and the full mip chain, under a
lock, to answer with one `u32`. Two paths did it per draw — `memory_key`,
which `current()` and `texture_at` reach for every placed texture a heap
slot reads, and `alias_source`'s row geometry, which every texture view of
every draw reaches. The resource now works the swizzle out once at
creation, where `footprint` already called the same function, and both
paths read the word.

`Deferred`'s two dedup sets are lists. A list asks a few dozen times, and
a linear scan over three words beats SipHash over a tuple plus a set
probe at that length; `first_use` and both `defer_refresh`/`defer_heap`
keep their answers.

`memory::note_use` becomes an atomic count, and the per-pipeline map that
names the busiest shaders is diagnostics only — nothing but the frame line
reads it, and filling it cost a global lock, a `String` allocation and a
`BTreeMap` descent per draw per memory-reading stage. The release line
still prints the same count, from `USES`.

`memory::log_frame` asks whether the info level is enabled before it sorts
the map and formats up to 32 `String`s, so a run logging above it no
longer builds them for nothing.

Measured on an Intel Arc B580, `cargo test --release -p victory-gpu --lib
-- --exact --ignored --nocapture
resource::tests::a_swizzle_costs_no_layout_per_placed_texture`:
deriving a 2048x2048 12-mip texture's swizzle costs 290 ns, a 1920x1080
render target's 263 ns; reading the kept word costs nothing measurable.
Synthetic descriptions only, no game data.

Synthetic tests only; no game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:17:52 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 14:34 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Caching layout_swizzle on Resource at creation avoids rebuilding the full console mip layout per memory_key / lias_source lookup; gating Stats::uses behind diagnostics with an atomic USES counter and racing::enabled!(INFO) guard removes per-draw global lock/allocation overhead in non-diagnostics builds; small Vec lookups for Deferred::{refreshed, textures} are clean.
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 18 passed, 0 failed, 1 ignored (_swizzle_costs_no_layout_per_placed_texture).
  • cargo test -p victory-gpu --lib memory::tests:: -- --test-threads=1 --skip lists_share_a_table_and_copies_keep_their_views (RTX 5090): 7 passed, 0 failed (pre-existing #1744 test skipped).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 14:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/3238#pullrequestreview-6075910299) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090): - Caching layout_swizzle on Resource at creation avoids rebuilding the full console mip layout per memory_key / lias_source lookup; gating Stats::uses behind diagnostics with an atomic USES counter and racing::enabled!(INFO) guard removes per-draw global lock/allocation overhead in non-diagnostics builds; small Vec lookups for Deferred::{refreshed, textures} are clean. - cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 18 passed, 0 failed, 1 ignored (_swizzle_costs_no_layout_per_placed_texture). - cargo test -p victory-gpu --lib memory::tests:: -- --test-threads=1 --skip lists_share_a_table_and_copies_keep_their_views (RTX 5090): 7 passed, 0 failed (pre-existing #1744 test skipped). - cargo clippy -p victory-gpu --all-targets -- -D warnings: clean. - cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings: clean. - cargo xtask policy: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠KainanP on GitHub · 2026-11-26 14:50 UTC · original

Measured on main 63bcca4 with this PR merged (RTX 4060 Laptop, release, median of 7 interleaved runs; method in #2586):

  • alias_source: 120 → 14 ns, and an indexed draw reading six textures: 2713 → 1966 ns. Good.
  • But Deferred::refreshed as a Vec with contains grows with the list. In a sequence where each draw binds a new constant-ring position (root CBV, 4 root constants, index buffer, draw; lists reset every 2,000 calls), the sequence costs 2388 ns per draw, against 1966 for the bare draw. On main and on every other build measured, the setters add ~70 ns. A world frame's lists refresh thousands of distinct ranges, so the scan is O(n) per refresh and O(n²) per list. A set with a cheap hash (#2102's FxHash) keeps the gain without that cost. #1978 has the same alias_source change alone.

AI use: Claude Code (Claude Opus 5.5) ran the measurements and drafted this comment.

**@⁠KainanP** on GitHub · 2026-11-26 14:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3238#issuecomment-7195398489) Measured on main 63bcca4 with this PR merged (RTX 4060 Laptop, release, median of 7 interleaved runs; method in https://git.gtahub.st/github/Marvisak--VIctory/issues/2586): - `alias_source`: 120 → 14 ns, and an indexed draw reading six textures: 2713 → 1966 ns. Good. - But `Deferred::refreshed` as a `Vec` with `contains` grows with the list. In a sequence where each draw binds a new constant-ring position (root CBV, 4 root constants, index buffer, draw; lists reset every 2,000 calls), the sequence costs **2388 ns per draw, against 1966 for the bare draw**. On main and on every other build measured, the setters add ~70 ns. A world frame's lists refresh thousands of distinct ranges, so the scan is O(n) per refresh and O(n²) per list. A set with a cheap hash (#2102's FxHash) keeps the gain without that cost. #1978 has the same `alias_source` change alone. _AI use: Claude Code (Claude Opus 5.5) ran the measurements and drafted this comment._
Reviewer measurement on #3238 (RTX 4060 Laptop): the Vec scan kept the
alias_source win but a draw sequence that binds a new constant-ring
position each draw cost 2388 ns against 1966 ns bare, because the scan is
O(n) per refresh and O(n^2) per list, while a world frame's lists refresh
thousands of distinct ranges. SipHash over the tuple was the original cost.

`Deferred::refreshed` and `Deferred::textures` are now `FastSet` — a
`HashSet` with an in-tree FxHash (the same multiply-rotate step
victory-shader's structure.rs uses; no new dependency), keeping insert at
O(1) regardless of list length.

Synthetic tests only; no game data.
Author
Owner

@⁠borosf on GitHub · 2026-11-26 14:59 UTC · original

Fixed at b5e79ab9b7999caef232e4025e17397f0919080a: Deferred::refreshed and Deferred::textures are now FastSet — a HashSet with an in-tree FxHash (the same multiply-rotate step as victory-shader's structure.rs; no new dependency) — so insert is O(1) at any list length instead of the O(n) Vec scan your sequence measured (2388 ns vs 1966 ns bare draw). The alias_source change is untouched and keeps your 120 → 14 ns. Clippy clean on both feature sets, 229 passed / 2 failed (the known #1744/#1829 baseline), rustfmt hunks host.rs 85/85. Synthetic tests only; no game data.

**@⁠borosf** on GitHub · 2026-11-26 14:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/3238#issuecomment-7195615146) Fixed at `b5e79ab9b7999caef232e4025e17397f0919080a`: `Deferred::refreshed` and `Deferred::textures` are now `FastSet` — a `HashSet` with an in-tree FxHash (the same multiply-rotate step as victory-shader's `structure.rs`; no new dependency) — so insert is O(1) at any list length instead of the O(n) Vec scan your sequence measured (2388 ns vs 1966 ns bare draw). The alias_source change is untouched and keeps your 120 → 14 ns. Clippy clean on both feature sets, 229 passed / 2 failed (the known #1744/#1829 baseline), rustfmt hunks host.rs 85/85. Synthetic tests only; no game data.
This pull request can be merged automatically.
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/3238/head:github-bot/gh-3238'
git switch 'github-bot/gh-3238'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3238'
git switch 'github-bot/gh-3238'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3238'
git switch 'github-bot/gh-3238'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3238'
git switch 'main'
git merge --squash 'github-bot/gh-3238'
git switch 'main'
git merge --ff-only 'github-bot/gh-3238'
git switch 'main'
git merge 'github-bot/gh-3238'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3238
No description provided.