xma2: borrow the bit reservoir instead of cloning it per frame #884

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

@⁠mastrr9 on GitHub · 2026-11-24 07:24 UTC · original
mastrr9:perf/xma2-avoid-per-frame-reservoir-clone → main · +15 −2 · 1 files · 1 commits · open

Part of #459. Eliminates the per-frame 32 KB self.reservoir.buf.clone() heap allocation and copy in crates/xcl/src/hle/xma2/mod.rs (decode_frame):

  • decode_frame only reads the reservoir (its only writer, save_bits, is never called during decode_frame), so it now swaps the buffer out with std::mem::take into decode_frame_from and restores it immediately afterward.
  • Removes ~32 KB of allocation/memcpy per 512-sample XMA frame on the game thread (~17% faster XMA decode in off-tree profiling, 86 → 101 M samples/s) with bit-identical output against the existing FFmpeg-verified test vectors.

Tested

  • Hardware/OS: NVIDIA RTX 5080 (driver 617.42 / 32.0.16.1742), Windows 11 Pro 25H2 (26200.9974), rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC debug, DXC Windows SDK 10.0.26100.0, d3d12SDKLayers.dll installed
  • Commit tested: 6b825b83107a0f8cb729ea77630dbabdf1642f82
  • cargo build --workspace: clean (no new warnings; only pre-existing fetch_update deprecation warning)
  • cargo test --workspace -- --test-threads=4: 713 passed, 0 failed, 41 ignored (all 7 hle::xma2::tests::* tests including decodes_xma2_and_xma1_bitstreams_matching_ffmpeg passed)
  • SKIPPED GPU tests: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64

Regression run
Smoke run by @⁠Toons29 in comment below: #884
Contributed by a community tester; the PR author did not run it. Both blocks are pasted as posted.

main:

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB

PR:

xcl-smoke v1
commit: 958c517e218843d9701b96d3d27603a5f92dbe90
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1621 (PresentX 1621)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14811 MB

Tester's setup and limits: AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

Tester's AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; the tester checked the blocks before posting.

Not tested

  • No retail title was available to the author; whether the community regression run above reaches the code this PR changes is not known.

AI use

  • Authored with Claude Code (Claude Opus 5.5); reviewed, audited, and verified on native Windows/RTX 5080 hardware with Antigravity (Gemini 4 Neon + Claude Sonnet 5.5).
  • Claude Code (Claude Sonnet 5.5) added the community regression run to this description and updated the lines that it made out of date or that the PR check requires.
**@⁠mastrr9** on GitHub · 2026-11-24 07:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/884) `mastrr9:perf/xma2-avoid-per-frame-reservoir-clone` → `main` · +15 −2 · 1 files · 1 commits · open Part of #459. Eliminates the per-frame 32 KB `self.reservoir.buf.clone()` heap allocation and copy in `crates/xcl/src/hle/xma2/mod.rs` (`decode_frame`): - `decode_frame` only reads the reservoir (its only writer, `save_bits`, is never called during `decode_frame`), so it now swaps the buffer out with `std::mem::take` into `decode_frame_from` and restores it immediately afterward. - Removes ~32 KB of allocation/memcpy per 512-sample XMA frame on the game thread (~17% faster XMA decode in off-tree profiling, 86 → 101 M samples/s) with bit-identical output against the existing FFmpeg-verified test vectors. **Tested** - Hardware/OS: NVIDIA RTX 5080 (driver `617.42` / `32.0.16.1742`), Windows 11 Pro 25H2 (`26200.9974`), `rustc 1.100.0 (0ef1bc386 2026-11-09)` MSVC debug, DXC Windows SDK `10.0.26100.0`, `d3d12SDKLayers.dll` installed - Commit tested: `6b825b83107a0f8cb729ea77630dbabdf1642f82` - `cargo build --workspace`: clean (no new warnings; only pre-existing `fetch_update` deprecation warning) - `cargo test --workspace -- --test-threads=4`: **713 passed, 0 failed, 41 ignored** (all 7 `hle::xma2::tests::*` tests including `decodes_xma2_and_xma1_bitstreams_matching_ffmpeg` passed) - `SKIPPED` GPU tests: `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64`, `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64` **Regression run** Smoke run by @⁠Toons29 in comment below: https://git.gtahub.st/github/xcl-dev--xcl/pulls/884 Contributed by a community tester; the PR author did not run it. Both blocks are pasted as posted. main: ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` PR: ``` xcl-smoke v1 commit: 958c517e218843d9701b96d3d27603a5f92dbe90 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1621 (PresentX 1621) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14811 MB ``` Tester's setup and limits: AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, `cargo build --release -p xcl`, 60 s, `XCL_NETWORK=0`. This is a regression check only: it does not show whether the run reaches the code this PR changes. Tester's AI use: Claude Code (Claude Opus 5.5) built both trees and ran `scripts/smoke.ps1`; the tester checked the blocks before posting. **Not tested** - No retail title was available to the author; whether the community regression run above reaches the code this PR changes is not known. **AI use** - Authored with Claude Code (Claude Opus 5.5); reviewed, audited, and verified on native Windows/RTX 5080 hardware with Antigravity (Gemini 4 Neon + Claude Sonnet 5.5). - Claude Code (Claude Sonnet 5.5) added the community regression run to this description and updated the lines that it made out of date or that the PR check requires.
decode_frame cloned the whole 32 KB reservoir buffer for every
512-sample frame, an allocation and a copy that cost about a sixth of
XMA decode time. Frame decoding only reads the reservoir, so take the
buffer for the frame and put it back afterwards. Output is unchanged
bit for bit.

Assisted-by: Claude Code (Claude Opus 5.5)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:35:38 +00:00
Author
Owner

@⁠ettehn on GitHub · 2026-11-24 07:27 UTC · original · edited 2026-11-24 07:29 UTC

Independent allocation check on Windows x64 (Ryzen 9 8945HX, rustc 1.97.1 MSVC, optimized). I built the unchanged XMA decoder modules from beaa245 and 6b825b8 in the same standalone harness, wrapping the system allocator to count alloc calls requesting exactly 32,832 bytes (the reservoir plus padding). After one warm-up, each build decoded the repository’s mono 48 kHz synthetic packet 100 times with EOS on the same decoder.

Main made 1,200 allocations of that size; the PR made 0. Each decode produced 6,080 f32 samples equal to the warm-up, and the serialized PCM SHA-256 matched across both builds: 530d467ae72347511f4365fc3bb08406d284a88acfe768bca567397f63aa449d. In a separate optimized run with the default allocator, I timed 5,000 decodes after 200 warm-ups per process. Ten paired runs alternated main/head order. Median elapsed time was 194.95 ms on main and 159.58 ms on the PR (22.2% more samples/s on this mono fixture). The PR was faster in all ten pairs. This is decoder-only throughput on this CPU.

OpenAI Codex (GPT-6.2 Astra) assisted with verification.

Originally posted by @⁠ettehn in https://github.com/ivall/xcl/issues/884#issuecomment-7125021832

**@⁠ettehn** on GitHub · 2026-11-24 07:27 UTC · [original](https://github.com/xcl-dev/xcl/pull/884#issuecomment-7125021832) · edited 2026-11-24 07:29 UTC Independent allocation check on Windows x64 (Ryzen 9 8945HX, rustc 1.97.1 MSVC, optimized). I built the unchanged XMA decoder modules from `beaa245` and `6b825b8` in the same standalone harness, wrapping the system allocator to count alloc calls requesting exactly 32,832 bytes (the reservoir plus padding). After one warm-up, each build decoded the repository’s mono 48 kHz synthetic packet 100 times with EOS on the same decoder. Main made **1,200** allocations of that size; the PR made **0**. Each decode produced 6,080 f32 samples equal to the warm-up, and the serialized PCM SHA-256 matched across both builds: `530d467ae72347511f4365fc3bb08406d284a88acfe768bca567397f63aa449d`. In a separate optimized run with the default allocator, I timed 5,000 decodes after 200 warm-ups per process. Ten paired runs alternated main/head order. Median elapsed time was 194.95 ms on main and 159.58 ms on the PR (22.2% more samples/s on this mono fixture). The PR was faster in all ten pairs. This is decoder-only throughput on this CPU. OpenAI Codex (GPT-6.2 Astra) assisted with verification. _Originally posted by @⁠ettehn in https://github.com/ivall/xcl/issues/884#issuecomment-7125021832_
Author
Owner

@⁠SlopTart on GitHub · 2026-11-24 09:34 UTC · original

Linux / AMD RDNA3 datapoint.

Merged state: head 958c517 onto main 077ce40, clean merge.

Full suite, cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 706 passed, 10 failed, 41 ignored; failures are the known Wine-lane set from my #42 rows plus this rig's title-textures flake family (see #718). bit-reservoir borrow tests green.

Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. My Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

**@⁠SlopTart** on GitHub · 2026-11-24 09:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/884#issuecomment-7127592239) Linux / AMD RDNA3 datapoint. **Merged state:** head `958c517` onto main `077ce40`, clean merge. **Full suite, `cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4`:** **706 passed, 10 failed, 41 ignored**; failures are the known Wine-lane set from my #42 rows plus this rig's title-textures flake family (see #718). bit-reservoir borrow tests green. **Not tested:** retail titles (none here), Windows, RDNA4. Counts only, no game data. **Machine:** RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross `x86_64-pc-windows-gnu`, `XCL_NETWORK=0`, release profile, `--test-threads=4`. My Linux rows: #42 / #718. AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 03:06 UTC · original

Regression run of 958c517e on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 958c517e218843d9701b96d3d27603a5f92dbe90
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1621 (PresentX 1621)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14811 MB

Against the limits in CONTRIBUTING.md: 100 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

**@⁠Toons29** on GitHub · 2026-11-25 03:06 UTC · [original](https://github.com/xcl-dev/xcl/pull/884#issuecomment-7151593806) Regression run of `958c517e` on the retail title, for this PR's **Regression run** section. Both blocks are from `scripts/smoke.ps1` as printed, not edited: `main` on `4178e888`, this PR on its head commit with no uncommitted changes, same title and time. ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` ``` xcl-smoke v1 commit: 958c517e218843d9701b96d3d27603a5f92dbe90 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1621 (PresentX 1621) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14811 MB ``` Against the limits in CONTRIBUTING.md: 100 % of `main`'s frames, 100 % of its peak commit, no crash, no device removal. AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, `cargo build --release -p xcl`, 60 s, `XCL_NETWORK=0`. This is a regression check only: it does not show whether the run reaches the code this PR changes. AI use: Claude Code (Claude Opus 5.5) built both trees and ran `scripts/smoke.ps1`; I checked the blocks before posting.
Author
Owner

@⁠JJAutoCompare on GitHub · 2026-11-25 05:45 UTC · original

Empirical test report (958c517e)

Tested commit 958c517e218843d9701b96d3d27603a5f92dbe90 from PR #884.

Hardware / environment

  • Windows build 26200, x64 MSVC toolchain. AMD64 Family 23 Model 113 Stepping 0 (AuthenticAMD), 12 logical processors visible to the test process.
  • rustc 1.100.0 (0ef1bc386 2026-11-09). DXC 1.9.2609.5 (01b62ad47). Cargo used the test profile with debug info and incremental compilation disabled.
  • Both commands ran outside Invoke-ArcTest.ps1. The focused run set XCL_ADAPTER=NONE_CPU_ONLY and RUST_TEST_NOCAPTURE=1. The selected tests use the Rust decoder and in-memory fixtures, with no D3D12 calls or GPU use.

Commands and results

  • cargo test --workspace --no-run exited 0 after compiling the workspace test targets.
  • cargo test -p xcl --lib hle::xma2::tests:: -- --nocapture exited 0. 7 passed, 0 failed, 0 ignored, 0 skipped. No case-sensitive SKIPPED lines appeared in the captured output.

Observed behavior

  • The cross-packet fixture produced 2,860 samples after trimming. The test compared selected samples against FFmpeg reference values and found identical output for streaming and single-shot decoding. This exercises the reservoir across a packet boundary.
  • The XMA1 and XMA2 fixtures matched the asserted sample counts and FFmpeg reference points. The mono fixture also produced identical samples when the decoder was reused after end of stream. The suite checked 5.1 channel interleaving and corrupt and empty packets.

Not tested

The full GPU workspace suite and game smoke runs were omitted. This run also omitted XAudio2 playback and performance or allocation measurements.

AI use

Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr884-focused.log and published.

**@⁠JJAutoCompare** on GitHub · 2026-11-25 05:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/884#issuecomment-7154106319) ### Empirical test report (958c517e) Tested commit `958c517e218843d9701b96d3d27603a5f92dbe90` from PR #884. **Hardware / environment** - Windows build 26200, x64 MSVC toolchain. AMD64 Family 23 Model 113 Stepping 0 (`AuthenticAMD`), 12 logical processors visible to the test process. - `rustc 1.100.0 (0ef1bc386 2026-11-09)`. DXC `1.9.2609.5 (01b62ad47)`. Cargo used the test profile with debug info and incremental compilation disabled. - Both commands ran outside `Invoke-ArcTest.ps1`. The focused run set `XCL_ADAPTER=NONE_CPU_ONLY` and `RUST_TEST_NOCAPTURE=1`. The selected tests use the Rust decoder and in-memory fixtures, with no D3D12 calls or GPU use. **Commands and results** - `cargo test --workspace --no-run` exited 0 after compiling the workspace test targets. - `cargo test -p xcl --lib hle::xma2::tests:: -- --nocapture` exited 0. 7 passed, 0 failed, 0 ignored, 0 skipped. No case-sensitive `SKIPPED ` lines appeared in the captured output. **Observed behavior** - The cross-packet fixture produced 2,860 samples after trimming. The test compared selected samples against FFmpeg reference values and found identical output for streaming and single-shot decoding. This exercises the reservoir across a packet boundary. - The XMA1 and XMA2 fixtures matched the asserted sample counts and FFmpeg reference points. The mono fixture also produced identical samples when the decoder was reused after end of stream. The suite checked 5.1 channel interleaving and corrupt and empty packets. **Not tested** The full GPU workspace suite and game smoke runs were omitted. This run also omitted XAudio2 playback and performance or allocation measurements. **AI use** Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against `pr884-focused.log` and published.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 09:52 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 09:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/884#issuecomment-7158914199) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/884/head:github-bot/gh-884'
git switch 'github-bot/gh-884'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-884'
git switch 'github-bot/gh-884'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-884'
git switch 'github-bot/gh-884'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-884'
git switch 'main'
git merge --squash 'github-bot/gh-884'
git switch 'main'
git merge --ff-only 'github-bot/gh-884'
git switch 'main'
git merge 'github-bot/gh-884'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.