compression: Snappy and LZ4 block formats in std/compression (step 1 of #121) #432
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
Context
Step 1 of #121 (FIRST-PARTY.md gap 26: zstd, snappy and lz4 for
std/compression). The two byte-oriented LZ77 block formats come first: they need no checksum and no new package, and they are what MongoDB's wire compression (snappy) and Kafka (snappy, lz4) build on. Frames, checksums and zstd are the later steps listed in #121.Design
Ordinary TorbScript over
Bytesinstd/compression, like DEFLATE: no native, no syntax change, no new driver flag, no std name the compiler looks up by string. Touches the seed: no. Implemented from the public format descriptions (Snappyformat_description.txt, LZ4'slz4_Block_format.md); no reference code is copied or linked, so the BSD-licensed reference implementations impose nothing on the repository.std/compression/src/snappy.trbandstd/compression/src/lz4.trb; the operations are methods ofBytesdeclared in theextend Bytesblock ofstd/compression/src/lib.trb, imported by the name of the list they extend (use Buffer.snappyCompressed from "std/compression"), asgzippedis. Failures are the existingCompressionError(reason, byte offset).fn snappyCompressed(): Byteswrites the raw Snappy block format (preamble varint of the uncompressed length, literal and copy elements, 1-, 2- and 4-byte-offset copies as the format allows); greedy hash-table match search, deterministic (same bytes, same output on every machine).fn snappyDecompressed(): Result<Bytes, CompressionError>reads every valid block; fails on a truncated preamble or element, a copy that reaches before the start of the output, and an output length different from the preamble's. A preamble that declares more than the input could ever produce must not make it allocate that much up front (a 5-byte input declaring 4 GiB fails fast).fn lz4BlockCompressed(): Byteswrites one LZ4 block (sequences of token, literals, 2-byte little-endian offset, match length; the last sequence literals only; the end-of-block parsing restrictions of the format: last match starts at least 12 bytes before the end, last 5 bytes literals); greedy, deterministic.fn lz4BlockDecompressed(maximumSize: Int): Result<Bytes, CompressionError>: an LZ4 block does not store its decoded size, so the caller names the largest output it accepts; fails when the output would exceed it, on a truncated sequence, and on an offset of 0 or beyond the start of the output. Overlapping copies (offset smaller than length) repeat bytes as the formats define.deflated/inflatedandgzipped/gunzipped(participle methods ofBytes, #146); "Block" is in the LZ4 names because step 2 adds the frame format beside it.docs/design/FIRST-PARTY.mdgap 26 becomes "partly done" (snappy block and lz4 block; the rest named). Add a time-bounded round trip of each codec tostd/compression/testsonly if it stays well under the existing suite's time; no change tobenchmarks/required.Acceptance criteria
Bytes.snappyCompressed/snappyDecompressedandlz4BlockCompressed/lz4BlockDecompressedexist instd/compression, as methods importable withuse Buffer.<name> from "std/compression".Fail(CompressionError)with an offset, never a panic: truncated stream, copy before the start, offset 0 (LZ4), declared length mismatch (Snappy), output overmaximumSize(LZ4), a Snappy preamble declaring 4 GiB over a tiny input (no large allocation).std/compressiondoc comments updated (summary names all five formats),skills/regenerated,torb format --checkclean on touched files, FIRST-PARTY.md gap 26 marked partly done.std/, but the codecs run in both back ends) not required unless the round touchescompiler/src/ir,compiler/src/backendorruntime/.Dependencies
None. Blocks the later steps of #121 only in the sense that they extend the same files.
Commits
One or two Conventional Commits:
feat(std): snappy and lz4 blocks in std/compressionand, if the regenerated skills and design record are committed separately,docs(std): ....[loop:torbscript] claim implement by session:loop-torbscript-20261023 until 2026-10-23T20:32Z
[loop:torbscript] implement: snappy and lz4 block codecs in std/compression with tests, docs and regenerated skills; one docs-declaration round fixed after the first CI run, PR #433, CI success (tier A green), next: review. Tokens: not metered inside this worker run (long run, one context compaction).