ws-arch2-unix: archive/lz4 (LZ4 frame read: linked / independent blocks, block / content XXH32, content size, skippable + legacy frames, concatenation; write: independent blocks) on codecs/compress/Lz4 (merged from ws/roblox ba4b20e) + codecs/compress/Xxh32; containers/Archive.streamData (shared single-stream payload: buffer / one entry / tar of entries; used by z and lz4); tests vs the lz4 tool both ways; bench lines

containers/Archive: additive helper streamData (requires containers/Tar); nothing existing changes.
This commit is contained in:
4DBug committed 2026-10-31 14:40:26 -05:00
1 parent 197ddc561a
commit 411261470a
10 files changed
+705 -34

No files matched your search

+18
View File
@@ -267,6 +267,24 @@ do
Bzip2.decompress(bz)
end)
end
do
local Lz4F = require("@formats/archive/lz4")
local LF = require("@tests/gen/lz4frame")
local LC = require("@tests/gen/lz4frame_corpora")
local frameData
for _, c in LF.cases do
if c.name == "text/BX" then
frameData = buffer.fromstring(c.data)
end
end
bench("lz4 frame decode (64K, xxh32)", 5, #LC.text, function()
Lz4F.decompress(frameData)
end)
local tb = buffer.fromstring(LC.text)
bench("lz4 frame encode (xxh32)", 5, #LC.text, function()
Lz4F.compress(tb)
end)
end
do
local UnixCompress = require("@formats/codecs/compress/UnixCompress")
local ZC = require("@tests/gen/z")
+21
View File
@@ -0,0 +1,21 @@
-- tools/fixtures/_lz4frame_encode.luau: run by tools/fixtures/lz4frame.py under the luau CLI: writes every corpus of
-- tests/gen/lz4frame_corpora as LZ4 frames with archive/lz4 (several option sets) and prints "<name> <hex>" lines for
-- the lz4 tool to decompress.
local Lz4F = require("@formats/archive/lz4")
local C = require("@tests/gen/lz4frame_corpora")
local sets = {
default = {},
b64k = { blockSize = 65536, blockChecksum = true, contentSize = true },
nocrc = { contentChecksum = false, blockSize = 262144 },
}
for name, data in C do
for sname, o in sets do
local b = assert(Lz4F.compress(data, o :: any))
local t = table.create(buffer.len(b))
for i = 0, buffer.len(b) - 1 do
t[i + 1] = string.format("%02x", buffer.readu8(b, i))
end
print(name .. "/" .. sname .. " " .. table.concat(t))
end
end
+72
View File
@@ -0,0 +1,72 @@
# LZ4 frame fixtures (tools/fixtures.py): corpora compressed by the lz4 tool 1.10 (nixpkgs#lz4) with its frame options
# (default, linked blocks -BD, 64 KB blocks -B4 with and without -BD, block checksums -BX, --content-size,
# --no-frame-crc, -9, the legacy format -l), concatenated frames and a skippable frame; and our writer's frames
# (tools/fixtures/_lz4frame_encode.luau under the luau CLI) decompressed by `lz4 -d`, as verdicts.
# tests/gen/lz4frame.luau { cases = { { name, corpus, data } }, ours = { { name, ok } } }
# tests/gen/lz4frame_corpora.luau { [name] = bytes }
import os, shutil, struct, subprocess, tempfile
INPUTS = ["src/Formats/archive/lz4/init.luau", "src/Formats/codecs/compress/Lz4.luau", "src/Formats/codecs/compress/Xxh32.luau",
"tools/fixtures/_lz4frame_encode.luau"]
def lz4(args, data=None, path=None):
argv = ["nix", "shell", "nixpkgs#lz4", "-c", "lz4"] + args + ([path] if path else [])
return subprocess.run(argv, input=data, capture_output=True, check=True).stdout
def corpora(rng):
base = os.path.dirname(os.__file__)
text = bytearray()
for name in sorted(os.listdir(base)):
if name.endswith(".py"):
with open(os.path.join(base, name), "rb") as f:
text += f.read()
if len(text) >= 160000:
break
return {"empty": b"", "short": b"hello, lz4 frames\n", "text": bytes(text[:160000]), "random": rng.randbytes(20000),
"zeros": b"\0" * 300000}
def luau_cmd():
exe = shutil.which("luau")
return [exe] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau"]
def generate(ctx):
cs = corpora(ctx.rng(43))
cases = []
tmp = tempfile.mkdtemp()
try:
for name, data in cs.items():
path = os.path.join(tmp, name)
with open(path, "wb") as f:
f.write(data)
variants = [("default", ["-c"])]
if name in ("text", "zeros", "short"):
variants += [("BD", ["-c", "-BD"]), ("B4", ["-c", "-B4"]), ("B4BD", ["-c", "-B4", "-BD"]), ("BX", ["-c", "-B4", "-BX"]),
("size", ["-c", "--content-size"]), ("nocrc", ["-c", "--no-frame-crc"]), ("hc9", ["-c", "-9", "-B4", "-BD"]),
("legacy", ["-c", "-l"])]
for vname, args in variants:
cases.append({"name": name + "/" + vname, "corpus": name, "data": lz4(args, path=path)})
finally:
shutil.rmtree(tmp)
# two frames, and a skippable frame in front
two = lz4(["-c"], data=cs["short"]) + lz4(["-c", "-B4"], data=cs["text"])
cases.append({"name": "concat", "corpus": "short+text", "data": two})
skip = struct.pack("<II", 0x184D2A53, 5) + b"skip!" + lz4(["-c"], data=cs["short"])
cases.append({"name": "skippable", "corpus": "short", "data": skip})
ctx.emit("lz4frame_corpora", cs)
enc = subprocess.run(luau_cmd() + [os.path.join(ctx.root, "tools", "fixtures", "_lz4frame_encode.luau")], capture_output=True,
check=True, cwd=ctx.root).stdout.decode()
ours = []
for line in enc.split("\n"):
if line.strip():
name, hexdata = line.split(" ", 1)
try:
got = lz4(["-d", "-c"], data=bytes.fromhex(hexdata.strip()))
ok = got == cs[name.split("/")[0]]
except subprocess.CalledProcessError:
ok = False
ours.append({"name": name, "ok": ok})
ctx.emit("lz4frame", {"cases": cases, "ours": sorted(ours, key=lambda o: o["name"])})