ws-arch2-unix: codecs/compress/UnixCompress (.Z LZW decode + encode, bit-identical to ncompress 5.0: group padding on width changes / CLEAR, ratio-check clears) + archive/z (single-stream, .taz / .tZ -> .tar, writes ncompress's bytes), tests vs ncompress and gzip -d, bench lines
This commit is contained in:
1 parent
8b94584d0f
commit
6eadacce64
10 files changed
+690
-1
No files matched your search
+13
-1
@@ -1,7 +1,7 @@
|
||||
-- Throughput (bash tools/bench.sh): codec MB/s under both runtimes. MB/s are of decompressed output, best of n runs.
|
||||
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus), tools/fixtures/png.py (1024 x 768 images), tools/fixtures/mp3.py,
|
||||
-- tools/fixtures/opus.py (3 s stereo files; MB/s of f32 output, 48 kHz stereo f32 is 0.384 MB/s of real time),
|
||||
-- tools/fixtures/flac.py; wav encodes / decodes a generated tone; zstd / brotli / bzip2 / lzma / lz4 (*_bench corpora), fonts
|
||||
-- tools/fixtures/flac.py; wav encodes / decodes a generated tone; zstd / brotli / bzip2 / lzma / lz4 (*_bench corpora), .Z (tools/fixtures/z.py), fonts
|
||||
-- (glyphs per second), json / xml / yaml, jpeg / gif / tiff / qoi / tga / pnm / hdr and ImageOps.
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
@@ -267,6 +267,18 @@ do
|
||||
Bzip2.decompress(bz)
|
||||
end)
|
||||
end
|
||||
do
|
||||
local UnixCompress = require("@formats/codecs/compress/UnixCompress")
|
||||
local ZC = require("@tests/gen/z")
|
||||
local zc = ZC.cases[5] -- the 700 KB corpus, -b 16
|
||||
local zb, zi = buffer.fromstring(zc.z), buffer.fromstring(zc.input)
|
||||
bench(".Z decompress (lzw -b16)", 5, #zc.input, function()
|
||||
UnixCompress.decompress(zb)
|
||||
end)
|
||||
bench(".Z compress (lzw -b16)", 3, #zc.input, function()
|
||||
UnixCompress.compress(zi)
|
||||
end)
|
||||
end
|
||||
do
|
||||
local Lzma = require("@formats/codecs/compress/Lzma")
|
||||
local LB = require("@tests/gen/lzma_bench")
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# .Z fixtures (tools/fixtures.py): Unix compress files written by ncompress 5.0 (nixpkgs#ncompress, `compress -c -f
|
||||
# -b N`) from a few inputs: empty, one byte, short text, a 700 KB word corpus with incompressible stretches (the 16-bit
|
||||
# table fills, the ratio check emits CLEAR codes), random bytes, and every width 10..16; a .tar.Z for the api's unwrap.
|
||||
# gzip -d reads each file too (its output must be the input: a second decoder agreeing on what ncompress wrote).
|
||||
# tests/gen/z.luau { cases = { { name, input, bits, z } }, tarZ = data, tarNames = { ... } }
|
||||
import io, subprocess, tarfile
|
||||
|
||||
|
||||
def compress(data, bits):
|
||||
return subprocess.run(["nix", "shell", "nixpkgs#ncompress", "-c", "compress", "-c", "-f", "-b", str(bits)], input=data,
|
||||
capture_output=True, check=True).stdout
|
||||
|
||||
|
||||
def corpus(rng, size):
|
||||
words = ["".join(rng.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(rng.randint(2, 9))) for _ in range(3000)]
|
||||
out = bytearray()
|
||||
while len(out) < size:
|
||||
if rng.random() < 0.002:
|
||||
out += rng.randbytes(rng.randint(500, 4000)) # incompressible stretches: the ratio drops, CLEAR follows
|
||||
else:
|
||||
out += (" ".join(rng.choice(words) for _ in range(12)) + ".\n").encode()
|
||||
return bytes(out[:size])
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(23)
|
||||
big = corpus(rng, 700000)
|
||||
inputs = [("empty", b"", 16), ("one", b"A", 16), ("text", b"TOBEORNOTTOBEORTOBEORNOT#" * 40, 16),
|
||||
("random", rng.randbytes(30000), 16), ("corpus16", big, 16)]
|
||||
mid = big[:120000]
|
||||
for bits in range(10, 16): # (-b 9 output does not round-trip through ncompress or gzip: not a valid file)
|
||||
inputs.append(("corpus-b%d" % bits, mid, bits))
|
||||
cases = []
|
||||
for name, data, bits in inputs:
|
||||
z = compress(data, bits)
|
||||
back = subprocess.run(["nix", "shell", "nixpkgs#gzip", "-c", "gzip", "-d", "-c"], input=z, capture_output=True, check=True).stdout
|
||||
assert back == data, "gzip -d disagrees on " + name
|
||||
cases.append({"name": name, "input": data, "bits": bits, "z": z})
|
||||
b = io.BytesIO()
|
||||
with tarfile.open(fileobj=b, mode="w", format=tarfile.PAX_FORMAT) as t:
|
||||
for n, d in (("a.txt", b"alpha\n"), ("b/c.txt", b"gamma " * 100)):
|
||||
ti = tarfile.TarInfo(n)
|
||||
ti.size, ti.mtime = len(d), 1700000000
|
||||
t.addfile(ti, io.BytesIO(d))
|
||||
ctx.emit("z", {"cases": cases, "tarZ": compress(b.getvalue(), 16), "tarNames": ["a.txt", "b/c.txt"]})
|
||||
Reference in new issue
Block a user