Repository navigation
shader_library: 9-bit canonical Huffman LUT and 64-bit BitReader refill (PERF-ZLIB-HUFFMAN-LUT) - #9566
shader_library: 9-bit canonical Huffman LUT and 64-bit BitReader refill (PERF-ZLIB-HUFFMAN-LUT)#9566iliassgm wants to merge 2 commits into
Conversation
…ll (PERF-ZLIB-HUFFMAN-LUT)
|
Tested
( Counts and pass/fail only; no game files. AI-assisted (DeepSeek V4.1 Flash coding agent). |
|
Reviewed
One tiny defensive note (non-blocking): |
|
The new 1..15-bit dynamic-block fixture in huffman_lut_checks() has an incomplete code-length tree. It assigns 17 symbols a length of 5. That fills only 17 of 32 slots. The Windows shader_library test passes, but CPython 3.14.7 zlib rejects the 95-byte fixture with "invalid code lengths set". Both this head (bcb1f88) and parent 6312c05 accept it. The issue is in the new positive test fixture. To reproduce, write w.bytes to fixture.bin immediately before auto bad_adler = w.bytes in section 4, then run: I tested a complete tree. Give symbols 0 through 14 length 4, and symbols 15 and 18 length 5. Emit canonical codes 0 through 14 in four bits, 30 for symbol 15, and 31 for symbol 18 in five bits. Keep the literal/EOB tree and payload unchanged. The revised fixture is 92 bytes. Python decodes it to b'abcdefghijklmno' * 4, and the PR's C++ checks still pass, including the prefix-truncation checks. AI-assisted. |
katherinebytes
left a comment
There was a problem hiding this comment.
Independent CPU review of bcb1f888aa36a6b86a3c61c7e5b9952dfa5c4420 against 5d74c272363f2703265d44e6aaab1a1df8ddfaf3. I inspected the refill/bit-position bookkeeping, canonical lookup and EOF/slow-path checks, and the added synthetic cases. No blocking finding in this focused review; I recommend merge subject to the project's normal checks.
macOS 27.2.1, Apple Silicon, Apple clang++, synthetic inputs, no GPU or game data:
- Compiled
runtime/test/shader_library_test.cppdirectly with C++20,-O2 -Wall -Wextra; the test passed. - Recompiled and ran it with AddressSanitizer and UndefinedBehaviorSanitizer; it passed without a sanitizer report.
- Changed-file no-game-data check and
git diff --checkpassed.
CMake is unavailable here, so this is a direct test build, not a full runtime build. I did not independently benchmark or run the Windows/GPU smoke tests. AI-assisted (Codex). Review claim: #9522 (comment)
|
I checked how the three inflate optimizations compose, starting at All four builds produced byte-identical output to Python zlib-ng 1.3.1 on 171 independently encoded, valid synthetic streams, with the output limit set to the exact decoded length. Each nonempty stream also returned false with the limit set one byte below that length. This includes 96 mixed streams covering all six permutations of stored, fixed-Huffman, and dynamic-Huffman data. I assembled them from raw DEFLATE segments using Focused full-inflate timings (Linux x86-64, Ryzen 7 5825U, GCC 16.2.1
This supports integrating the checksum batching after the Huffman change and retaining the byte-position adjustment when rebasing the bulk-copy patch. It also gives the bulk-copy patch separate coverage for the stored path after Huffman lookahead. These timings characterize the synthetic workloads. The independent corpus does not rely on the code-length fixture discussed above. AI-assisted (Codex). Synthetic inputs only. |
… for standard zlib
|
Thanks @shdann for catching the incomplete code-length tree in section 4 of And thank you @katherinebytes, @DaviTostes, @SweatierKey, and @matheusfermiano-source for the thorough macOS/Linux/ASan/UBSan reviews and the #8682 + #8942 composition benchmarks! AI-assisted: yes |
|
Re-checked the new head
The new head only makes the section-4 dynamic code-length tree complete for standard zlib; no behavior change for the other paths. ( Counts and pass/fail only; no game files. AI-assisted (DeepSeek V4.1 Flash coding agent). |
Claimed in the coordination issue as: PERF-ZLIB-HUFFMAN-LUT.
What and why
leonida::zlib_inflate(runtime/src/shader_library.h) runs on every FSSM shader container extraction (50k+CreatePipelineStatecalls at boot) and on DirectStorage zlib streaming (#7180). Previously,detail::decodepulled one bit at a time throughBitReader::bits(1)(up to 15 iterations per literal/length/distance symbol), andBitReaderrefilled one byte at a time.This PR speeds up Huffman decoding without changing the public API, output bytes, error rejection, or overlapping #8682 (Adler-32 batching) or #8942 (stored/match bulk copies):
detail::BitReader:fill()loads up to 56..63 bits in one little-endian 8-bytememcpy(with a portable byte-loop tail near EOF and on big-endian hosts), keeping bits>= count_zero sopeek()/drop()work directly from the bit buffer.position()returnspos_ - count_ / 8andalign()rewindspos_ -= count_ / 8before clearing the buffer, preserving exact trailer and stored-block offsets.<= 9bits) indetail::Huffman/build_huffman: sizesfast_bits = min(max_len, 9)(so 5-bit distance and small code-length tables touch only 32..128 entries instead of 512) and fills LSB-first reversed slots using astatic constexpr4-bit nibble reversal table (zero function-local dynamic static / TLS-0 guards).detail::decode: 1..9-bit canonical codes resolve in one lookup and drop (checkinglen <= available()at EOF). Codes of length 10..15 and unassigned/empty tables fall through to a register-based canonical scan overpeek()that produces identical symbols and failure states.Benchmarks (Windows 11 x64, Intel Core i5-10400F, MSVC 19.44
/O2, synthetic inputs only)upstream/main)detail::decode(21.48M syms, 2..11-bit codes)zlib_inflateZ_HUFFMAN_ONLY512 KiB (incl. unbatched Adler-32)zlib_inflate4 KiB dynamic (shader-like, 2000 reps)zlib_inflate16 KiB dynamic (500 reps)zlib_inflate1 MiB fixed (20 reps, incl. 3.17 ms unbatched Adler-32)zlib_inflate256 KiB deep 10..15-bit dynamic (50 reps)(When combined with #8682's Adler-32 batching and #8942's bulk match copy, the deflate speedup dominates end-to-end throughput.)
Verification
/std:c++20 /W4 /permissive-, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0): 0 warnings in modified files.runtime/build/shader_library_test.exe: PASS (exit 0), including newhuffman_lut_checks()covering:BitReader::fill()/bits()/position()/align()/byte()across 1..64 bits, all 8 mod-8 alignments, and< 8-byte short buffers;fixed -> stored -> fixedzlib streams hitting all 8 mod-8 bit offsets beforealign()and Adler-32, with every prefix truncation rejected;zlib_inflatedynamic block spanning 1..15-bit codes with every prefix truncation and corrupted Adler-32 rejected.runtime/test/shader_library_test.cppagainstupstream/main'sruntime/src/shader_library.hfails (error C2039: 'fast_bits' / 'kFastBits' / 'fast_mask' / 'fast', exit 2).Z_SYNC_FLUSH/Z_FULL_FLUSH, sizes 0..16 KiB) matched Pythonzlib.decompressbit-for-bit, including exact-limit, limit-1, 1-byte truncation, and corrupted Adler-32 checks.d3d12_draw_test.exeandd3d12_compute_test.exeboth pass (exit 0).python -I tests/test_ci_registration.py: 14/14 OK;python -I scripts/check_no_game_data.py: clean; 0 CRLF bytes.AI-assisted: yes
Checklist
CONTRIBUTING.md), including in logs or reports quoted here.docs/design.md, with how it was confirmed. (Not applicable: no translator change.)