Skip to content

bcpack: Rust port of the lane-array model for the strict path (~30x lane layer, ~10x BC1 end to end) - #6029

Closed
TG0000 wants to merge 2 commits into
ItsNotRexed:mainfrom
TG0000:perf/bcpack-native-lanes
Closed

TG0000 wants to merge 2 commits into
ItsNotRexed:mainfrom
TG0000:perf/bcpack-native-lanes

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in #5781 (comment).

What

tools/bcpack.decode_lanes() is the lane (entropy) layer of the default strict BCPack path used by ttpf_mips and the assets stage. It now runs a Rust port of the lane-array model when tools/bcpack_calibration is built.

  • New export unpack_model (tools/bcpack_calibration/src/model.rs): a line-by-line port of _decode_lanes_raw, virtual zero bytes included. bcpack.py passes it its own probability tables, so both sides use the same models.
  • It accepts exactly the streams the Python model accepts, with the same symbols and byte count. When the library is not built, or a stream is refused, Python runs as before and raises its usual error. Strict output does not change.
  • No new Python dependency: ctypes only, no NumPy. LEONIDA_BCPACK_NATIVE=0 turns it off.

Why not the existing unpack (third_party/bcpack-rs)

The first commit used it. It follows the hardware RTL, which has no virtual zero bytes. As a result it refuses every q = 0 fragment whose omitted trailing zero byte comes before a symbol (docs/textures.md, flush = 1), and those chunks fell back to Python with no speed-up. The second commit replaces it with the model port. unpack and the calibrated path are not changed.

Measured (synthetic chunks only, no game files)

Python Rust model port
lane layer, 8,000 random chunks (all six codecs, 1 to 1,024 blocks) ~72 s ~2.4 s (~30×)
bcpack.decode end to end, 3 BC1 chunks of 16,384 blocks, q = 0 1.22 s (0.40 MB/s) 0.12 s (4.05 MB/s, ~10×)

The 8,000 chunks include sound ones, single- and multi-fragment ones (mixed widths, q and flush), and ones that are corrupted, cut short or followed by trailing bytes. Result: 3,705 accepted by both with identical lanes, 2,017 refused by both, 0 accepted by only one side, 0 mismatches.

Notes and findings (not fixed here)

  • Python and third_party/bcpack-rs disagree on validity: virtual zeros, as above. Which one real chunks need can only be counted with the game. The calibrated path already falls back to Python when unpack refuses.
  • 4 width-9 probability tables differ between tools/bcpack_tables.py and third_party/bcpack-rs/src/tables.rs (q = 8, 10, 13, 14, off by 1 to 3 in a few bands). The default lane widths are at most 8, so width 9 is not reached today.
  • The BC1 to BC7 block rebuild in Python now dominates decode(). It is the next step.

Tested

AI-assisted (Claude Code).

🤖 Generated with Claude Code

TG0000 and others added 2 commits November 23, 2026 13:50
tools/bcpack_calibration already exports `unpack` (third_party/bcpack-rs).
decode_lanes now tries it first and keeps the Python model for the streams
it refuses, so strict output is unchanged. About 60x faster on the entropy
layer and 12x end to end for BC1 on synthetic chunks; LEONIDA_BCPACK_NATIVE=0
turns it off. tests/test_bcpack_native.py checks the two agree (skipped when
the library is not built).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
third_party/bcpack-rs follows the hardware RTL and has no virtual zero
bytes, so it refused every q = 0 fragment whose omitted trailing zero comes
before a symbol, and those chunks fell back to Python. tools/bcpack_calibration
now exports unpack_model, a line-by-line port of _decode_lanes_raw fed
bcpack.py's own probability tables: it accepts exactly the streams Python
accepts, with the same symbols (8,000 synthetic chunks, sound and corrupt,
single and multi-fragment: 0 disagreements). About 30x on the lane layer and
10x end to end for BC1, q = 0 included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TG0000 TG0000 changed the title bcpack: decode lane symbols with the Rust reader when it is built (~60x entropy, ~12x BC1 end to end) bcpack: Rust port of the lane-array model for the strict path (~30x lane layer, ~10x BC1 end to end) Nov 23, 2026
@kirigayakazima

Copy link
Copy Markdown

Tested 20bcad26786c56d9d5919ea03c069411cb6d1159 (merged cleanly with main 561b65a8) on Windows 11 x64, rustc 1.99.0 (x86_64-pc-windows-msvc), Python 3.14.7: pass (AI-assisted).

Commands & Results (synthetic inputs only, no game files)

  • cargo test --manifest-path tools/bcpack_calibration/Cargo.toml: compiles cleanly (0 warnings, 0 tests in crate).
  • cargo build --release --locked --offline --manifest-path tools/bcpack_calibration/Cargo.toml: builds tools/bcpack_calibration/target/release/bcpack_calibration.dll cleanly.
  • python -I tests/test_bcpack_native.py -v: 4/4 passed (test_native_accepts_exactly_what_python_accepts, test_uniform_model_and_virtual_zeros, test_decode_lanes_falls_back_and_raises_like_python, test_switch_off).
  • python -I tests/test_bcpack.py: 17/17 passed (verified with native DLL active).
  • python -I tests/test_bcpack_history_calib.py: 19/19 passed.
  • python -I tests/test_bcpack_tables.py: 34/34 passed.
  • python -I tests/test_bcpack_tables_bptc.py: 21/21 passed.
  • python -I tests/test_text_write_line_endings.py: 3/3 passed.
  • python -m compileall -q tools/bcpack.py tests/test_bcpack_native.py: exit 0.
  • Extra synthetic parity & benchmark checks: 1,000 randomized streams across all 6 active codecs (including trailing zeros, trailing non-zero bytes, truncation, bitflips) + interleaved multi-fragment (flush=False -> flush=True) parity gave 0 mismatches (585 accepted, 415 refused); 20x512 BC1-block benchmark measured 8.5 ms native vs 233.5 ms Python (~27.5x speedup).

Code Audit Notes (non-blocking)

  • tools/bcpack.py line-by-line parity with tools/bcpack_calibration/src/model.rs checks out, including RANS_L home-state checks, MAX_VIRTUAL_ZEROS, HDR0/HDR1/HDR2 bounds, and trailing non-zero rejection (bcpack.py PROB_TABLES also verified to have 0 zero-frequency entries).
  • Minor hardening suggestions for unpack_model (tools/bcpack_calibration/src/lib.rs:277-300): null-pointer guards on data/widths/tables/output and lanes.checked_mul(target) before slice::from_raw_parts(_mut) would protect direct C-ABI callers (Python callers via _decode_lanes_native already pass valid buffers with nsym > 0). Also _native_unpack's _NATIVE list check could drop duplicate appends under concurrent threads, though both are harmless as written.

@TG0000

TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @kirigayakazima for the thorough Windows + MSVC rustc 1.99 verification and audit! Good note on the C-ABI null-pointer and \checked_mul\ guards in \unpack_model\ — we'll keep that in mind for follow-up hardening (and keep #6029\ clean as-is since all Python callers pass valid non-empty buffers). AI-assisted.

@TG0000

TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded: all of #6029's Rust lane model was squash-merged into \main\ via #6308 (\ 3c10d234). Thanks again @kirigayakazima for testing!

@TG0000 TG0000 closed this Nov 23, 2026
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