GPU: cut pipeline creation allocations and CPU overhead #3396

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

@⁠Desamodeth on GitHub · 2026-11-26 22:05 UTC · original
Desamodeth:opt/gpu-pipeline-create-stream → main · +682 −95 · 4 files · 1 commits · open

Summary

During boot, a title can create ~60,000 pipeline states (create_stream) while drawing with only a small subset. Previously each graphics create_stream call allocated ~43 heap buffers (~10 KiB of transient churn and ~1.3 KiB retained):

  1. Zero-allocation streaming GraphicsState::hash_describe (pipeline/host.rs, pipeline/make.rs): Replaces ormat!("{:?} ...") + Vec + per-input String allocations inside make::key with a stack-buffered HashSink ([u8; 512]) feeding lake3::Hasher directly. Output is byte-for-byte identical to the previous Debug format, so early-pipelines.txt keys and Prebuild::describe() do not change.
  2. Bounded semantic-name interning (pipeline/host.rs): Matches common HLSL semantics (POSITION, NORMAL, TANGENT, BINORMAL, TEXCOORD, COLOR, etc.) lock-free without allocation and interns up to 256 custom semantic names process-wide, falling back to GraphicsState::names once capped (preventing unbounded growth on adversarial streams).
  3. Compact 16-byte pipeline keys & lock-free wanted_early (pipeline/make.rs, pipeline/library.rs): Stores the 16 raw BLAKE3 key bytes ([u8; 16]) instead of heap-allocated 32-char Strings in Pending::key, CREATED, and Used. Splits immutable earlier-run keys into BEFORE: OnceLock<HashMap<[u8; 16], usize>> so wanted_early runs lock-free on every pipeline creation.
  4. Deduplicate Vec<Arc> on Pipeline (pipeline.rs): On Windows, Pipeline::shaders() borrows from self.host.shaders() instead of cloning Vec<Arc> per pipeline (offset_of!(Pipeline, packet) == 0x10 unchanged; portable non-Windows layout preserved).

(Orthogonal to #2211, #2974, #2986, #3005, and #3188.)

Verification (CPU-only, Windows 11 x64, Ryzen 7 9800X3D,

ustc 1.98.1 MSVC, base 63bcca43)

  • Tests:
    • cargo test --locked -p victory-gpu --lib -- --test-threads=1 (all 22 CPU-only pipeline tests, incl. new write_describe_matches_debug_format, semantic_interning_reuses_common_and_caps_custom_names, key_hex_roundtrip_and_rejection): 22 passed, 0 failed
    • cargo test --locked -p victory-gpu --features diagnostics --lib (8 CPU-only pipeline/make tests): 8 passed, 0 failed
    • cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default & --features diagnostics): clean
    • cargo check --locked --workspace & cargo xtask policy: clean (191 files ok)
    • No GPU/WARP tests or game files were used.
  • Bit-exact key & describe() parity: Checked across all 6,000 typical, 4,000 minimal, 1,000 adversarial, and escaped-CStr + depth_bounds streams — 100% identical FNV-1a key hashes and describe() bytes (3,826 B, xca87b3c5d8f76ed8, key 5b359d782f08ba1a5965b9a0edb4a18c).

Benchmarks (--release, synthetic create_stream / create_compute, counting GlobalAlloc)

Deterministic allocations & heap memory (exact before/after)

Workload Metric main (63bcca43) This PR Delta
W1 Typical 6,000 gfx PSOs (6 std inputs) Heap allocations 258,007 (43.0/PSO) 30,007 (5.0/PSO) -88.4%
Total bytes allocated 59,974,512 B 6,372,464 B -89.4%
Live heap (6,000 PSOs live) 7,835,200 B 6,234,176 B -20.4%
Peak heap during batch 7,839,280 B 6,234,176 B -20.5%
Retained after releasing all 6,000 347,200 B 90,176 B -74.0%
W2 Minimal 4,000 gfx PSOs (0 inputs) Heap allocs / Live heap 64,001 / 3,948,800 B 16,001 / 3,435,264 B -75.0% / -13.0%
W3 Adversarial 1,000 PSOs (4,000 unique custom names) Heap allocs / Live / Post-release 35,000 / 1,196,000 B / 24,000 B 9,944 / 1,088,624 B / 5,072 B -71.6% / -9.0% / -78.9%
W4 Compute 2,000 PSOs (create_compute) Heap allocs / Live heap 12,000 / 673,024 B 6,000 / 528,000 B -50.0% / -21.5%

Interleaved Release timings (10 runs each: 5× main → pr + 5× pr → main, medians)

| Workload | main ( /
) | This PR ( /

) Delta
W1 Typical 6k gfx (ns/PSO) 11,735.5 (11,714.4 / 11,826.1) 7,238.0 (7,208.8 / 7,267.2) -38.3% (1.62×)
W2 Minimal 4k gfx, 0 inputs (ns/PSO) 9,307.4 (9,235.5 / 9,329.8) 6,493.7 (6,491.2 / 6,506.5) -30.2% (1.43×)
W3 Adversarial 1k, 4k unique semantics (ns/PSO) 11,513.8 (10,924.6 / 11,700.1) 7,069.1 (7,070.0 / 7,068.2) -38.6% (1.63×)
W4 Compute 2k (ns/PSO) 785.7 (799.4 / 765.7) 571.3 (570.7 / 571.9) -27.3% (1.38×)
W5 Multi-thread 4×1k gfx (µs total) 14,821.4 (14,552.5 / 15,321.8) 8,689.3 (8,979.2 / 7,801.5) -41.4% (1.71×)

Trade-offs

  • GraphicsState::write_describe explicitly mirrors the windows crate's Debug text of D3D12_{BLEND,RASTERIZER,DEPTH_STENCIL,RT_FORMAT_ARRAY} and DXGI_SAMPLE_DESC to keep existing early-pipelines.txt keys bit-exact; write_describe_matches_debug_format pins this against ormat!("{:?}") so any future windows crate format change is caught immediately in unit tests.
  • Up to 256 non-standard custom semantic names are retained process-wide in the interner (~4–8 KiB upper bound); names beyond that cap fall back to per-state CString storage and free with the pipeline.
  • Because library::tag hashes host.rs and library.rs, prebuilt-pipelines.txt refreshes on first prebuild after this change (standard for any edit to those files), while early-pipelines.txt keys remain 100% compatible.

Claim: #1633

AI-assisted with Gemini 4 Neon

**@⁠Desamodeth** on GitHub · 2026-11-26 22:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/3396) `Desamodeth:opt/gpu-pipeline-create-stream` → `main` · +682 −95 · 4 files · 1 commits · open ### Summary During boot, a title can create ~60,000 pipeline states (create_stream) while drawing with only a small subset. Previously each graphics create_stream call allocated ~43 heap buffers (~10 KiB of transient churn and ~1.3 KiB retained): 1. **Zero-allocation streaming GraphicsState::hash_describe (pipeline/host.rs, pipeline/make.rs):** Replaces ormat!("{:?} ...") + Vec<String> + per-input String allocations inside make::key with a stack-buffered HashSink ([u8; 512]) feeding lake3::Hasher directly. Output is **byte-for-byte identical** to the previous Debug format, so early-pipelines.txt keys and Prebuild::describe() do not change. 2. **Bounded semantic-name interning (pipeline/host.rs):** Matches common HLSL semantics (POSITION, NORMAL, TANGENT, BINORMAL, TEXCOORD, COLOR, etc.) lock-free without allocation and interns up to 256 custom semantic names process-wide, falling back to GraphicsState::names once capped (preventing unbounded growth on adversarial streams). 3. **Compact 16-byte pipeline keys & lock-free wanted_early (pipeline/make.rs, pipeline/library.rs):** Stores the 16 raw BLAKE3 key bytes ([u8; 16]) instead of heap-allocated 32-char Strings in Pending::key, CREATED, and Used. Splits immutable earlier-run keys into BEFORE: OnceLock<HashMap<[u8; 16], usize>> so wanted_early runs lock-free on every pipeline creation. 4. **Deduplicate Vec<Arc<Shader>> on Pipeline (pipeline.rs):** On Windows, Pipeline::shaders() borrows from self.host.shaders() instead of cloning Vec<Arc<Shader>> per pipeline (offset_of!(Pipeline, packet) == 0x10 unchanged; portable non-Windows layout preserved). *(Orthogonal to #2211, #2974, #2986, #3005, and #3188.)* ### Verification (CPU-only, Windows 11 x64, Ryzen 7 9800X3D, ustc 1.98.1 MSVC, base 63bcca43) - **Tests:** - cargo test --locked -p victory-gpu --lib -- --test-threads=1 (all 22 CPU-only pipeline tests, incl. new write_describe_matches_debug_format, semantic_interning_reuses_common_and_caps_custom_names, key_hex_roundtrip_and_rejection): **22 passed, 0 failed** - cargo test --locked -p victory-gpu --features diagnostics --lib (8 CPU-only pipeline/make tests): **8 passed, 0 failed** - cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default & --features diagnostics): clean - cargo check --locked --workspace & cargo xtask policy: clean (191 files ok) - No GPU/WARP tests or game files were used. - **Bit-exact key & describe() parity:** Checked across all 6,000 typical, 4,000 minimal, 1,000 adversarial, and escaped-CStr + depth_bounds streams — **100% identical FNV-1a key hashes and describe() bytes** (3,826 B, xca87b3c5d8f76ed8, key 5b359d782f08ba1a5965b9a0edb4a18c). ### Benchmarks (--release, synthetic create_stream / create_compute, counting GlobalAlloc) #### Deterministic allocations & heap memory (exact before/after) | Workload | Metric | main (63bcca43) | This PR | Delta | |---|---|---:|---:|---:| | **W1** Typical 6,000 gfx PSOs (6 std inputs) | Heap allocations | 258,007 (43.0/PSO) | 30,007 (5.0/PSO) | **-88.4%** | | | Total bytes allocated | 59,974,512 B | 6,372,464 B | **-89.4%** | | | Live heap (6,000 PSOs live) | 7,835,200 B | 6,234,176 B | **-20.4%** | | | Peak heap during batch | 7,839,280 B | 6,234,176 B | **-20.5%** | | | Retained after releasing all 6,000 | 347,200 B | 90,176 B | **-74.0%** | | **W2** Minimal 4,000 gfx PSOs (0 inputs) | Heap allocs / Live heap | 64,001 / 3,948,800 B | 16,001 / 3,435,264 B | **-75.0%** / **-13.0%** | | **W3** Adversarial 1,000 PSOs (4,000 unique custom names) | Heap allocs / Live / Post-release | 35,000 / 1,196,000 B / 24,000 B | 9,944 / 1,088,624 B / 5,072 B | **-71.6%** / **-9.0%** / **-78.9%** | | **W4** Compute 2,000 PSOs (create_compute) | Heap allocs / Live heap | 12,000 / 673,024 B | 6,000 / 528,000 B | **-50.0%** / **-21.5%** | #### Interleaved Release timings (10 runs each: 5× main → pr + 5× pr → main, medians) | Workload | main ( / ) | This PR ( / ) | Delta | |---|---:|---:|---:| | **W1** Typical 6k gfx (ns/PSO) | 11,735.5 (11,714.4 / 11,826.1) | **7,238.0** (7,208.8 / 7,267.2) | **-38.3% (1.62×)** | | **W2** Minimal 4k gfx, 0 inputs (ns/PSO) | 9,307.4 (9,235.5 / 9,329.8) | **6,493.7** (6,491.2 / 6,506.5) | **-30.2% (1.43×)** | | **W3** Adversarial 1k, 4k unique semantics (ns/PSO) | 11,513.8 (10,924.6 / 11,700.1) | **7,069.1** (7,070.0 / 7,068.2) | **-38.6% (1.63×)** | | **W4** Compute 2k (ns/PSO) | 785.7 (799.4 / 765.7) | **571.3** (570.7 / 571.9) | **-27.3% (1.38×)** | | **W5** Multi-thread 4×1k gfx (µs total) | 14,821.4 (14,552.5 / 15,321.8) | **8,689.3** (8,979.2 / 7,801.5) | **-41.4% (1.71×)** | ### Trade-offs - GraphicsState::write_describe explicitly mirrors the windows crate's Debug text of D3D12_{BLEND,RASTERIZER,DEPTH_STENCIL,RT_FORMAT_ARRAY} and DXGI_SAMPLE_DESC to keep existing early-pipelines.txt keys bit-exact; write_describe_matches_debug_format pins this against ormat!("{:?}") so any future windows crate format change is caught immediately in unit tests. - Up to 256 non-standard custom semantic names are retained process-wide in the interner (~4–8 KiB upper bound); names beyond that cap fall back to per-state CString storage and free with the pipeline. - Because library::tag hashes host.rs and library.rs, prebuilt-pipelines.txt refreshes on first prebuild after this change (standard for any edit to those files), while early-pipelines.txt keys remain 100% compatible. Claim: https://git.gtahub.st/github/Marvisak--VIctory/issues/1633 AI-assisted with Gemini 4 Neon
Stream GraphicsState::write_describe directly into BLAKE3 without intermediate Debug/Vec<String> heap allocations (preserving bit-exact pipeline keys), intern common/custom input-layout semantic names with a bounded cap, store compact 16-byte pipeline keys in Pending/CREATED/Used with lock-free wanted_early lookup, and avoid duplicating Vec<Arc<Shader>> on Pipeline on Windows.

AI-assisted with Gemini 4 Neon
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:06:33 +00:00
Author
Owner

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

LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Interning common semantic names, avoiding intermediate formatting allocations in pipeline description/key generation, and trimming pipeline creation overhead pass cleanly with strong unit coverage.
  • cargo test -p victory-gpu --lib pipeline:: -- --test-threads=1: 26 passed, 0 failed (including semantic_interning_reuses_common_and_caps_custom_names, write_describe_matches_debug_format, and key_hex_roundtrip_and_rejection).
  • 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).

**@⁠Dukestar69** on GitHub · 2026-11-26 22:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/3396#pullrequestreview-6080991874) · **approved these changes** LGTM — verified on RTX 5090 (Windows / D3D12) against `origin/main` (`63bcca4`). - Interning common semantic names, avoiding intermediate formatting allocations in pipeline description/key generation, and trimming pipeline creation overhead pass cleanly with strong unit coverage. - `cargo test -p victory-gpu --lib pipeline:: -- --test-threads=1`: 26 passed, 0 failed (including `semantic_interning_reuses_common_and_caps_custom_names`, `write_describe_matches_debug_format`, and `key_hex_roundtrip_and_rejection`). - `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).
Author
Owner

@⁠passerbyyhx on GitHub · 2026-11-26 22:32 UTC · original

I tested early-cache compatibility in both directions between parent 63bcca43c6a00a758510398fa43096ce7e4f7df0 and head 92f218c54d56f34bec955621f2e46e2238977605 on Windows D3D12 with an RTX 4090 Laptop GPU (621.14). Each direction used a fresh directory and three separate processes. The fixture exercises the real title key, early queue, library load, and save paths with synthetic compute and graphics shaders. Two graphics PSOs shared shaders and differed only in sample mask.

The old and new builds produced identical keys. Both produced B,A on disk after two producer processes used A, then B. The opposite build started both before first use and recorded two real pipeline-library hits. The cold graphics PSO C was offered first and stayed unqueued until first use. It then made one new PSO, and the file saved as C,B,A in both directions.

Synthetic test fixture and runner

AI-assisted with Codex.

**@⁠passerbyyhx** on GitHub · 2026-11-26 22:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/3396#issuecomment-7204777618) I tested early-cache compatibility in both directions between parent `63bcca43c6a00a758510398fa43096ce7e4f7df0` and head `92f218c54d56f34bec955621f2e46e2238977605` on Windows D3D12 with an RTX 4090 Laptop GPU (621.14). Each direction used a fresh directory and three separate processes. The fixture exercises the real title key, early queue, library load, and save paths with synthetic compute and graphics shaders. Two graphics PSOs shared shaders and differed only in sample mask. The old and new builds produced identical keys. Both produced `B,A` on disk after two producer processes used A, then B. The opposite build started both before first use and recorded two real pipeline-library hits. The cold graphics PSO C was offered first and stayed unqueued until first use. It then made one new PSO, and the file saved as `C,B,A` in both directions. [Synthetic test fixture and runner](https://github.com/passerbyyhx/VIctory/commit/acf279ccca66604fac0177bc19b1de4fe11b722c) AI-assisted with Codex.
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 22:52 UTC · original

Windows 11, 63bcca4 + this PR, Radeon RX 7900 XTX:

  • cargo test -p victory-gpu --lib: 234 passed / 0 failed / 1 ignored (unpatched 63bcca4 here: 231 passed; +3).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.

The three added tests are pipeline::host::tests::semantic_interning_reuses_common_and_caps_custom_names, pipeline::host::tests::write_describe_matches_debug_format and pipeline::library::tests::key_hex_roundtrip_and_rejection. The pipeline-creation path they cover is exercised on the device by the rest of the suite, which is green. Synthetic only. AI-assisted.

**@⁠Jackster** on GitHub · 2026-11-26 22:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/3396#issuecomment-7205058999) Windows 11, `63bcca4` + this PR, Radeon RX 7900 XTX: - `cargo test -p victory-gpu --lib`: 234 passed / 0 failed / 1 ignored (unpatched `63bcca4` here: 231 passed; +3). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. The three added tests are `pipeline::host::tests::semantic_interning_reuses_common_and_caps_custom_names`, `pipeline::host::tests::write_describe_matches_debug_format` and `pipeline::library::tests::key_hex_roundtrip_and_rejection`. The pipeline-creation path they cover is exercised on the device by the rest of the suite, which is green. Synthetic only. AI-assisted.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 23:02 UTC · original · approved these changes

Reviewed 92f218c5 merged onto main (63bcca43) — clean allocation and CPU overhead reduction on pipeline creation.

Verification

  • cargo xtask policy: 191 files ok; cargo check --locked --workspace --all-targets: ok; cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (both default and --features diagnostics): 0 warnings.
  • pipeline:: unit tests passed on both a discrete NVIDIA RTX 5090 and WARP with --features diagnostics and VICTORY_D3D12_DEBUG=1.
  • Verified GraphicsState::read (pipeline/host.rs:363-387), intern_semantic (pipeline/host.rs:183-202), write_describe / hash_describe (pipeline/host.rs:414-548), key_bytes (pipeline/make.rs:115-129), and lock-free BEFORE: OnceLock<...> (pipeline/library.rs:501-536):
    • Tested all 12 edge-case f32 values (+0.0, -0.0, NaN, ±Inf, subnormals, MIN/MAX) and extreme i32/u32 bounds: byte-for-byte identical to Debug formatting and blake3(describe).
    • Ran an 8-thread concurrency test hammering GraphicsState::read (past MAX_INTERNED_SEMANTICS into the owned self.names fallback), key_bytes, note_created_key, wanted_early_key, and note_used_key: 0 panics and deterministic keys across all threads.
    • --release microbenchmark on 10,000 iterations of GraphicsState::read + key_bytes vs Debug formatting + blake3: 52.5 ms vs 70.1 ms (1.34× faster) with zero intermediate String/Vec<String> allocations.

Minor non-blocking note

  • Fuzzing all 255 non-NUL bytes (1..=255) in SemanticName showed 254/255 match Debug byte-for-byte (hash_describe matches blake3(describe) for all 255/255), with the single exception of ASCII single quote ' (0x27), which CStr's Debug escapes as '. Adding && b != b''' to the fast-path guard in push_cstr_quoted (crates/victory-gpu/src/pipeline/host.rs:245) would make parity 100% exact even for non-HLSL semantic strings.

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 23:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/3396#pullrequestreview-6081324438) · **approved these changes** Reviewed `92f218c5` merged onto `main` (`63bcca43`) — clean allocation and CPU overhead reduction on pipeline creation. ### Verification - `cargo xtask policy`: 191 files ok; `cargo check --locked --workspace --all-targets`: ok; `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (both default and `--features diagnostics`): 0 warnings. - `pipeline::` unit tests passed on both a discrete NVIDIA RTX 5090 and WARP with `--features diagnostics` and `VICTORY_D3D12_DEBUG=1`. - Verified `GraphicsState::read` (`pipeline/host.rs:363-387`), `intern_semantic` (`pipeline/host.rs:183-202`), `write_describe` / `hash_describe` (`pipeline/host.rs:414-548`), `key_bytes` (`pipeline/make.rs:115-129`), and lock-free `BEFORE: OnceLock<...>` (`pipeline/library.rs:501-536`): - Tested all 12 edge-case `f32` values (`+0.0`, `-0.0`, `NaN`, `±Inf`, subnormals, `MIN`/`MAX`) and extreme `i32`/`u32` bounds: byte-for-byte identical to `Debug` formatting and `blake3(describe)`. - Ran an 8-thread concurrency test hammering `GraphicsState::read` (past `MAX_INTERNED_SEMANTICS` into the owned `self.names` fallback), `key_bytes`, `note_created_key`, `wanted_early_key`, and `note_used_key`: 0 panics and deterministic keys across all threads. - `--release` microbenchmark on 10,000 iterations of `GraphicsState::read` + `key_bytes` vs `Debug` formatting + `blake3`: **52.5 ms vs 70.1 ms (`1.34×` faster)** with zero intermediate `String`/`Vec<String>` allocations. ### Minor non-blocking note - Fuzzing all 255 non-NUL bytes (`1..=255`) in `SemanticName` showed 254/255 match `Debug` byte-for-byte (`hash_describe` matches `blake3(describe)` for all 255/255), with the single exception of ASCII single quote `'` (`0x27`), which `CStr`'s `Debug` escapes as `'`. Adding `&& b != b'''` to the fast-path guard in `push_cstr_quoted` (`crates/victory-gpu/src/pipeline/host.rs:245`) would make parity 100% exact even for non-HLSL semantic strings. LGTM.
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/3396/head:github-bot/gh-3396'
git switch 'github-bot/gh-3396'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3396'
git switch 'github-bot/gh-3396'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3396'
git switch 'github-bot/gh-3396'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3396'
git switch 'main'
git merge --squash 'github-bot/gh-3396'
git switch 'main'
git merge --ff-only 'github-bot/gh-3396'
git switch 'main'
git merge 'github-bot/gh-3396'
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!3396
No description provided.