Files
4DBug 763f4a7c04 ws-cleanup: shared plumbing: Err.from, core/Sniff, Pcm.drain, Bits.msbPut; DUP lint; tools/move.py
- core/Err.from(message, offset?, kind?): the private-submodule -> Err plumbing (a kind, or a 'code: ' prefix /
  'truncated' / 'past the end' in the message) that Brotli, Bzip2, Zstd, Jpeg, WOFF2, FBX, plist, msgpack and
  bmfont each wrote.
- core/Sniff (new): Sniff.text(r, n, trim?) (the start of a text file without its BOM), Sniff.dropBom(s),
  Sniff.magic(r, s); 13 text formats' sniff / decode BOM handling goes through it.
- codecs/Pcm.drain(dec, source?, maxFrames?): a format's decode() through its open(); the collect-the-Decoder
  loops of aac, aiff, au, caf, flac, m4a, mp3, opus, vorbis and wav are one call each.
- core/Bits.msbPut: the MSB-first bit writer the MP3 / MP2 / FLAC encoders each copied.
- tools/lint_rules.py DUP: a function body of 12+ code lines repeated token for token in another module fails.
- tools/move.py: moves a module and rewrites every reference (requires, deps, headers, fixture INPUTS, docs).
- tools/fixtures/zstd.py: the incompressible corpus is smaller (zstd.luau 15.1 -> 13.4 MB; every fixture module is
  now under 14.3 MB).
- tools/bench.luau: MPEG-1 also runs with the api's LuaJIT trace limits; the sections that compare tuned / default
  limits set them explicitly (jitTune), so the order of sections no longer leaks the tuning.
2026-10-31 12:00:59 -05:00

86 lines
4.3 KiB
Python

# Zstd fixtures (tools/fixtures.py): deterministic corpora compressed with the zstd CLI (the reference) at levels
# 1..19 and --ultra -22, --long, small windows (small blocks), with / without the content checksum, with / without the
# content size (stdin), multi-frame and skippable-frame streams, and empty input; plus a ~6 MB bench corpus.
# tests/gen/zstd.luau { corpora = { [name] = bytes }, cases = { { name, corpus, data } } }
# tests/gen/zstd_bench.luau { size, data3, data19, crc }
import os, random, shutil, subprocess, tempfile, zlib
def generate(ctx):
rng = random.Random(7)
def zstd(args, data, from_file=True):
exe = shutil.which("zstd")
if from_file:
with tempfile.TemporaryDirectory() as d:
p = os.path.join(d, "in")
with open(p, "wb") as f:
f.write(data)
argv = ["zstd", "-q", "-c"] + args + [p]
if exe:
return subprocess.run([exe] + argv[1:], capture_output=True, check=True).stdout
return ctx.nix("zstd", argv)
argv = ["zstd", "-q", "-c"] + args
if exe:
return subprocess.run([exe] + argv[1:], input=data, capture_output=True, check=True).stdout
return ctx.nix("zstd", argv, input=data)
def text_corpus(limit):
base = os.path.dirname(os.__file__)
out = bytearray()
for name in sorted(os.listdir(base)):
if name.endswith(".py"):
with open(os.path.join(base, name), "rb") as f:
out += f.read()
if len(out) >= limit:
break
return bytes(out[:limit])
text = text_corpus(400000)
corpora = {
"empty": b"",
"one": b"a",
"short": b"hello, hello, hello zstd world\n",
"text": text[:200000],
"random": rng.randbytes(16000), # incompressible: kept small (each case stores it again)
"runs": b"abc" * 4000 + b"\0" * 20000 + bytes(range(256)) * 40 + b"z" * 300,
"far": (lambda r: r + rng.randbytes(30000) + r)(rng.randbytes(20000)),
"binary": bytes(rng.choice(b"\0\0\0\x01\x02\xff ABC") for _ in range(60000)),
"mixed": bytes(rng.choice(b"aaaaabbbcd\n") for _ in range(30000)) + rng.randbytes(3000) + text[200000:300000],
"big": (text + bytes(rng.choice(b"\0\0\x01\x02\x03xyz") for _ in range(200000))),
}
cases = [] # (name, corpus, data)
for cname, data in corpora.items():
levels = [1, 3, 19] if cname in ("big",) else [1, 2, 3, 5, 7, 9, 12, 15, 17, 19]
for lv in levels:
cases.append(("%s/l%d" % (cname, lv), cname, zstd(["-%d" % lv], data)))
if cname != "big":
cases.append(("%s/ultra22" % cname, cname, zstd(["--ultra", "-22"], data)))
cases.append(("%s/long" % cname, cname, zstd(["--long=24", "-9"], data)))
cases.append(("%s/negative" % cname, cname, zstd(["--fast=3"], data)))
cases.append(("%s/wlog10" % cname, cname, zstd(["--zstd=wlog=10", "-6"], data)))
cases.append(("%s/nocheck" % cname, cname, zstd(["-5", "--no-check"], data)))
cases.append(("%s/stdin" % cname, cname, zstd(["-4", "--check"], data, from_file=False)))
# several frames, a skippable frame between them, and a skippable frame first
t = corpora["text"]
skip = (0x184D2A53).to_bytes(4, "little") + (11).to_bytes(4, "little") + b"skip me now"
multi = zstd(["-3"], t[:50000]) + skip + zstd(["-1", "--no-check"], corpora["runs"]) + zstd(["-3"], b"") + zstd(["-19"], t[50000:90000])
corpora["multi"] = t[:50000] + corpora["runs"] + t[50000:90000]
cases.append(("multi/frames", "multi", multi))
corpora["skipfirst"] = corpora["short"]
cases.append(("skipfirst/frames", "skipfirst", skip + zstd(["-3"], corpora["short"]) + skip))
ctx.emit("zstd", {
"corpora": corpora,
"cases": [{"name": n, "corpus": c, "data": d} for n, c, d in cases],
})
bench = bytearray()
while len(bench) < 6_000_000:
bench += text + bytes(rng.choice(b"\0\0\0\x01\x02\xff ABC") for _ in range(100000)) + rng.randbytes(20000)
bench = bytes(bench[:6_000_000])
ctx.emit("zstd_bench", {"size": len(bench), "crc": zlib.crc32(bench), "data3": zstd(["-3"], bench),
"data19": zstd(["-19"], bench)})