Formats/XMA: add 10-bit VLC lookup table, u64 bit-peek, and quant-factor LUT #2661
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2663"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-25T17:44:02Z.
Claimed in #1633: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7158204478
Summary
Speeds up hot XMA2 frame decoding paths in
crates/victory-formats/src/xma.rs(+212/-2) with bit-for-bit identical decoded output:Bits::peek32: reads a single unalignedu64::from_be_bytesword on the hot path (>= 8bytes remaining) with a 5-byte tail fallback.Vlc10-bit primary table: precomputes aBox<[(i32, u8); 1024]>table inVlc::newresolving >99% of Huffman symbols in O(1), falling back to prefix-bounded binary search for codes > 10 bits.quant_factorLUT: caches10.0_f32.powf(exp as f32 / 16.0)in a bit-exactOnceLock<[f32; 384]>table forexp in -128..256(verified via.to_bits()equality across the full range).Measured (
--release xma::tests::vlc_and_quant_throughput)main)coef028.09 ns/sym6.30 ns/symcoef123.47 ns/sym5.86 ns/symvec423.76 ns/sym5.58 ns/symquant_factor10.69 ns/op1.18 ns/opNot a duplicate of
crates/victory-formats/src/xma.rs.Verification
windows-latestMSVC + WARP SM 6.6 CI (both clippy-D warningsprofiles,xtaskpolicy 6/6,victory-formats41/41 including exhaustive bit-exact equivalence tests forpeek32,Vlc, andquant_factor): https://github.com/RVZO6/VIctory/actions/runs/45666985023AI-assisted: T3 Code (Antigravity, gemini-4-neon)
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T17:57:06Z.
Reviewed: the 10-bit direct lookup is the standard ff_vlc optimization — prefix span fill for len ≤ FAST_BITS, else first-encountered fallback index;
peek32's u64 big-endian read covers bit offsets 0–7 for a full 32-bit window, tail path preserved for the last 8 bytes. CI green.Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T18:22:59Z.
Independent verification of #2663 (XMA: 10-bit VLC lookup, u64 bit-peek, quant-factor LUT)
PR head
485fb82b4dcf0c8075677b9d5006a3bb3b4c8f12, merges cleanly ontoorigin/main13f91caee25f2fdf2c062be5f7a27f058e51cbca.Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1, CPU only (
VICTORY_WARP=1/VICTORY_D3D12_WARP=1still set; this crate touches no GPU),cargo ... -j 4 --locked.Tests/clippy vs baseline.
origin/main:cargo test -p victory-formats --lib --locked= 38 passed, 0 failed, 5 ignored. With #2663: 41 passed, 0 failed, 6 ignored (+3 tests, +1 ignored benchmark).cargo clippy -p victory-formats --all-targets --locked: 0 warnings on both.Equivalence. Full diff read.
peek32: the 8-byte path reads bitspos..pos+32from oneu64(at most 7+32 bits) and falls back to the old zero-padding loop whenever fewer than 8 bytes remain, so reads past the buffer still see zeros as before. VLC: codes are contiguous, so the first code with a given 10-bit prefix starts that prefix's region; prefixes with no code (the unused tail of an incomplete table) fall back to a search from index 0, which reproduces the old "last code <= v".quant_factoris the samepowfevaluated once at startup, so it is bit-exact within a build, with apowffallback outside [-128, 256). Tests added:peek32vs the byte loop at every position for lengths 0-24; all 7 Huffman tables vs binary search at each code's first and last value plus a 4 KiB random stream;quant_factorbits vspowffor -200..300.Perf (release, in-PR benchmark). VLC read 21.5-24.6 -> 4.1-4.3 ns/symbol (5.3-5.7x) on the coef0/coef1/vec4 tables; quant factor 8.63 -> 0.74 ns (11.6x). Synthetic random bitstreams.
Findings: no bugs. Observation: each
Vlcnow owns an 8 KiB lookup table (7 tables), negligible.Limits: no game files, so no real XMA streams were decoded; coverage is synthetic (exhaustive at code boundaries plus random streams).
AI-assisted: independent verification by Claude Code / Claude Sonnet 5.5, run on behalf of oni-swr. Claims for #1633.
upstream-sync referenced this pull request2026-11-25 19:06:38 +00:00
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:22:14Z.
Automated triage for the maintainers.
Merge candidate (performance, same output). In
crates/victory-formats/src/xma.rs:Bits::peek32reads one big-endian u64 when 8 bytes remain and otherwise falls back to the old loop.Vlcgains a 10-bit direct lookup table, with the old binary search kept for longer codes.quant_factorcaches10^(exp/20)for exponents in [-128, 256), computed once with the samepowf.I checked that codes are handed out contiguously, so a 10-bit region holds either one short code or long codes starting at the region's start. The slow-path search from that index therefore cannot underflow, and the empty tail falls back to a full search, as before. The tests compare against the old paths at every code boundary, at every bit position and for exponents -200..300. The "title" (XMA) data path is unchanged in semantics; the LUT memory is about 8 KiB per table. It merges cleanly; CI
check: passing.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:50:43Z.
Code review and Windows 11 release benchmark verification at head
485fb82b4dcfmerged cleanly ontomain(63bcca43c6a0):crates/victory-formats/src/xma.rs):Bits::peek32(xma.rs:203-210): takes theu64::from_be_bytesfast path only whenself.data.get(byte..byte + 8)isSome, falling back safely topeek32_tail()near stream end; bit shifts match across all bit offsets (fast_peek32_matches_byte_loopcheckslen in 0..=24,pos in 0..=(len*8 + 16)).xma.rs:258-306): fills1 << (10 - len)prefix slots forlen <= 10, stores the first sorted-table index(idx, 0)forlen > 10falling back to#[cold] read_slow(partition_point(|c| c.0 <= v) - 1; sincecodes[0].0 == 0 <= v,partition_point >= 1never underflows). Verified across all 7 XMA2 Huffman tables.xma.rs:320-338, line 1169):OnceLock384-entry table forexp in [-128, 256)with single unsigned range check viawrapping_sub, falling back to10f32.powf(...)out of range;quant_table_matches_powf_bitsverifies exact bit equality across-200..=300.cargo xtask policy:191 files ok;cargo clippy -p victory-formats --all-targets -- -D warnings: 0 warningscargo test --locked -p victory-formats --all-targets: 49/49 passed (6 ignored)--releasexma::tests::vlc_and_quant_throughput:vlc coef0:21.13 -> 3.45 ns/sym(6.12x)vlc coef1:18.60 -> 3.15 ns/sym(5.90x)vlc vec4:16.66 -> 3.00 ns/sym(5.55x)quant:4.84 -> 0.42 ns/op(11.56x)Clean, bit-exact, and a huge speedup; LGTM.
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T04:31:52Z.
Review: APPROVED
Tested merged onto
origin/main(63bcca4) on Windows 11 x64:cargo test -p victory-formats --lib xma::: 23 passed, 0 failed, 4 ignored (including newfast_peek32_matches_byte_loop,quant_table_matches_powf_bits, andtabled_vlc_matches_binary_search).cargo clippy -p victory-formats --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-26T11:40:48Z.
NVIDIA full-suite run, to go with the reports above by Dukestar69, fuzeofficlal, oni-swr, since none of them names a GPU run. Tested exact head
485fb82(on its base13f91ca).Tested
dxil.dllfrom Windows SDK 10.0.26100 (the loader's default), debug.cargo build --workspace --locked, thencargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 776 passed, 0 failed, 10 ignored.fast_peek32_matches_byte_loop,tabled_vlc_matches_binary_search,quant_table_matches_powf_bits.--skipleaves out tests that fail on main63bcca4on this card too, not because of this PR:heap_direct_texture_reads_mark_their_heap_indexends thevictory-shadertest process with0xc0000005(#2571), so without the skip the rest of that crate never reports;dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_viewsand the twostride_testshit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317);gathers_at_a_level_match_the_hardwarefails on lane 0 (#1744).Not tested
AI use
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T12:10:58Z.
Review: APPROVED
Reviewed and verified
485fb82bagainstorigin/main(63bcca43; cleangit merge-tree):Bits::peek32(crates/victory-formats/src/xma.rs:203-218) extracts identical bits to the 5-byte tail path across all bit alignments, short buffers, and past physical EOF withoutbyte + 8overflow.prefix << 22 <= vand empty buckets fall back to index0, sopartition_point(|c| c.0 <= v) - 1inVlc::read_slow(xma.rs:300-306) cannot underflow; documenting or asserting that invariant inVlc::newwould make the safety contract explicit.quant_factor(xma.rs:323-338, 1169) matches baseline'sexp / 20.0across[-128, 255]and falls back cleanly outside that range.Minor non-blocking notes: the PR description says
/ 16.0whereas both the baseline and LUT use/ 20.0, andcargo fmtflags line wrapping in the new test lines (xma.rs:1460..1554).Local verification on Windows 11 (Ryzen 9 9950X):
xtask policy(184 files ok) andcargo clippy -p victory-formats --all-targets -- -D warnings(default and--all-features): clean.cargo test -p victory-formats --locked(both debug and--release):41 passed; 0 failed; 6 ignored.--release vlc_and_quant_throughput:coef05.85x (19.89 -> 3.40 ns/sym),coef15.83x (18.04 -> 3.10 ns/sym),vec45.58x (16.15 -> 2.90 ns/sym),quant12.48x (4.64 -> 0.37 ns/op).Imported from Marvisak/VIctory. Originally posted by rezioliver-collab on 2026-11-26T18:00:57Z.
The speedup from the 10-bit lookup on Apple M1 depended on how many symbols reached the fallback. I compared
485fb82bwith its base13f91caeusing identical synthetic bitstreams.coef0speedupcoef1speedupvec4speedupWith all-long codes,
vec4went from 33.40 to 36.35 ns/symbol. A fresh build reproduced that result (33.94 to 36.90 ns/symbol). The long-code fractions were chosen explicitly to measure fallback cost.Each cell used 5,000 deterministic symbols encoded with the corresponding table. I checked every decoded symbol and its ending bit position against the encoder’s record. All 45,000 matched on both revisions. The factors are median base/PR ratios from 12 paired release runs with alternating order, macOS 14.7, Rust 1.98.1.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T00:11:05Z.
On Windows I decoded the same 62 generated mono frames on main
63bcca4and on485fb82merged onto it. The fixture varies every ordinary signed quantizer step, then flushes the decoder. All four outputs matched byte for byte, with 32,000 PCM samples per run and 31,721 nonzero samples.I put the full-decoder regression in test-only commit 165f0a7. It compares every PCM sample with the direct gain formula and the existing untabled inverse-transform reference. The generated single-bin frame came from #2795. On that commit, 50 release tests passed with six ignored, and strict Clippy and the asset policy check passed.
One description nit: the gain divisor shown in the summary is 16.0. The implementation and the reference both use 20.0.
If you reuse the test, please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg [email protected] if folding it into another commit.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T19:09:56Z.
I combined the XMA table changes at
485fb82with the in-place coefficient scaling from #3783. Combined ad7d8d5 has both tested commits as parents. The dequantizer keeps this PR's quantization lookup and scales the decoded coefficient slices in place.Against this PR with current main, 1,152 generated mono and stereo frames at four sample rates gave 1,053,696 bit-identical PCM samples and 1,769,472 bit-identical float output words. The optimized Windows CPU benchmark medians were 0.998x, 0.997x and 0.996x for the 128-, 256- and 512-sample subframes. All 49 active formats tests, strict all-feature Clippy and the asset policy passed.
RVZO6 can use the combined branch if both changes are picked up. Please retain the Git authors on cherry-pick or rebase, or include Co-authored-by: andrewpozdnakov7-jpg [email protected] if folding the in-place change into another commit.
AI-assisted with OpenAI Codex.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.