Shaders: speed up DXBC container MD5 hashing by 16-43% #2806

Open
brainbudt wants to merge 1 commit from upstream-pr/2808 into main
brainbudt commented 2026-11-25 21:06:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by brainbudt on 2026-11-25T21:06:38Z.


Every emitted DXBC container is MD5-hashed, and the compressor previously selected word schedules and rotation amounts inside its 64-step loop while allocating a temporary buffer to finalize each digest.

What changed

  • Specialize the four MD5 rounds and construct the DXBC-specific padding in a 64-byte stack buffer in crates/victory-shader/src/emit/container.rs.
  • Add exact wire-byte, digest-boundary, and malformed-field tests, plus an ignored release benchmark for container emission and parsing.
  • Pure safe Rust, no new dependencies; clean merge compatibility checked against #1065 and #1303.

Benchmark (8,652-byte synthetic 5-part container, median of 5 alternating release runs)

Host Operation Before After Gain
macOS ARM64 Build + DXBC hash 18.31 µs 10.53 µs -42.5%
Linux x86-64 Build + DXBC hash 16.84 µs 14.15 µs -16.0%

Verification

  • Byte-for-byte identical output across 4,096 payload lengths (9,443,207 bytes compared) and 1,513 parser input pairs on both macOS ARM64 and Linux x86-64.
  • Windows MSVC cargo check -p victory-shader --target x86_64-pc-windows-msvc --tests and all-target Clippy -D warnings passed (default and all features).

Reviewed with mistral-loop: 2 rounds with Mistral Large 4 and DeepSeek V4.1 Flash on high. Verdict: approved.

Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).

<!-- greyfinch-sync:Marvisak/VIctory:issue:5499180908 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2808). Originally posted by [brainbudt](https://github.com/brainbudt) on 2026-11-25T21:06:38Z. --- Every emitted DXBC container is MD5-hashed, and the compressor previously selected word schedules and rotation amounts inside its 64-step loop while allocating a temporary buffer to finalize each digest. ### What changed - Specialize the four MD5 rounds and construct the DXBC-specific padding in a 64-byte stack buffer in `crates/victory-shader/src/emit/container.rs`. - Add exact wire-byte, digest-boundary, and malformed-field tests, plus an ignored release benchmark for container emission and parsing. - Pure safe Rust, no new dependencies; clean merge compatibility checked against #1065 and #1303. ### Benchmark (8,652-byte synthetic 5-part container, median of 5 alternating release runs) | Host | Operation | Before | After | Gain | |---|---|---:|---:|---:| | macOS ARM64 | Build + DXBC hash | 18.31 µs | 10.53 µs | -42.5% | | Linux x86-64 | Build + DXBC hash | 16.84 µs | 14.15 µs | -16.0% | ### Verification - Byte-for-byte identical output across 4,096 payload lengths (9,443,207 bytes compared) and 1,513 parser input pairs on both macOS ARM64 and Linux x86-64. - Windows MSVC `cargo check -p victory-shader --target x86_64-pc-windows-msvc --tests` and all-target Clippy `-D warnings` passed (default and all features). Reviewed with mistral-loop: 2 rounds with Mistral Large 4 and DeepSeek V4.1 Flash on high. Verdict: approved. Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).
kotekompotek commented 2026-11-25 21:17:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T21:17:22Z.

Review: COMMENTED


Checked the nonstandard padding carefully: the D3D12 container hash puts the bit count FIRST (bytes 0-3), then data tail + 0x80, and 2*len+1 at bytes 60-63. The rewrite reproduces both cases exactly — rest < 56 single block [bitcount][rest][0x80][zeros][len<<1|1] and rest ≥ 56 with the 0x80-terminated first block then [bitcount][zeros][len<<1|1]. Round specialization uses the canonical schedule (7/12/17/22, 5/9/14/20, 4/11/16/23, 6/10/15/21). The malformed-fields test is a useful parse-coverage bonus. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065108723 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2808#pullrequestreview-6065108723). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T21:17:22Z. **Review:** COMMENTED --- Checked the nonstandard padding carefully: the D3D12 container hash puts the bit count FIRST (bytes 0-3), then data tail + 0x80, and `2*len+1` at bytes 60-63. The rewrite reproduces both cases exactly — rest < 56 single block `[bitcount][rest][0x80][zeros][len<<1|1]` and rest ≥ 56 with the 0x80-terminated first block then `[bitcount][zeros][len<<1|1]`. Round specialization uses the canonical schedule (7/12/17/22, 5/9/14/20, 4/11/16/23, 6/10/15/21). The malformed-fields test is a useful parse-coverage bonus. LGTM.
fuzeofficlal commented 2026-11-25 22:04:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T21:23:31Z.


Reviewed and benchmarked 7d7efe35 on Windows 11 against main (13f91ca).

Code review & byte-exactness

  • Pure safe Rust (as_chunks::<64>() / as_chunks::<4>()), unrolling all 4 MD5 rounds and removing the temporary tail Vec allocation while preserving DXBC's non-standard padding (len << 3 and len << 1 | 1) across both rest.len() < 56 and rest.len() >= 56 boundaries.
  • Independently cross-checked every length 0..=256 (all 64-byte remainder classes plus block boundaries 55, 56, 63, 64, 119, 120, 128) as well as 511, 512, 513, 4095, 4096 against 13f91ca: 100% byte-identical digests.

Verification & same-machine --release benchmark (rustc 1.98.1 MSVC, Windows SDK 10.0.26100.0)

  • RTX 5090 (emit::container dxbc::): 6/6 passed; WARP + dxil.dll full victory-shader --features host: 270 passed, 0 failed (skipping only the 3 hardware-only synthetic_tests).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings; cargo xtask policy: 184 files ok; clean git merge-tree vs 13f91ca.
  • container_throughput (--release, 5-run median, 8,652 B 5-part container):
    • build + hash: 10,264.1 ns/op (13f91ca) -> 9,385.8 ns/op (-8.6% / 1.09× on MSVC, with build() container allocation dominating the remainder).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7175013852 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2808#issuecomment-7175013852). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T21:23:31Z. --- Reviewed and benchmarked `7d7efe35` on Windows 11 against `main` (`13f91ca`). ### Code review & byte-exactness - Pure safe Rust (`as_chunks::<64>()` / `as_chunks::<4>()`), unrolling all 4 MD5 rounds and removing the temporary `tail` `Vec` allocation while preserving DXBC's non-standard padding (`len << 3` and `len << 1 | 1`) across both `rest.len() < 56` and `rest.len() >= 56` boundaries. - Independently cross-checked every length `0..=256` (all 64-byte remainder classes plus block boundaries `55, 56, 63, 64, 119, 120, 128`) as well as `511, 512, 513, 4095, 4096` against `13f91ca`: **100% byte-identical digests**. ### Verification & same-machine `--release` benchmark (`rustc 1.98.1` MSVC, Windows SDK `10.0.26100.0`) - RTX 5090 (`emit::container dxbc::`): **6/6 passed**; WARP + `dxil.dll` full `victory-shader --features host`: **270 passed, 0 failed** (skipping only the 3 hardware-only `synthetic_tests`). - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default & `--features host`): **0 warnings**; `cargo xtask policy`: **184 files ok**; clean `git merge-tree` vs `13f91ca`. - `container_throughput` (`--release`, 5-run median, 8,652 B 5-part container): - `build + hash`: `10,264.1 ns/op` (`13f91ca`) -> **`9,385.8 ns/op`** (**-8.6% / 1.09×** on MSVC, with `build()` container allocation dominating the remainder).
cloph-dsp commented 2026-11-25 22:04:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-25T21:25:26Z.


Local verification on Windows / rustc 1.100.0 / MSVC, no game data, no GPU, no clippy/rustup, CARGO_BUILD_JOBS=2.

  • cargo test --locked -p victory-shader --lib container --quiet -> 3 passed, 1 ignored benchmark, 0 failed (273 filtered out).
  • Targeted: cargo test --locked -p victory-shader --lib reports_malformed_fields --quiet -> 1 passed.
  • git diff --check origin/main..HEAD clean.
  • Negative control: odd_parts_match_wire_bytes, dxbc_hash_padding_boundaries and reports_malformed_fields are all absent from origin/main, so the wire-byte, digest-boundary and malformed-field coverage is new here.
  • The rewritten MD5 block scheduler keeps the same D3D12 container digest; the synthetic tests cover exact wire bytes, padding at 56/64-byte boundaries and seven malformed field cases.
  • Full victory-shader lib was not run: emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index crashes on unmodified base 13f91ca on this machine (pre-existing).

AI-assisted (Hy4 flash).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175056606 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2808#issuecomment-7175056606). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-25T21:25:26Z. --- Local verification on Windows / rustc 1.100.0 / MSVC, no game data, no GPU, no clippy/rustup, CARGO_BUILD_JOBS=2. - `cargo test --locked -p victory-shader --lib container --quiet` -> 3 passed, 1 ignored benchmark, 0 failed (273 filtered out). - Targeted: `cargo test --locked -p victory-shader --lib reports_malformed_fields --quiet` -> 1 passed. - `git diff --check origin/main..HEAD` clean. - Negative control: `odd_parts_match_wire_bytes`, `dxbc_hash_padding_boundaries` and `reports_malformed_fields` are all absent from origin/main, so the wire-byte, digest-boundary and malformed-field coverage is new here. - The rewritten MD5 block scheduler keeps the same D3D12 container digest; the synthetic tests cover exact wire bytes, padding at 56/64-byte boundaries and seven malformed field cases. - Full victory-shader lib was not run: `emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index` crashes on unmodified base 13f91ca on this machine (pre-existing). AI-assisted (Hy4 flash).
Dukestar69 commented 2026-11-26 02:50:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T02:50:13Z.

Review: APPROVED


Tested merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-shader --lib emit::container::tests: 3 passed, 0 failed, 1 ignored (odd_parts_match_wire_bytes, md5_core_matches_the_standard, dxbc_hash_padding_boundaries).
  • cargo test -p victory-shader --lib dxbc::tests: 3 passed, 0 failed (including reports_malformed_fields).
  • cargo clippy -p victory-shader --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:6067899049 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2808#pullrequestreview-6067899049). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T02:50:13Z. **Review:** APPROVED --- Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64: - `cargo test -p victory-shader --lib emit::container::tests`: **3 passed, 0 failed, 1 ignored** (`odd_parts_match_wire_bytes`, `md5_core_matches_the_standard`, `dxbc_hash_padding_boundaries`). - `cargo test -p victory-shader --lib dxbc::tests`: **3 passed, 0 failed** (including `reports_malformed_fields`). - `cargo clippy -p victory-shader --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 03:05:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T02:18:55Z.


Automated triage for the maintainers.

This speeds up the DXBC container hash in emit/container.rs: hash builds its padded tail in a stack array instead of a Vec, and md5_block is unrolled by round. I checked the unrolled step order, the per-round message schedules ((5i+1), (3i+5), 7i mod 16 give the same words as the global index), and both tail cases (rest < 56 and >= 56) against the old code. They match, and dxbc_hash_padding_boundaries pins the old digests at the block boundaries. The new reports_malformed_fields test covers existing parse errors. The benchmark is #[ignore]d.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7180681699 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2808#issuecomment-7180681699). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T02:18:55Z. --- <!-- victory-triage v1 head=7d7efe35fffd379721ab910f833dffd87e7706c5 verdict=merge-candidate at=2026-11-26T02:18:50Z --> Automated triage for the maintainers. This speeds up the DXBC container hash in `emit/container.rs`: `hash` builds its padded tail in a stack array instead of a `Vec`, and `md5_block` is unrolled by round. I checked the unrolled step order, the per-round message schedules (`(5i+1)`, `(3i+5)`, `7i` mod 16 give the same words as the global index), and both tail cases (`rest < 56` and `>= 56`) against the old code. They match, and `dxbc_hash_padding_boundaries` pins the old digests at the block boundaries. The new `reports_malformed_fields` test covers existing parse errors. The benchmark is `#[ignore]`d. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-27 02:16:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T02:16:36Z.

Review: APPROVED


Reviewed 7d7efe35fffd379721ab910f833dffd87e7706c5.

LGTM — switching container::hash to as_chunks::<64>() + a stack [0; 64] tail buffer and specializing the four rounds in md5_block preserves byte-for-byte equivalence with the previous padding/compression implementation across all boundary remainders (< 56 vs >= 56), and dxbc::tests::reports_malformed_fields adds solid parser error coverage.

Verified locally:

  • cargo xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets / --all-features (-D warnings), and zero new rustfmt diffs (dxbc.rs passes cleanly; container.rs only has the pre-existing const K table layout).
  • dxbc::tests (3 passed) and emit::container::tests (3 passed) pass, and --release container_throughput passes on an 8,652-byte container (parse + find DXIL 30.48 ns/op, build + hash 9259.74 ns/op).
<!-- greyfinch-sync:Marvisak/VIctory:review:6082622510 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2808#pullrequestreview-6082622510). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-27T02:16:36Z. **Review:** APPROVED --- Reviewed `7d7efe35fffd379721ab910f833dffd87e7706c5`. **LGTM** — switching `container::hash` to `as_chunks::<64>()` + a stack `[0; 64]` tail buffer and specializing the four rounds in `md5_block` preserves byte-for-byte equivalence with the previous padding/compression implementation across all boundary remainders (`< 56` vs `>= 56`), and `dxbc::tests::reports_malformed_fields` adds solid parser error coverage. Verified locally: - `cargo xtask policy` (`191 files ok`), `cargo clippy -p victory-shader --all-targets` / `--all-features` (`-D warnings`), and zero new `rustfmt` diffs (`dxbc.rs` passes cleanly; `container.rs` only has the pre-existing `const K` table layout). - `dxbc::tests` (3 passed) and `emit::container::tests` (3 passed) pass, and `--release` `container_throughput` passes on an 8,652-byte container (`parse + find DXIL 30.48 ns/op`, `build + hash 9259.74 ns/op`).
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/2808:upstream-pr/2808'
git switch 'upstream-pr/2808'

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/2808'
git switch 'upstream-pr/2808'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2808'
git switch 'upstream-pr/2808'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2808'
git switch 'main'
git merge --squash 'upstream-pr/2808'
git switch 'main'
git merge --ff-only 'upstream-pr/2808'
git switch 'main'
git merge 'upstream-pr/2808'
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!2806
No description provided.