Skip to content

rsc8_audio: optional native header and table kernels (tools/audio_native), 43x-140x per table, Python fallback (PERF-RSC8-AUDIO-NATIVE) - #8947

Open
TG0000 wants to merge 2 commits into
ItsNotRexed:mainfrom
TG0000:perf/rust-audio-native
Open

TG0000 wants to merge 2 commits into
ItsNotRexed:mainfrom
TG0000:perf/rust-audio-native

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Item: PERF-RSC8-AUDIO-NATIVE (claimed in #8868). AI-assisted (Claude Code): an agent wrote and checked it on CPU only, using synthetic data only.

What changed

tools/rsc8_audio.py has no PCM or ADPCM code. Its per-word Python loops are the AWC (ADAT) header parse and the companion-table decoders, which run for every stream of a scan. This adds an optional Rust cdylib for those loops:

  • tools/audio_native/ (no dependencies, [dependencies] empty, panic = "abort"): audio_adat_header_v1, audio_grid20_v1, audio_index_runs_v1, audio_markers_v1, audio_marker_has_value_v1, plus audio_native_abi().
  • tools/_audio_native.py: the ctypes adapter. Every function returns None to fall back.
  • tools/rsc8_audio.py (+43/-2): one fast-path call in each of parse_adat_header_bytes, decode_grid20_table, decode_index_runs and decode_marker_records, placed after the reference's own size checks. In _tally_undocumented_tables, the start/end cue check runs before marker_records(), so a tag with no region cue no longer builds its record list. The Python code is unchanged and stays the specification.

The reference runs when:

  • the library is missing or fails to load;
  • it reports another ABI or declines a call (an output buffer is too small or an argument is out of range);
  • the input is not bytes;
  • the table is below the adapter's MIN_* size, where the ~1 µs ctypes call costs about as much as the Python loop;
  • or LEONIDA_AUDIO_NATIVE=0 is set.

Numbers

Python 3.12.10, Windows 11, one core, synthetic inputs. Best of 3 runs (Python) and best of 7 (native), same process, results compared with repr; ranges are over three separate runs:

Python native speedup
decode_grid20_table, 100,000 entries 27.7-29.9 ms 0.198-0.33 ms 90x-140x
decode_index_runs, 1,000 groups x 100 9.4-12.9 ms 0.213-0.218 ms 43x-61x
decode_marker_records, 100,000 records 41.2-43.8 ms 0.96-1.0 ms 43x-44x
parse_adat_header_bytes, 50,000 streams x 3 tags 159-176 ms 68-89 ms 2.0x-2.3x
decode_awc_bank x6 banks (40 streams each, 150-400 0x5a entries, 20-60 0xd9 groups, 200-600 0xbd records, a clip on every 4th stream) 133-153 ms 24.6-28.4 ms 5.0x-5.9x
  • The header parse is bound by building its result. The result is a list of ~200,000 Python tuples, so the adapter unpacks the kernel's words with struct.iter_unpack and islice. It does not touch the cyclic GC (a library call must not pause it for other threads), and the collector runs during that build in both paths, which is most of what is left.
  • Whole scans of real banks will gain less. docs/rsc8_audio.md counts 781,350 records in 420,168 0xbd tags, which is about 2 records per tag. Those tags stay below MIN_MARKER_BYTES and run in Python. The large 0x5a/0xd9 tables and big headers are where the kernels pay off.

Parity

tests/test_audio_native.py compares each fast path against the reference with repr, so key order, None and bool must match too, as must the first-seen order of the names dict:

  • Per kernel: 1,500 seeded cases each, valid and broken:
    • ADAT headers: random flag sets, 0-400 streams, tags past the file end, a bad count/hdr_sz, truncation, a bad magic, a zero-stream bank whose tables do not fill the header, a non-zero bank_offset;
    • grid tables: unsorted and repeated positions, a bad p, a bad magic, truncation;
    • index runs: overrunning groups, a bad marker, several hashes, a mismatched declared count, and the MAX_INDEXRUN_GROUPS cap (patched down to 5);
    • markers: descending offsets, 1-30 names, truncation;
    • the region-value check, with trailing partial records.
  • Edges: sizes on both sides of every MIN_* threshold, 0xFFFFFFFF words, and bank_offset/file_size at 2^63, at 2^64 (declined), and negative (declined).
  • Whole banks: 30 synthetic banks through decode_awc_bank, with identical JSON with and without the kernels.
  • Proof the native path ran: each parity test counts the calls the kernel answered.
  • Fallback: the opt-out variable, a missing library, a CDLL that raises, a library with another ABI, declined inputs, and every kernel returning its decline code. Each of these still gives the reference result.
  • Rust unit tests: 5 (header round trip, zero-stream bank, grid, index runs, markers).

Tests run

  • cargo build --release --locked and cargo test --release --locked in tools/audio_native: no warnings; 5 tests pass.
  • python -m compileall -q make_game.py tools tests and python -I make_game.py --help: pass.
  • python -I tests/test_audio_native.py (14 tests, with LEONIDA_AUDIO_NATIVE_BENCH=1): pass. With LEONIDA_AUDIO_NATIVE=0 the 11 native-only tests skip and the rest pass.
  • python -I tests/test_rsc8_audio.py (49 tests) with and without the library: pass. test_awc_markers.py, test_awc_cue_labels.py, test_awc_manifest.py and test_audio_wav.py: pass, with skips where game files are needed.
  • scripts/check_no_game_data.py on the new and changed files: clean.
  • test_text_write_line_endings.py fails on tools/leonida_tui.py:753 and tools/wave_diff.py:373, the same as on main. Neither file is touched here.
  • CI: a new step in the check job builds and tests the crate offline (like the BCPack step), asserts that the library loads, and runs both test files.

No GPU or driver is involved, and tests/semantic was not run because nothing in the translator changed.

Overlap

#7266 (TG0000, pure Python) rewrites the bodies of the same four decoders, so a trial merge conflicts in those functions. Whichever lands first, the other gets a rebase: the fast-path calls sit right after each function's size checks, so they port to either version of the loops. None of the other open rsc8_audio PRs touches these functions.

Depends on: none.

🤖 Generated with Claude Code

TG0000 and others added 2 commits November 23, 2026 20:50
…ive), Python fallback (PERF-RSC8-AUDIO-NATIVE)

A zero-dependency Rust cdylib for the word loops of parse_adat_header_bytes,
decode_grid20_table, decode_index_runs, decode_marker_records and the
region-cue check before pair_marker_regions, loaded with ctypes by
tools/_audio_native.py. The Python code stays the reference and runs when the
library is missing, fails to load, reports another ABI, declines a call, the
table is below the call-overhead threshold, or LEONIDA_AUDIO_NATIVE=0.

tests/test_audio_native.py: 1,500 seeded synthetic cases per kernel (valid
and broken), edges at every threshold and word bound, whole banks through
decode_awc_bank, fallback cases, and optional timings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ic GC

A library call must not switch off the collector for the whole process
(another thread may be allocating). The entries are still unpacked with
struct.iter_unpack and islice; parse_adat_header_bytes is now ~2x faster
than the reference on a 50,000-stream header instead of ~5x.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TG0000 TG0000 changed the title rsc8_audio: optional native header and table kernels (tools/audio_native), 44x-154x per table, Python fallback (PERF-RSC8-AUDIO-NATIVE) rsc8_audio: optional native header and table kernels (tools/audio_native), 43x-140x per table, Python fallback (PERF-RSC8-AUDIO-NATIVE) Nov 23, 2026
@kawaii-shakal

Copy link
Copy Markdown

Tested head a7af291 on Linux x86-64 (WSL2), CPython 3.14.10, cargo 1.100.0. CPU only, synthetic data only, no game files.

Result: no defect found.

What ran:

  • cargo build and cargo test --release --locked --offline: 5 of 5 pass. The crate has no build.rs and no dependencies.
  • tests/test_audio_native.py: 14 tests, 1 skipped (the benchmark). tests/test_rsc8_audio.py: 49 pass. Both also pass with the PR merged into main at 6312c05.
  • test_awc_markers, test_awc_cue_labels, test_awc_manifest and test_audio_wav ran with the library on and with LEONIDA_AUDIO_NATIVE=0, with the same last line of output in both modes (16 skips in test_audio_wav).
  • All 477 tests/test_*.py at the PR head against the 476 on main: 37 fail here, 38 on main. The only difference is test_docs_env_vars, which fails on newer main because of LEONIDA_TLS_GA_PATCH, not this PR. It fails the same way after a merge, naming only that variable.
  • A 150 s differential fuzz, native against the Python reference, comparing repr or the error type and text, on the merged tree. Valid and damaged inputs (bit flips, overwritten words, truncation, trailing bytes), bank_offset up to 2^64 and file_size up to 2^70:
Function Cases Mismatches
decode_grid20_table 316,749 0
decode_index_runs 316,749 0
decode_marker_records 316,749 0
marker region-value check 316,749 0
parse_adat_header_bytes 316,749 0
decode_awc_bank 316,749 0

The kernels answered 407,218 header, 128,139 grid, 238,287 index-run, 152,493 marker and 202,844 region-value calls. The rest were below the MIN_* sizes, declined, or Python-only runs.

Not tested: Windows or macOS, real AWC banks, the PR's timing numbers, and the CI step itself.


Review written by Claude (Claude Code, Opus 5.5), an AI agent, posted from a contributor's account.

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.

2 participants