Repository navigation
Decode the mode-2 payloads of pool subgroups - #24
Conversation
A mode-2 subgroup payload is an LZ token stream like a mode-2 block payload. It may start with a 13-byte pool header - a u32, the 0x47 marker at +4, the packed payload length at +5 (the length including the header) and the unpacked size at +9 - or with the file block's 9-byte stream header; without either, the tokens start at the first byte. The back-references reach into the pool stream: the reconstruction of every preceding pool of the document and of this pool before the record, each subgroup contributing its header and its decoded payload (the raw bytes when a payload does not fully decode, which keeps the distances of the following subgroups plausible). Reading a pool therefore decodes the pools before it in order. A distance is a u16, so the window keeps only its 64 KiB tail: the memory stays constant in the size of the file (a full reconstruction of a 777 MB sample exceeds 2 GiB). FindCompressedPools caches its list after the first scan so the pool objects stay identical between calls. Verified against saved files: every mode-2 subgroup of the corpus decodes, among them 14,446 in a 777 MB office sample and 5,254 in a stacked tower sample.
Review fixes: - The LZ decoder already appends every produced byte to the window, so a decoded subgroup's payload entered the history twice: the header went in, then the decoder's contribution, then the same output again. Sizes still matched, so the corrupt results passed as complete. The header now joins the window before the stream decodes, the decoder makes the payload's contribution, the declared tail of a buffer block is zero-filled, and a stream that fails has its partial contribution rolled back before the raw bytes go in. A test matches a later LZ subgroup against the decoded output of an earlier one and asserts the exact bytes. - A truncated zero-mask literal contributed its zero-filled remainder to the window (the fallback branch was unreachable because the data buffer always has the declared length); the window now takes the raw payload bytes whenever a payload does not fully decode, and a test pins the distance arithmetic through such a subgroup. - Reading pools in file order advances one shared window on the document, so every pool decodes once instead of once per read (the saved-file corpus test over two large models went from minutes to seconds); a read naming an earlier pool rebuilds the window, and a preceding pool that does not decode is skipped instead of blocking later pools. Raw and zero-mask records never consult the window; only mode-2 payloads do. - The changelog no longer names sizes and counts of private samples.
5237771 to
29725ca
Compare
|
Rebased on the updated #23; all four points are fixed in 29725ca:
|
Reading the pools in file order advances the shared window, so every pool decodes once. The new test opens a document over a stream that counts seeks to the pool markers - every pool decode starts by reading the pool header - and asserts that a full pass over eight pools reads each header exactly once (N decodes, not the N(N+1)/2 = 36 of a per-call rebuild).
|
Checked — the quadratic walk was the state of the first push ( Three pieces of evidence, the last one new (
That is also why I would not add a separate full-traversal iterator now: it would duplicate the contract of "read the pools in file order" that the single-read API already carries, with the same one-pass cost. The one thing an iterator could add beyond this is caching subgroup results across repeat or backward reads (both currently rebuild the window, as documented on |
Stacked on #23.
What changes
Mode-2 pool subgroups decode against the reconstructed pool stream. Each subgroup contributes its header and payload once. Truncated ZeroMask records contribute their stored bytes. An unreadable pool or a failed mode-2 decode resets the history, so a reference into a gap returns an empty partial result. Records can still reference known bytes decoded after the gap.
The document reuses the history while pools are read in file order and uses the cached pool index to select a target directly. An unsuccessful target read preserves progress through previously decoded pools.
Verification