Skip to content

Decode the mode-2 payloads of pool subgroups - #24

Merged
mahach666 merged 5 commits into
mainfrom
feat/subgroup-lz
Nov 20, 2026
Merged

mahach666 merged 5 commits into
mainfrom
feat/subgroup-lz

Conversation

@mahach666

@mahach666 mahach666 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Release build and tests passed on .NET 8 and .NET 10.
  • Regression tests cover consecutive LZ subgroups, truncated ZeroMask, skipped pools, recovery after a gap, retry after a failed target read, and one decode per pool during a sequential walk.

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.
@mahach666

Copy link
Copy Markdown
Contributor Author

Rebased on the updated #23; all four points are fixed in 29725ca:

  1. Double contribution — right, and thank you for the WXYZ repro: the decoder appends its produced bytes itself, so the caller's AddRange(data) put every decoded subgroup into the history twice, with sizes still matching. The header now goes into the window before the stream decodes, the decoder makes the payload's contribution exactly once, the declared tail of a buffer block is zero-filled, and a failed stream has its partial contribution rolled back before the raw bytes go in. ALaterLzSubgroupMatchesTheDecodedOutputOfAnEarlierOneExactlyOnce asserts the exact bytes of a match into an earlier subgroup's decoded output (it returns [4,0,0,0]-style garbage before the fix). The corpus test over your seven .pln is green; the large-model corpus run is green too (below).
  2. Truncated zero-mask fallback — the condition was dead (the data buffer always has the declared length); the window now takes the raw payload bytes whenever a payload does not fully decode, both for zero-mask and mode-2. ATruncatedZeroMaskSubgroupContributesItsRawBytesToTheWindow pins the distance arithmetic through such a subgroup (with the zero-filled contribution the match returns zeros).
  3. Quadratic walk and blocking — the document now keeps one shared window that reading pools in file order advances, so every pool decodes once; the corpus test over the two large models went from ~9.5 minutes to 19 seconds. A read naming an earlier pool rebuilds the window (documented on ReadSubgroups). A preceding pool that fails to decode is skipped — APoolThatDoesNotDecodeDoesNotBlockLaterPools builds a framing-valid pool whose QuickLZ frame fails and still reads the later raw pool. For the independent path: raw and zero-mask records never consult the window in ReadPool — only mode-2 payloads do.
  4. Changelog — the private-sample size and count are gone; the entry keeps the general result only.

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).
@mahach666

Copy link
Copy Markdown
Contributor Author

Checked — the quadratic walk was the state of the first push (5237771); the rebase push 29725ca already replaced it with the shared window you are describing (review point 6): PlaDocument keeps _poolWindow/_poolWindowThrough, and reading the pools in file order advances it, so ReadSubgroups() decodes the named pool and nothing else — the N(N+1)/2 rebuild never happens on the forward path. FindCompressedPools caching only removes the rescans, as you say; the decode saving comes from the window, not the cache.

Three pieces of evidence, the last one new (49a8a8b):

  • Counting-stream test (ReadingThePoolsInOrderDecodesEachPoolOnce, pushed as 49a8a8b): the document opens over a stream that counts seeks to the pool markers — every pool decode starts by reading its header. A full pass over 8 pools reads each header exactly once: N decodes, not the 36 you measured on the old push.
  • Wall clock on the same two large models: 9m34s on 5237771 (the quadratic version) → 19s on 29725ca. So the share of the 9m34s that your proposal would save is ~99% — it is already saved; what remains is one decode of every pool, which the iterator would do too (the window has to be built either way, including the mode-2 decodes of intermediate pools that later subgroups reference).
  • Raw and zero-mask records never consult the window; only mode-2 payloads do.

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 ReadSubgroups) — worth adding if a real caller needs it, not speculatively. Happy to build it if you see that caller.

@mahach666
mahach666 changed the base branch from refactor/core-architecture to main November 20, 2026 12:45
@mahach666
mahach666 merged commit 1011e77 into main Nov 20, 2026
4 checks passed
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.

1 participant