Skip to content

rsc8_audio: table decoders read their words a block at a time, 35-40% faster, output unchanged (PERF-RSC8-AUDIO) - #7266

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/rsc8-audio-fast
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/rsc8-audio-fast

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Item: PERF-RSC8-AUDIO (claimed in #7049). AI-assisted (Claude Code); written and checked by an agent, CPU only, synthetic data only.

What changed

tools/rsc8_audio.py has no PCM/ADPCM code; its hot loops are the AWC bank header parse and the companion-table decoders that run for every stream of a scan (420,168 0xbd tags in audio.rpf). Those now read their words a block at a time instead of one struct call and one Python iteration per word:

  • parse_adat_header_bytes: one unpack_from for the stream words and one per stream's tag rows.
  • decode_grid20_table: 256 entries per unpack_from; strictly-increasing positions with all(map(lt, ...)), p % 32 as word2 & 31 with any(map(...)).
  • decode_marker_records (now a thin wrapper of _decode_marker_records): 256 records per unpack_from; names counted with a Counter (first-seen order kept), ascending/max checks with map/max on slices. The region-word flag comes out of the same pass, so _tally_undocumented_tables no longer unpacks the tag a second time, and a tag with no region cue never builds its record list.
  • decode_index_runs: the ascending check is all(map(lt, run, run[1:])) and stops once a run has descended.
  • pair_marker_regions: the "no region cue" early-out is a set lookup.

No change to the CLI, progress, error handling, scan_*, the report writer or any returned key, value or order. Not touched: the lines of #6361, #4355, #5608. #5518 and #6780 re-add the region pairing that main already has, so a trial merge of them conflicts in the 0xbd block with this branch (it needs a rebase of theirs whichever lands first); #6361, #4355 and #5608 merge clean.

Numbers

Python 3.12.10, Windows 11, one core, synthetic banks (6 banks x 40 streams: 0x76, 0x5a 150-400 entries, 0xd9 20-60 groups, 0xbd 200-600 records with region cues on a third of the streams, 0x55, and an RSC8 clip on every fourth stream). Minimum of 25 interleaved runs, base and branch in one process:

before after change
decode_awc_bank x6, in memory 144.6-153.7 ms 91.4-92.8 ms 37-40 % less time (1.58-1.66x)
scan_dir of the same files (with file reads), best of 7, three runs 152.5-157.2 ms 99.5-115.2 ms 27-35 % less time

Memory (tracemalloc peak): decode_awc_bank x6 750 KiB -> 754 KiB (+4 KiB, 0.5 %, the per-block words); scan_dir 1681 KiB -> 1670 KiB; the 16 MiB-tag region tally 588,835 KiB -> 588,730 KiB. A 16 MiB 0xbd tag or a 300,000-entry 0x5a table now needs a bounded transient of 72 KiB / 64 KiB however large the tag (the block size caps it).

Parity

  • scan_dir JSON (records + summary, elapsed_seconds dropped) is byte-identical to before (same SHA-256 on every run), and decode_awc_bank results are repr-identical.
  • A differential fuzz against the previous file (not committed): 6,000 random grid/index-run/marker/region inputs including truncated and unsorted ones and sizes at the block edges, and 8,000 random ADAT headers (3,967 valid), all equal.
  • tests/test_rsc8_audio_tables.py (new, 21 tests): each decoder against a per-word reference at both sides of the block edge, every invariant broken at each block edge, truncated and random tables, names first-seen order, the z half of the p word, ADAT flags/zero-stream/tag-past-end/bank-offset cases, the tally path, and the bounded-memory ceiling. Mutating the block code (a <= for <, a 15 mask for 31, a >= bound, a wrong stride) makes it fail.

Tests run

python -m compileall -q tools/rsc8_audio.py tests; python -I tests/<f> for test_rsc8_audio.py (49), test_rsc8_audio_tables.py (21), test_awc_cue_labels.py, test_awc_manifest.py, test_awc_markers.py, test_awc_names.py, test_awc_opus.py, test_awc_shorts.py, test_audio_config.py, test_audio_stage_resume.py, test_audio_wav.py, test_file_mode.py: all pass (some skip without game files). scripts/check_no_game_data.py on the two files: clean. tests/semantic not run (no translator change). No GPU or driver involved. (test_text_write_line_endings.py fails on tools/leonida_tui.py:753 and tools/wave_diff.py:373, untouched here.)

Depends on: none.

🤖 Generated with Claude Code

…, output unchanged (PERF-RSC8-AUDIO)

parse_adat_header_bytes unpacks a stream's tag rows with one struct call
instead of one per word; decode_grid20_table and decode_marker_records read
256 entries/records per unpack_from and test ascending order, the p % 32
rule and the offset/extra maxima with all(map(...)), any(map(...)) and a
Counter; decode_index_runs stops comparing once a run has descended; and
the 0xbd tag is unpacked once (with its region-word flag) instead of twice
in _tally_undocumented_tables. pair_marker_regions' early-out is one set
lookup. The blocks keep the transient memory bounded (about 70 KiB however
large the tag). decode_marker_records, decode_grid20_table and
parse_adat_header_bytes return exactly what they did.

tests/test_rsc8_audio_tables.py compares each decoder with a per-word
reference on synthetic bytes at both sides of the block edge, with every
invariant broken at a block edge, truncated and random tables.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@airp1ane

Copy link
Copy Markdown
Contributor

Tested eb55b0d on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_rsc8_audio_tables.py - all exit 0.

AI-assisted (Claude Code).

@Kev-Ball

Copy link
Copy Markdown

Tested eb55b0dba against its direct parent 8575d36ed on the manifest-verified Xbox 1.0.2.1 audio archive. I ran scan_rpf(deep=True, collect_records=True, curves=True) on both revisions. All 7,162 banks, 1,606,241 streams, and 414,181 output records agreed. I checked the serialized summary and every ordered record, including marker and curve fields, by SHA-256. The only field excluded was the summary’s elapsed_seconds.

Process CPU time for the deep scan Parent PR
Seconds 6,343.7 5,193.9

The PR used 18.1% less CPU time in this one sequential pair (parent first). Timing covered the scan itself, with archive verification and report comparison outside the timed region. CachyOS, CPython 3.14.10, Core i9-12900K. AI-assisted QA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants