formats: xma: decode Huffman codes with a 2-level O(1) lookup table #362
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!362
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-362"
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?
@lucamzanon on GitHub · 2026-12-03 20:13 UTC · original
lucamzanon:xmem/2-xma-vlc→main· +1019 −8 · 1 files · 3 commits · merged 2026-12-05 17:27 UTCSummary
Replaces the per-symbol
partition_pointbinary search inVlc::read(crates/victory-formats/src/xma.rs) with a compact 2-level O(1) lookup table and single-loadpeek32, keeping hot primary tables to 19 KB (63 KB total across all 7 XMA tables).Design
Entry(u16symbol/subtable offset,u8length,u8subtable bits).min(total_entries + 2^bits):scale: 9 bits (512 primary / 1,536 total)scale_rl: 10 bits (1,024 / 3,264)coef[0]: 10 bits (1,024 / 3,742)coef[1]: 11 bits (2,048 / 6,520)vec4: 7 bits (128 / 478),vec2: 7 bits (128 / 334),vec1: 6 bits (64 / 328)partition_pointlogic so every entry is bit-exact by construction.085263b).Verification & Benchmarks (Ryzen 7 5700X / RX 9070 XT, Linux RADV)
vlc_tables_match_the_binary_search_on_every_prefixchecks all 2^maxlen prefixes (~8.9M lookups, up to 2^22 forcoef1) against the oldpartition_point+ byte-by-bytepeek32; 8 end-to-end PCM checksums (mono/stereo × 4 sample rates) match bit-for-bit.cargo test -p victory-formats --lib xma18/18 passed, Windows cross-compilex86_64-pc-windows-msvc+clippy --all-targets -D warnings+rustfmt --checkclean.VICTORY_BENCH=rt, 4K FSR 4 Quality, scripted moving Prison2m x 4, RX 9070 XT): 0 crashes, +2.38% 1% low (10.75vs10.50 fps; avg neutral at GPU-bound 4K RT).@Kal0s-design on GitHub · 2026-12-03 21:08 UTC · original
Tested
d06d065on Windows 11 with a native MSVC build, only-p victory-formats. Main hasn't touched this crate since the PR base, so I compared against current main.For the differential check I copied main's
Vlc::new,Vlc::readand the old 5-bytepeek32into a local test module (not committed) and ran them next to the new tables for all seven of them. Every code of every table at bit offsets 0 to 31, followed by 0, 1, 7 or 40 random bits, and the same buffers cut short at every byte so the code gets truncated. That's 910k reads with the same symbol and position, and the complete codes all give the expected symbol. Then every prefix up to each table's longest code length, with zero, one or random low bits at a random offset in the byte, 8.9M reads, all identical. Then random bitstreams of 0 to 200 bytes, some uniform and some mostly zeros or mostly ones, from a random start bit, picking tables at random and mixing in plainread(n)calls, and reading on until 40 bits past the end of the data. 60M reads in release and 6M in debug gave the same symbol, position andpeek32every time, and about 24M of those started within 32 bits of the end or beyond it.For whole frames I took frames from your synthetic
Streamand left some as they were, truncated some by bits or bytes, flipped a bit in some and replaced some with random bytes. I dumped them with the PR build and decoded the same input on main. In release that was 30720 frames, 16703 of them returning an error, and in debug 12288 frames with 6764 errors. Every frame gave the same result and the same PCM on both sides, flush output included.All seven tables are complete and stored sorted, so any 32 bits decode to some symbol, same as before. I also fed random code sets to both builders. Well-formed ones (about 22k) matched every time. Sets whose lengths are not prefix-free trip a debug_assert in debug and decode differently from the old search in release, but the constant XMA tables can never be like that, so this only matters if
Vlc::newis ever reused for other tables. The biggest table has 6520 entries, so u16 offsets are nowhere near the limit, and all symbols fit in i16.cargo test -p victory-formatspasses in debug and release, 57 passed and 9 ignored here against 52 and 7 on main. The new tests passed 20 runs out of 20. clippy with--all-targetshas no warnings on either side and fmt is clean.Your
vlc_throughputin release puts the table at roughly 5.2x to 6.7x faster per code than the binary search, depending on the table. I only timedreadby itself, not a full decode.AI-assisted: yes
@Luxuride on GitHub · 2026-12-04 12:10 UTC · original
Most complete PR in this batch, and the only one that holds up against today's
mainafter 173 commits:crates/victory-formats/src/xma.rsis untouched onmainsince your base, so the diff still applies to the real file, and I rancargo test -p victory-formats --lib xmatwice on Linux — 18 passed, 5 ignored — once on your head, once with only this file swapped into currentmain.Two hardening lines, then it is a yes.
lookup_tableonly fills codes present in the frame's length table, so for an incomplete (corrupt) set a primary entry can stayEntry::EMPTY, or a sub-table entry can beEMPTY, andVlc::read(crates/victory-formats/src/xma.rs:310) doesbits.pos += 0and returnsi32::from(0 as i16). The frame loop (xma.rs:853) gets no progress from that andoverread()is only tested at the end of the frame, so a crafted or garbage packet can spin instead of returningErr. Oneif e.len == 0 { return Err(..) }(or advancingbits.pos) closes it. It is reachable only from bad input, but "only from bad input" is where a decoder spends its adversarial budget.Entry.valueisu16(xma.rs:261) and the sub-table base is stored in it with a debug-onlydebug_assert!(xma.rs:384). In release, an unusually wide table truncates the base and decodes to the wrong entries silently. Theoretical with the 64–2048-entry tables you measured, but au32field costs nothing.Behaviour is otherwise preserved exactly, which is the part I checked hardest:
answer = partition_point(c.0 <= v) - 1(xma.rs:358) is the same expression the old binary search used, so unreachable prefixes map to the same longer code as before — including the over-advance, which is therefore not a regression.peek32's(u64::from_be_bytes(*b) << shift >> 32)is the same 32 bits as the old five-byte loop, andpeek32_loads_eight_bytes_like_fivecovers it.One honest note on evidence: the bit-exact PCM parity test against a reference decoder is
ignored(it needs outside files), so the parity you have actually demonstrated is the synthetic generator and the noise case, not a real soundtrack. A stale branch this clean should not still be in the pile.d06d0652c9toc71a806ae9@lucamzanon on GitHub · 2026-12-04 12:57 UTC · original
Thanks @Luxuride and @Kal0s-design for the thorough reviews!
Rebased cleanly onto
main(0fd7614) and applied both hardening items inc71a806:e.len == 0) inVlc::read: sub-table lookup only runs whene.bits > 0, and any remaininge.len == 0forcesbits.pos = bits.end.saturating_add(1)sooverread()trips immediately instead of spinning with zero progress. Addedempty_entry_marks_overread_instead_of_spinningcovering both primary and sub-tableEntry::EMPTY.Entry.valuefromu16tou32(withu32::try_from(at)instead of a debug-onlyu16assert).Re-verified
cargo test --locked --lib -p victory-formats xmain debug & release (19/19 pass),cargo clippy --no-deps --all-targets -p victory-formats -- -D warnings, andrustfmt --edition 2024 --check.