Skip to content

perf(bcpack): 128-bit register BitWriter, lazy BPTC state init, index_row LUT, and portable decode test - #1974

Closed
quietfold wants to merge 1 commit into
ProjectLeonida:mainfrom
quietfold:perf/rt-bcpack-decode-fast-bitwriter-and-bptc
Closed

quietfold wants to merge 1 commit into
ProjectLeonida:mainfrom
quietfold:perf/rt-bcpack-decode-fast-bitwriter-and-bptc

Conversation

@quietfold

Copy link
Copy Markdown
Contributor

Claim: RT-BCPACK-DECODE-FAST-BITWRITER-AND-BPTC (claim comment #7168983247 on #1608).

Independent of #1935 (runtime/src/bcpack_stream.h, untouched here); the two optimizations compose cleanly.

Optimizations (runtime/src/bcpack.cpp)

  1. 128-bit register-resident BitWriter for BC6H/BC7:
    • Replaces per-bit while (bits > 0) memory-RMW byte loops (called 82+ times per BC6H block) and per-block fill_n(dest, 16, 0) pre-zeroing with two uint64_t accumulators (lo/hi) and a single put1() helper, flushed in two 64-bit little-endian stores (with portable big-endian fallback).
  2. Eliminate 67 KiB up-front stack zero-fill in decode():
    • Previously every decode() call zero-initialized both array<Endpoints, 256> classic{} (8 KiB) and array<BptcState, 256> bptc{} (59 KiB) on the stack, even when decoding classic codecs or small mipmaps.
    • Now dispatches codec >= 6 vs codec < 6 into separate trivially default-constructible ring buffers without up-front zeroing (both rebuild_classic and rebuild_bptc only read past[(i - d) & 255] when 0 < d <= i, after that slot was written).
  3. Lazy BPTC zero / history fallback and per-block zeroing elimination (rebuild_bptc):
    • Uses compile-time kZeroBc6 and kZeroBc7 constants and only constructs a stack hist_state when eslot >= 0 || islot >= 0, skipping 232 bytes of stack zeroing + BC6 endpoint/index fill on steady-state blocks (i >= 256).
    • Avoids double lookup of bc6Modes/bc7Config, eliminates the 232-byte next = {} wipe per BPTC block by explicitly zeroing only unused BC7 endpoint/secondary-index slots, and uses a 6-byte little-endian memcpy in rebuild_classic.
  4. Precomputed kUnzigEven[256] / kUnzigOdd[256] LUTs for index_row(): replaces the per-pixel bit-extraction and unzig() arithmetic inside the 4–8× per-block index loop.
  5. Portable CPU-only CTest bcpack_decode (runtime/test/bcpack_decode_test.cpp): verifies all 6 codecs (BC1, BC3, BC4, BC5, BC6H, BC7) bit-exactly against dstorage_vectors.py with both fast_entropy = true/false, calibration profile history (missing-profile, confirmed, unconfirmed-predictor, fitted), classic fitted gating, and invalid/misaligned/truncated inputs.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran bcpack_decode_test cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Bit-exact end-to-end A/B benchmark at -O2 vs main (including full rANS entropy decode, best of 5 runs, Apple Silicon):
BC1_64blk_mip    (  512 B): base=    6.3 us  new=   4.5 us  speedup= 1.39x
BC6H_64blk_mip   ( 1024 B): base=   17.9 us  new=  16.2 us  speedup= 1.10x
BC7_64blk_mip    ( 1024 B): base=   18.5 us  new=  14.8 us  speedup= 1.25x
BC1_2048blk      (16384 B): base=  185.9 us  new= 177.6 us  speedup= 1.05x
BC3_2048blk      (32768 B): base=  360.9 us  new= 359.1 us  speedup= 1.01x
BC5_2048blk      (32768 B): base=  359.4 us  new= 356.8 us  speedup= 1.01x
BC6H_2048blk     (32768 B): base=  730.5 us  new= 691.5 us  speedup= 1.06x
BC7_2048blk      (32768 B): base=  694.4 us  new= 607.4 us  speedup= 1.14x
  • Ran python3 -I scripts/check_no_game_data.py on all 3 changed files and python3 -I tests/test_text_write_line_endings.py: all passed.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@aslan2142

Copy link
Copy Markdown
Collaborator

Closing this PR in favor of #2567 (BCPACK-STANDALONE-BC1-BC7).

Side-by-side comparison:

Because #2567 removes the code #1974 optimizes, we are keeping #2567 forward. Thanks @IlyassRS3 for the optimization work.

(AI-assisted review.)

@aslan2142 aslan2142 closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants