ws-arch: merge ws/arch-lzma
This commit is contained in:
commit
f6ce1da736
16 files changed
+2912
No files matched your search
@@ -0,0 +1,102 @@
|
||||
# 7z fixtures (tools/fixtures.py): a small tree (text, x86 machine code, runs, an empty file, empty and nested
|
||||
# directories, UTF-16 names with accents and a surrogate pair) archived by 7-Zip 2x (nix _7zz) with many method chains:
|
||||
# LZMA / LZMA2 / copy / Deflate, solid and non-solid, BCJ / ARM64 / delta filters, plain and compressed headers; plus
|
||||
# archives whose methods are not supported (BZip2, PPMd, BCJ2, AES) and an empty archive.
|
||||
# tests/gen/7z.luau { files = { { name, dir, data?, mtime } }, cases = { { name, data, unsupported? } } }
|
||||
import os, random, subprocess, sys, tempfile
|
||||
|
||||
TOOL = "_7zz"
|
||||
|
||||
|
||||
def tree(rng):
|
||||
with open(os.path.realpath(sys.executable), "rb") as f:
|
||||
code = f.read(60000)
|
||||
base = os.path.dirname(os.__file__)
|
||||
text = b""
|
||||
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) > 50000:
|
||||
break
|
||||
return [
|
||||
("readme.txt", text[:50000]),
|
||||
("bin/code.bin", code),
|
||||
("bin/runs.dat", b"ab" * 5000 + b"\0" * 9000 + bytes(range(256)) * 8),
|
||||
("bin/random.dat", rng.randbytes(3000)),
|
||||
("empty.txt", b""),
|
||||
("emptydir/", None),
|
||||
("a/b/c/deep.txt", b"deep\n" * 10),
|
||||
("héllo wörld ☃.txt", "unicode ☃\n".encode()),
|
||||
("\U0001d11e clef.txt", b"g clef\n"),
|
||||
]
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = random.Random(77)
|
||||
files = tree(rng)
|
||||
mtime = 1600000000
|
||||
out_files = []
|
||||
cases = []
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
src = os.path.join(d, "src")
|
||||
for name, data in files:
|
||||
p = os.path.join(src, name)
|
||||
if data is None:
|
||||
os.makedirs(p, exist_ok=True)
|
||||
os.utime(p, (mtime, mtime))
|
||||
out_files.append({"name": name.rstrip("/"), "dir": True, "mtime": mtime})
|
||||
else:
|
||||
os.makedirs(os.path.dirname(p), exist_ok=True)
|
||||
with open(p, "wb") as f:
|
||||
f.write(data)
|
||||
os.utime(p, (mtime + 60, mtime + 60))
|
||||
out_files.append({"name": name, "dir": False, "data": data, "mtime": mtime + 60})
|
||||
for sub in ("bin", "a", "a/b", "a/b/c"):
|
||||
os.utime(os.path.join(src, sub), (mtime, mtime))
|
||||
out_files.append({"name": sub, "dir": True, "mtime": mtime})
|
||||
|
||||
def make(name, args, unsupported=None):
|
||||
arc = os.path.join(d, name + ".7z")
|
||||
if os.path.exists(arc):
|
||||
os.remove(arc)
|
||||
subprocess.run(["nix", "shell", "nixpkgs#" + TOOL, "-c", "7zz", "a", "-bd", "-y", arc] + args + ["."],
|
||||
cwd=src, capture_output=True, check=True)
|
||||
with open(arc, "rb") as f:
|
||||
c = {"name": name, "data": f.read()}
|
||||
if unsupported:
|
||||
c["unsupported"] = unsupported
|
||||
cases.append(c)
|
||||
|
||||
make("lzma-nonsolid", ["-m0=LZMA", "-ms=off", "-mhc=off"])
|
||||
make("lzma-solid", ["-m0=LZMA:d=1m", "-ms=on"])
|
||||
make("lzma2-solid", ["-m0=LZMA2", "-ms=on"])
|
||||
make("lzma2-mt-blocks", ["-m0=LZMA2:d=64k:c=20k", "-ms=on", "-mmt=4"])
|
||||
make("lzma2-hc-off", ["-m0=LZMA2", "-mhc=off"])
|
||||
make("copy", ["-m0=Copy", "-mhc=off"])
|
||||
make("copy-hc", ["-m0=Copy"])
|
||||
make("deflate", ["-m0=Deflate", "-ms=on"])
|
||||
make("bcj-lzma", ["-m0=BCJ", "-m1=LZMA", "-ms=on"])
|
||||
make("bcj-lzma2", ["-m0=BCJ", "-m1=LZMA2", "-ms=off"])
|
||||
make("arm64-lzma2", ["-m0=ARM64", "-m1=LZMA2"])
|
||||
make("arm-lzma2", ["-m0=ARM", "-m1=LZMA2"])
|
||||
make("delta-lzma", ["-m0=Delta:4", "-m1=LZMA"])
|
||||
make("ppc-sparc", ["-m0=PPC", "-m1=SPARC", "-m2=LZMA2"])
|
||||
make("lzma-lc-lp", ["-m0=LZMA:lc=0:lp=2:pb=0", "-ms=e"])
|
||||
make("auto", ["-mx=9"])
|
||||
make("fast", ["-mx=1"])
|
||||
make("bzip2", ["-m0=BZip2", "-mhc=off"], unsupported="bzip2")
|
||||
make("ppmd", ["-m0=PPMd", "-mhc=off"], unsupported="ppmd")
|
||||
make("bcj2", ["-m0=BCJ2", "-m1=LZMA", "-m2=LZMA", "-m3=LZMA", "-mb0s0:1", "-mb0s1:2", "-mb0s2:3", "-mhc=off"],
|
||||
unsupported="bcj2")
|
||||
make("aes", ["-m0=LZMA2", "-mhc=off", "-pSecret"], unsupported="aes")
|
||||
# an empty archive
|
||||
arc = os.path.join(d, "empty.7z")
|
||||
empty = os.path.join(d, "emptysrc")
|
||||
os.makedirs(empty)
|
||||
subprocess.run(["nix", "shell", "nixpkgs#" + TOOL, "-c", "7zz", "a", "-bd", "-y", arc, "-x!*", "."],
|
||||
cwd=empty, capture_output=True)
|
||||
if os.path.exists(arc):
|
||||
with open(arc, "rb") as f:
|
||||
cases.append({"name": "empty", "data": f.read(), "empty": True})
|
||||
ctx.emit("7z", {"files": out_files, "cases": cases})
|
||||
@@ -0,0 +1,105 @@
|
||||
# LZMA / LZMA2 / xz fixtures (tools/fixtures.py): deterministic corpora compressed by liblzma (Python's lzma module and
|
||||
# the xz CLI): every preset and -e, lc / lp / pb variants, raw LZMA1 / LZMA2, .lzma files with and without the size
|
||||
# and end marker, xz with every check type, BCJ (x86, PowerPC, ARM, ARM-Thumb, SPARC, ARM64) and delta filters,
|
||||
# multi-block (--block-size), multi-stream and stream padding.
|
||||
# tests/gen/lzma.luau { corpora = { [name] = bytes }, cases = { { name, kind, corpus, data, props?, dict?,
|
||||
# size? } } } kind: "raw" (LZMA1), "raw2" (LZMA2), "alone", "xz"
|
||||
# tests/gen/lzma_bench.luau { size, crc, xz, alone } (a ~4 MB corpus at -6)
|
||||
import lzma, os, random, subprocess, sys, zlib
|
||||
|
||||
|
||||
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])
|
||||
|
||||
|
||||
def binary_corpus(limit):
|
||||
# machine code: a slice of the Python interpreter (x86-64 here; exercises the x86 BCJ filter)
|
||||
with open(os.path.realpath(sys.executable), "rb") as f:
|
||||
data = f.read()
|
||||
if len(data) < limit:
|
||||
lib = os.path.join(os.path.dirname(os.path.realpath(sys.executable)), "..", "lib")
|
||||
for name in sorted(os.listdir(lib)):
|
||||
p = os.path.join(lib, name)
|
||||
if name.startswith("libpython") and os.path.isfile(p):
|
||||
with open(p, "rb") as f:
|
||||
data += f.read(limit)
|
||||
break
|
||||
return data[:limit]
|
||||
|
||||
|
||||
def xz_cli(args, data):
|
||||
return subprocess.run(["xz"] + args + ["-c"], input=data, capture_output=True, check=True).stdout
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = random.Random(7)
|
||||
corpora = {
|
||||
"empty": b"",
|
||||
"one": b"a",
|
||||
"text": text_corpus(150000),
|
||||
"binary": binary_corpus(120000),
|
||||
"random": rng.randbytes(20000),
|
||||
"runs": b"abc" * 3000 + b"\0" * 30000 + bytes(range(256)) * 30 + b"z" * 300,
|
||||
"words": b" ".join(rng.choice([b"lorem", b"ipsum", b"dolor", b"sit", b"amet", b"\xff\xfe", b"0123"]) for _ in range(20000)),
|
||||
}
|
||||
cases = []
|
||||
|
||||
def add(name, kind, corpus, data, **extra):
|
||||
c = {"name": name, "kind": kind, "corpus": corpus, "data": data}
|
||||
c.update(extra)
|
||||
cases.append(c)
|
||||
|
||||
for cname, data in corpora.items():
|
||||
small = cname in ("empty", "one", "random")
|
||||
presets = [0, 6] if small else [0, 1, 3, 6, 9, 9 | lzma.PRESET_EXTREME]
|
||||
for p in presets:
|
||||
add("%s/xz-%d%s" % (cname, p & 15, "e" if p & lzma.PRESET_EXTREME else ""), "xz", cname,
|
||||
lzma.compress(data, lzma.FORMAT_XZ, check=lzma.CHECK_CRC64, preset=p))
|
||||
for chk, cn in ((lzma.CHECK_NONE, "none"), (lzma.CHECK_CRC32, "crc32"), (lzma.CHECK_SHA256, "sha256")):
|
||||
add("%s/xz-check-%s" % (cname, cn), "xz", cname, lzma.compress(data, lzma.FORMAT_XZ, check=chk, preset=2))
|
||||
# .lzma (alone): with a size (Python writes an end marker and no size; the CLI writes the same); raw streams
|
||||
add("%s/alone" % cname, "alone", cname, lzma.compress(data, lzma.FORMAT_ALONE, preset=6))
|
||||
for lc, lp, pb in ((3, 0, 2), (0, 2, 0), (4, 0, 4), (1, 3, 1), (0, 4, 3)):
|
||||
f = [{"id": lzma.FILTER_LZMA1, "preset": 6, "lc": lc, "lp": lp, "pb": pb, "dict_size": 1 << 20}]
|
||||
raw = lzma.compress(data, lzma.FORMAT_RAW, filters=f)
|
||||
add("%s/raw-lc%d-lp%d-pb%d" % (cname, lc, lp, pb), "raw", cname, raw, props=bytes([(pb * 5 + lp) * 9 + lc]), size=len(data))
|
||||
for lc, lp, pb in ((3, 0, 2), (0, 4, 4), (2, 2, 1)):
|
||||
f = [{"id": lzma.FILTER_LZMA2, "preset": 6, "lc": lc, "lp": lp, "pb": pb}]
|
||||
add("%s/raw2-lc%d-lp%d-pb%d" % (cname, lc, lp, pb), "raw2", cname, lzma.compress(data, lzma.FORMAT_RAW, filters=f))
|
||||
if not small:
|
||||
for fid, fname in ((lzma.FILTER_X86, "x86"), (lzma.FILTER_POWERPC, "ppc"), (lzma.FILTER_ARM, "arm"),
|
||||
(lzma.FILTER_ARMTHUMB, "armthumb"), (lzma.FILTER_SPARC, "sparc")):
|
||||
f = [{"id": fid}, {"id": lzma.FILTER_LZMA2, "preset": 3}]
|
||||
add("%s/xz-%s" % (cname, fname), "xz", cname, lzma.compress(data, lzma.FORMAT_XZ, filters=f))
|
||||
f = [{"id": lzma.FILTER_X86, "start_offset": 0x1000}, {"id": lzma.FILTER_LZMA2, "preset": 3}]
|
||||
add("%s/xz-x86-start" % cname, "xz", cname, lzma.compress(data, lzma.FORMAT_XZ, filters=f))
|
||||
for dist in (1, 4, 256):
|
||||
f = [{"id": lzma.FILTER_DELTA, "dist": dist}, {"id": lzma.FILTER_LZMA2, "preset": 3}]
|
||||
add("%s/xz-delta%d" % (cname, dist), "xz", cname, lzma.compress(data, lzma.FORMAT_XZ, filters=f))
|
||||
add("%s/xz-cli-arm64" % cname, "xz", cname, xz_cli(["--arm64", "--lzma2=preset=3"], data))
|
||||
add("%s/xz-cli-blocks" % cname, "xz", cname, xz_cli(["-6", "--block-size=20000"], data))
|
||||
add("%s/xz-cli-x86-delta" % cname, "xz", cname, xz_cli(["--x86", "--delta=dist=2", "--lzma2=preset=1"], data))
|
||||
add("%s/alone-cli" % cname, "alone", cname, xz_cli(["--format=lzma", "-3"], data))
|
||||
# multi-stream + stream padding
|
||||
corpora["multi"] = corpora["text"][:40000] + corpora["runs"] + b"" + corpora["one"]
|
||||
multi = (lzma.compress(corpora["text"][:40000], lzma.FORMAT_XZ, preset=1) + b"\0" * 8 +
|
||||
lzma.compress(corpora["runs"], lzma.FORMAT_XZ, check=lzma.CHECK_CRC32) +
|
||||
lzma.compress(b"", lzma.FORMAT_XZ) + lzma.compress(b"a", lzma.FORMAT_XZ, check=lzma.CHECK_NONE) + b"\0" * 4)
|
||||
add("multi/xz-streams", "xz", "multi", multi)
|
||||
|
||||
ctx.emit("lzma", {"corpora": corpora, "cases": cases})
|
||||
|
||||
# real text and machine code, little long-range repetition
|
||||
bench = text_corpus(2_000_000) + binary_corpus(1_000_000)
|
||||
bench = bench[:3_000_000]
|
||||
ctx.emit("lzma_bench", {"size": len(bench), "crc": zlib.crc32(bench),
|
||||
"xz": lzma.compress(bench, lzma.FORMAT_XZ, preset=6),
|
||||
"alone": lzma.compress(bench, lzma.FORMAT_ALONE, preset=6)})
|
||||
Reference in new issue
Block a user