runtime: decode short Huffman codes with a lookup table in the zlib decoder #1736
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1743"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-24T18:02:31Z.
Change
src/startup_stubs/inflate.h(the zlib decoder of the DirectStorage stand-in) decoded every Huffman code one bit at a time; its header said "correct but slow, to be replaced by a real decoder if loading time matters". This PR adds a lookup table for codes of up to 10 bits, filled when the code is built. Longer codes, unused table slots and the last byte of input keep the existing bit-at-a-time loop.The lookup peeks at the next input bytes without consuming them and then takes exactly the code's length through the existing
bits(), so bounds checks, the returned statuses (ok,insufficient_output,corrupt_stream,bad_checksum) and the bit position seen by stored blocks and the Adler-32 trailer are unchanged. No public signature changes, andxcrdapi.cppis not touched.This is an independent change; I found no open issue or PR about decoder throughput, and no open PR touches
inflate.hor its tests (checked against open PRs by touched file on 2026-11-24).Verification
Commit
30c332cbon base65e758b0, Windows 11, MSVC 19.44 x64, release (/O2 /Ob2).65e758b0, same filter withctest --preset release: 15 passed, 0 failed, 0 skipped.python -B tests/docs_links_check.pyandpython -B tests/bidi_controls_check.py: pass.Not run: Debug configuration, and any hardware or WARP test (the change has no GPU path).
Decoded MiB/s on 16 MiB synthetic streams, Ryzen 7 9800X3D, before and after measured back to back with the probe's batch mode (runs vary by roughly 20 to 40 %; the zlib column is CPython's zlib 1.3.1 and includes Python call overhead):
The timing driver is a local script and is not part of this PR; the probe's batch mode it drives is.
Evidence and follow-up
New fixture data is generated by the test from fixed seeds with CPython's zlib; no external inputs.
tests/startup_inflate_check.py: 900 seeded random streams (random, repetitive, word and geometric-byte data; levels 0-9; all five zlib strategies; some with a sync or full flush), 92 of them with codes longer than 10 bits (asserted). Each runs at exact capacity, one byte short, truncated, and with one to three bits flipped: 3597 cases. Status, size and bytes must equal the same probe built withX2W_INFLATE_FAST_BITS=0(bit-at-a-time only) and CPython's zlib.X2W_INFLATE_FAULTY_TABLE(codes exactly 10 bits long decode as the next symbol) must differ from the reference; it differs in 265 cases.Local-data observation, not reproducible from this PR: in one local startup run per build, with a local timer around the stand-in's inflate calls (not included here), the same 10,488 streams and 762,010,967 output bytes took 3,957 ms of cumulative decode time before and 2,262 ms after (about 184 and 321 MiB/s). Byte and stream counts were identical in both runs; with one run per build there is no measured run-to-run spread. The public fixture verifies decoding equality and the synthetic throughput, not these numbers.
Things a reviewer may want to weigh:
inflate.hgains two test-only macros (X2W_INFLATE_FAST_BITS,X2W_INFLATE_FAULTY_TABLE) and CMake gains two extra probe targets for the reference and the control.Huffmangrows by 2 KiB per table.Left for follow-up (not measured in detail): match copy, the byte-wise stored-block copy, Adler-32, and refilling the bit buffer several bytes at a time.
Checklist
docs/startup-runtime.md, zlib decoder check)AI-assisted (Claude Code): the change, tests and measurements were produced with an AI agent and the commands listed were executed locally.
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T18:10:19Z.
Review: COMMENTED
Review of head
30c332cb5b(base65e758b0, merge-base with origin/maina4d17cf5). Verdict: no P1, no P2.Read-through of src/startup_stubs/inflate.h
i = reversed + k * 2^lenforlen <= fast_bits, so every index stays below1 << fast_bits. Over-subscribed sets return false inconstructbefore the table is filled. Entries aresymbol << 4 | len(max 287<<4|10), which fits uint16_t and is never 0, so 0 safely means "slow path".fast_bitsare left to the slow path.bits()keepsbit_count < 8between calls (need <= 13),stored()resets it, and the peek requires 2 unread bytes, so at least 16 bits are available for a code of at most 10. The shift is at most 15 (no 32-bit overflow), andbits(s, len)cannot setfailedon the fast path. The last byte of input and the final partial bits still use the slow path.distance > out_pos), the length/capacity checks, the stored-block checks and the Adler-32 check are untouched by the diff.Commands run locally on Linux (g++ 14.2, Python 3.14.8, zlib 1.3.1)
g++ -std=c++17 -O2 -Wall -Wextra -fsanitize=address,undefined -fno-sanitize-recover=undefined -I src tests/startup_inflate_probe.cpp, built three ways (default,-DX2W_INFLATE_FAST_BITS=0,-DX2W_INFLATE_FAULTY_TABLE). The only warnings are the existing-Wmissing-field-initializersones at tests/startup_inflate_probe.cpp:99.python3 -B tests/startup_inflate_check.py <fast> <bitwise> <faulty>with the ASan/UBSan probes passes: 3597 random cases are equal to the bit-at-a-time build and to zlib (92 streams have codes over 10 bits), and the faulty control differs in 265 cases.p_fastandp_fb1/4/9/11/15(table widths 1, 4, 9, 11, 15, all with ASan/UBSan) against the bit-at-a-time probe: 33216 cases (random bytes after a valid zlib header, about 3000 hand-built dynamic headers with random incomplete or over-subscribed litlen/dist length sets, every truncation of 60 valid streams, and 1-4 bit flips). There were 0 mismatches in status, size and bytes, 0 guard-byte writes, and no sanitizer reports. Every OK result also matched CPythonzlib.decompress. A second seed on the default and faulty builds gave 32961 cases: 0 mismatches for the default, 51 for the faulty one.python3 -B tests/docs_links_check.pypasses (17 files, 365 links).python3 -B tests/bidi_controls_check.pypasses (1296 files).git diff --check origin/main...HEADis clean.Local mutations, each reverted with
git checkout -- .(worktree clean afterwards)<< (len - 1 - b)changed to<< b).startup_inflate_check.pyfails ("fixed: exit 1: rejected").bit_count + 7instead of+ 8. It fails with the same assertion.entry & 7instead ofentry & 15bits. It fails with the same assertion.The new random-stream case compares against the same bit-at-a-time build, so the differential check does its job; the fixed-block case catches these three first.
Nits (not blocking)
constructnow zeroes and fills a 1024-entry table even for the 19-symbol code-length alphabet. This is a small constant cost per dynamic block, and the reversal loop could be a table lookup if it ever shows in profiles.startup_inflate_probe_bitwiseand_faultyare added to the default build even where thestartup_inflatetest is not registered (noINTAKE_PYTHON). Guarding them with the same condition would save two compiles.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:22:25Z.
Review: COMMENTED
Review of head
fe375f4315(baseorigin/mainddeaa5eb). Verdict: no P1, no P2. I reviewed an earlier head (30c332cb) of this PR; this is the re-check of the new head.What changed since
30c332cbOne commit,fe375f43, "pin the single 1-bit distance code in the inflate check": a hand-built raw stream with exactly one 1-bit distance code (an incomplete set) in the fast build and the bit-at-a-time build (raw_single_distance,raw_single_distance_bw). That covers the case I called out as not reached by the encoder-made streams.inflate.hitself is unchanged since the earlier review, so the reading of the lookup table (index bounds, unassigned slots, length 0 and over-long codes, the 16-bit peek and the refill at the end of input) stands.Commands run locally on Linux (g++ 14.2, Python 3.14.8, zlib 1.3.1)
g++ -std=c++17 -O2 -fsanitize=address,undefined -fno-sanitize-recover=undefined -I src tests/startup_inflate_probe.cpp, default,-DX2W_INFLATE_FAST_BITS=0and-DX2W_INFLATE_FAULTY_TABLE.python3 -B tests/startup_inflate_check.py <default> <bitwise> <faulty>: passes (10 cases plus 3 controls; 3597 random cases equal to the bit-at-a-time build and to zlib; the faulty table control differs in 265).python3 -B tests/docs_links_check.py,python3 -B tests/bidi_controls_check.py: pass.git diff --check origin/main...HEAD: clean.Local mutations (each reverted, tree clean afterwards)
bit_count + 7: fails.entry & 7bits: fails.Not run: a Windows build and CTest. I have no Windows machine in this environment.
Nit (not blocking) The documented exception (a stream that
inflate.hdecodes past an incomplete code set that zlib rejects) is still a difference from zlib; the new case pins the single 1-bit distance code as a deliberate accepted form and the bit-at-a-time build agrees with it.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-24T19:05:58Z.
fe375f43adds a hand-built dynamic block whose only distance code is 1 bit long; it must decode with both the table and the bit-at-a-time probe, and a construct() that rejects incomplete distance sets fails it. The same commit builds the bitwise and faulty probes only inside if(EXISTS INTAKE_PYTHON), and the 900-stream counts are unchanged.