perf: Adler-32 by 88-byte SWAR blocks (2.4x interpreted); inflate tables carry length / distance base and extra bits; tools/bench_core.luau

This commit is contained in:
4DBug committed 2026-11-01 18:49:31 -06:00
1 parent 01c044805f
commit 158fa3d3f0
4 files changed
+341 -31

No files matched your search

@@ -23,6 +23,19 @@ local DEXT = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
-- RFC 1951 3.2.7: the order the code-length code lengths are sent in -- RFC 1951 3.2.7: the order the code-length code lengths are sent in
local CLORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 } local CLORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }
-- table symbols: literals 0..255, end of block 256, invalid 257..511 (codes 286 / 287), lengths 512 + base * 8 + extra
-- bits; distances base * 16 + extra bits
local LMAP, DMAP = table.create(288, 300), table.create(30, 0)
for i = 0, 256 do
LMAP[i + 1] = i
end
for i = 1, 29 do
LMAP[257 + i] = 512 + LBASE[i] * 8 + LEXT[i]
end
for i = 1, 30 do
DMAP[i] = DBASE[i] * 16 + DEXT[i]
end
local LITCAP = Huffman.capacity(288, LITBITS, 15) local LITCAP = Huffman.capacity(288, LITBITS, 15)
local DISTCAP = Huffman.capacity(32, DISTBITS, 15) local DISTCAP = Huffman.capacity(32, DISTBITS, 15)
local CLCAP = Huffman.capacity(19, CLBITS, 7) local CLCAP = Huffman.capacity(19, CLBITS, 7)
@@ -35,10 +48,10 @@ do
bfill(l, 144, 9, 112) bfill(l, 144, 9, 112)
bfill(l, 256, 7, 24) bfill(l, 256, 7, 24)
bfill(l, 280, 8, 8) bfill(l, 280, 8, 8)
FLITS = Huffman.buildLsb(l, 0, 288, FLIT, LITBITS) FLITS = Huffman.buildLsbMap(l, 0, 288, FLIT, LITBITS, LMAP)
local d = bcreate(30) local d = bcreate(30)
bfill(d, 0, 5, 30) bfill(d, 0, 5, 30)
FDISTS = Huffman.buildLsb(d, 0, 30, FDIST, DISTBITS) FDISTS = Huffman.buildLsbMap(d, 0, 30, FDIST, DISTBITS, DMAP)
end end
local Inflate = {} local Inflate = {}
@@ -240,8 +253,8 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
if readu8(lens, 256) == 0 then if readu8(lens, 256) == 0 then
return nil, "missing end-of-block code" return nil, "missing end-of-block code"
end end
lsub = Huffman.buildLsb(lens, 0, hlit, lt, LITBITS) lsub = Huffman.buildLsbMap(lens, 0, hlit, lt, LITBITS, LMAP)
dsub = Huffman.buildLsb(lens, hlit, hdist, dt, DISTBITS) dsub = Huffman.buildLsbMap(lens, hlit, hdist, dt, DISTBITS, DMAP)
if lsub < 0 or dsub < 0 then if lsub < 0 or dsub < 0 then
return nil, "invalid literal/length or distance code set" return nil, "invalid literal/length or distance code set"
end end
@@ -299,13 +312,12 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
end end
writeu8(out, op, sym) writeu8(out, op, sym)
op += 1 op += 1
elseif sym == 256 then elseif sym < 512 then
break if sym == 256 then
else break
sym -= 256
if sym > 29 then
return nil, "invalid literal/length code"
end end
return nil, "invalid literal/length code"
else
if bitcnt < 20 then if bitcnt < 20 then
if pos + 4 <= endpos then if pos + 4 <= endpos then
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt)) bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
@@ -327,8 +339,8 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
end end
end end
end end
local len = LBASE[sym] local len = rshift(sym, 3) - 64
local xb = LEXT[sym] local xb = band(sym, 7)
if xb > 0 then if xb > 0 then
len += band(bitbuf, lshift(1, xb) - 1) len += band(bitbuf, lshift(1, xb) - 1)
bitbuf = rshift(bitbuf, xb) bitbuf = rshift(bitbuf, xb)
@@ -348,12 +360,9 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
end end
bitbuf = rshift(bitbuf, l) bitbuf = rshift(bitbuf, l)
bitcnt -= l bitcnt -= l
local dsym = rshift(e, 5) + 1 local dsym = rshift(e, 5)
if dsym > 30 then local dist = rshift(dsym, 4)
return nil, "invalid distance code" xb = band(dsym, 15)
end
local dist = DBASE[dsym]
xb = DEXT[dsym]
if xb > 0 then if xb > 0 then
if bitcnt < 13 then if bitcnt < 13 then
if pos + 4 <= endpos then if pos + 4 <= endpos then
+7 -2
View File
@@ -38,7 +38,7 @@ end
local count = table.create(MAXLEN + 1, 0) local count = table.create(MAXLEN + 1, 0)
local nextc = table.create(MAXLEN + 1, 0) local nextc = table.create(MAXLEN + 1, 0)
local function build(lens: buffer, off: number, n: number, tab: buffer, P: number, msb: boolean): number local function build(lens: buffer, off: number, n: number, tab: buffer, P: number, msb: boolean, map: { number }?): number
for i = 1, MAXLEN + 1 do for i = 1, MAXLEN + 1 do
count[i] = 0 count[i] = 0
end end
@@ -76,7 +76,7 @@ local function build(lens: buffer, off: number, n: number, tab: buffer, P: numbe
if l > 0 then if l > 0 then
local c = nextc[l] local c = nextc[l]
nextc[l] = c + 1 nextc[l] = c + 1
local entry = s * 32 + l local entry = (if map then map[s + 1] else s) * 32 + l
if l <= P then if l <= P then
if msb then if msb then
local first = lshift(c, P - l) local first = lshift(c, P - l)
@@ -136,6 +136,11 @@ function Huffman.buildLsb(lens: buffer, off: number, n: number, tab: buffer, P:
return build(lens, off, n, tab, P, false) return build(lens, off, n, tab, P, false)
end end
-- map[s + 1] replaces symbol s in the entries (values < 2^27)
function Huffman.buildLsbMap(lens: buffer, off: number, n: number, tab: buffer, P: number, map: { number }): number
return build(lens, off, n, tab, P, false, map)
end
function Huffman.buildMsb(lens: buffer, off: number, n: number, tab: buffer, P: number): number function Huffman.buildMsb(lens: buffer, off: number, n: number, tab: buffer, P: number): number
return build(lens, off, n, tab, P, true) return build(lens, off, n, tab, P, true)
end end
+25 -11
View File
@@ -2,24 +2,38 @@
--!native --!native
-- core/Adler32: Adler-32 (RFC 1950, the zlib checksum). Absorbed from deflate-luau src/Deflate/Checksum.luau (same author; -- core/Adler32: Adler-32 (RFC 1950, the zlib checksum). Absorbed from deflate-luau src/Deflate/Checksum.luau (same author;
-- no third-party code). Never throws: the range is clamped. -- no third-party code). Never throws: the range is clamped.
local readu8 = buffer.readu8 local readu8, readu32 = buffer.readu8, buffer.readu32
local band, rshift = bit32.band, bit32.rshift
local function adler32(buf: buffer, off: number, len: number, adler: number?): number local function adler32(buf: buffer, off: number, len: number, adler: number?): number
local a0 = adler or 1 local a0 = adler or 1
local a, b = a0 % 65536, a0 // 65536 local a, b = a0 % 65536, a0 // 65536
local i, e = off, off + len local i, e = off, off + len
while i < e do -- 88-byte blocks, two bytes per 16-bit lane of each u32: pl / ph sum bytes 0, 2 / 1, 3 of the words, ql / qh sum
-- zlib NMAX: the largest n with 255 n (n + 1) / 2 + (n + 1) (BASE - 1) < 2^32 -- those prefix sums (each lane stays below 2^16 over 22 words). Byte t of word j (of m) weighs 4 (m - j) - t in b.
local stop = math.min(e, i + 5552) local e88 = e - 88
while i < stop do while i <= e88 do
a += readu8(buf, i) local pl, ph, ql, qh = 0, 0, 0, 0
b += a for j = i, i + 84, 4 do
i += 1 local w = readu32(buf, j)
pl += band(w, 0x00FF00FF)
ph += band(rshift(w, 8), 0x00FF00FF)
ql += pl
qh += ph
end end
a %= 65521 i += 88
b %= 65521 local s0, s2 = pl % 65536, pl // 65536
local s1, s3 = ph % 65536, ph // 65536
local q = ql % 65536 + ql // 65536 + qh % 65536 + qh // 65536
b = (b + 88 * a + 4 * q - s1 - 2 * s2 - 3 * s3) % 65521
a = (a + s0 + s1 + s2 + s3) % 65521
end end
return b * 65536 + a while i < e do
a += readu8(buf, i)
b += a
i += 1
end
return (b % 65521) * 65536 + a % 65521
end end
local function range(buf: buffer, offset: number?, length: number?): (number, number) local function range(buf: buffer, offset: number?, length: number?): (number, number)
+282
View File
@@ -0,0 +1,282 @@
-- Throughput of the core codecs (bash tools/bench.sh core): inflate / deflate, checksums, rbxm / rbxmx, json / xml,
-- F.detect, gltf / obj and the other compressors. MB/s are of the decoded side, best of n runs.
local Deflate = require("@rosetta/codecs/compress/Deflate")
local Crc32 = require("@rosetta/core/Crc32")
local Adler32 = require("@rosetta/core/Adler32")
local Base64 = require("@rosetta/core/Base64")
local Md5 = require("@rosetta/core/Md5")
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
local B = require("@tests/gen/deflate_bench")
local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
local filter: string? = (...) :: any
local function want(labels: string): boolean
return filter == nil or string.find(labels, filter :: string, 1, true) ~= nil
end
local function bench(label: string, n: number, bytes: number, f: () -> ())
if filter and not string.find(label, filter, 1, true) then
return 0
end
f()
local dt, spent, i = math.huge, 0, 0
while i < n or (spent < 1.5 and i < 40) do -- best of n, and of at least 1.5 s: the machine is shared
local t0 = os.clock()
f()
local d = os.clock() - t0
dt, spent, i = math.min(dt, d), spent + d, i + 1
end
print(string.format("%-8s %-36s %8.2f ms %7.1f MB/s", runtime, label, dt * 1000, bytes / dt / 1e6))
return dt
end
local z = buffer.fromstring(B.zlib)
local g = buffer.fromstring(B.gzip)
local out = assert(Deflate.zlib(z))
assert(buffer.len(out) == B.size and Crc32.of(out) == B.crc, "bench corpus mismatch")
bench("inflate (growing output)", 5, B.size, function()
Deflate.inflate(z, 2, buffer.len(z) - 6)
end)
bench("inflate (exact size)", 5, B.size, function()
Deflate.inflate(z, 2, buffer.len(z) - 6, B.size)
end)
bench("zlib (+ adler32)", 5, B.size, function()
Deflate.zlib(z)
end)
bench("gunzip (+ crc32)", 5, B.size, function()
Deflate.gunzip(g)
end)
bench("crc32", 10, B.size, function()
Crc32.of(out)
end)
bench("adler32", 10, B.size, function()
Adler32.of(out)
end)
if want("compress -6 text compress -1 text compress -6 f32 inflate f32") then
local part = buffer.create(1500000)
buffer.copy(part, 0, out, 0, 1500000)
local c6 = assert(Deflate.compress(part, { level = 6 }))
bench("compress -6 text (1.5 MB in)", 2, 1500000, function()
Deflate.compress(part, { level = 6 })
end)
bench("compress -1 text (1.5 MB in)", 2, 1500000, function()
Deflate.compress(part, { level = 1 })
end)
-- f32 vertex data: a smooth surface sampled on a grid (positions, normals, uvs), as glTF / mesh writers emit
local N = 180
local fl = buffer.create(N * N * 32)
for y = 0, N - 1 do
for x = 0, N - 1 do
local o = (y * N + x) * 32
local h = math.sin(x * 0.07) * math.cos(y * 0.05) * 3
buffer.writef32(fl, o, x * 0.25)
buffer.writef32(fl, o + 4, h)
buffer.writef32(fl, o + 8, y * 0.25)
buffer.writef32(fl, o + 12, -math.cos(x * 0.07) * 0.2)
buffer.writef32(fl, o + 16, 0.97)
buffer.writef32(fl, o + 20, math.sin(y * 0.05) * 0.15)
buffer.writef32(fl, o + 24, x / N)
buffer.writef32(fl, o + 28, y / N)
end
end
local cf = assert(Deflate.compress(fl, { level = 6 }))
bench("compress -6 f32 (" .. buffer.len(fl) // 1000 .. " KB in)", 2, buffer.len(fl), function()
Deflate.compress(fl, { level = 6 })
end)
bench("inflate f32", 5, buffer.len(fl), function()
Deflate.inflate(cf, 0, buffer.len(cf))
end)
if not filter then
print(string.format("%-8s compress -6 sizes: text %d (zlib -6 ref %s), f32 %d", runtime, buffer.len(c6), tostring((B :: any).z6 or "?"), buffer.len(cf)))
end
end
if want("base64 encode base64 decode md5") then
local s = buffer.create(3000000)
buffer.copy(s, 0, out, 0, 3000000)
local enc = Base64.encode(s)
bench("base64 encode", 5, 3000000, function()
Base64.encode(s)
end)
bench("base64 decode", 5, 3000000, function()
Base64.decode(enc)
end)
bench("md5", 5, 3000000, function()
Md5.of(s)
end)
end
if want("rbxm decode rbxm encode rbxmx decode rbxmx encode") then
local Rbx = require("@rosetta/codecs/model/Rbx")
local Rbxm = require("@rosetta/model/rbxm")
local Rbxmx = require("@rosetta/model/rbxmx")
local tree = Rbx.tree()
local folder = Rbx.add(tree, Rbx.instance("Folder", "Parts"))
for i = 1, 20000 do
local p = Rbx.add(folder, Rbx.instance("Part", "P" .. i))
Rbx.set(p, "CFrame", "CFrame", { i, i % 7, -i, 1, 0, 0, 0, 1, 0, 0, 0, 1 })
Rbx.set(p, "size", "Vector3", { 4, 1, 2 + i % 3 })
Rbx.set(p, "Color3uint8", "Color3uint8", { i % 256, 128, 255 - i % 256 })
Rbx.set(p, "Anchored", "Bool", i % 2 == 0)
Rbx.set(p, "Transparency", "Float32", (i % 4) / 4)
Rbx.set(p, "Material", "Enum", 256)
end
local bin = Rbxm.encode(tree) :: buffer
local xml = Rbxmx.encode(tree) :: buffer
bench("rbxm decode (20k parts)", 3, buffer.len(bin), function()
Rbxm.decode(Bytes.reader(bin))
end)
bench("rbxm encode (20k parts)", 3, buffer.len(bin), function()
Rbxm.encode(tree)
end)
bench("rbxmx decode (20k parts)", 3, buffer.len(xml), function()
Rbxmx.decode(Bytes.reader(xml))
end)
bench("rbxmx encode (20k parts)", 3, buffer.len(xml), function()
Rbxmx.encode(tree)
end)
end
if want("json decode (indented) json decode (compact) json encode") then
local Json = require("@rosetta/codecs/text/Json")
local JB = require("@tests/gen/json_bench")
local v = assert(Json.decode(JB.text))
bench("json decode (indented)", 5, #JB.text, function()
Json.decode(JB.text)
end)
bench("json decode (compact)", 5, #JB.compact, function()
Json.decode(JB.compact)
end)
bench("json encode (compact, sorted)", 5, #JB.compact, function()
Json.encode(v)
end)
end
if want("xml decode (DOM) xml events xml encode") then
local Xml = require("@rosetta/codecs/text/Xml")
local XB = require("@tests/gen/xml_bench")
local doc = assert(Xml.decode(XB.text))
bench("xml decode (DOM)", 5, #XB.text, function()
Xml.decode(XB.text)
end)
bench("xml events", 5, #XB.text, function()
for _ in Xml.events(XB.text) do
end
end)
bench("xml encode", 5, #XB.text, function()
Xml.encode(doc)
end)
end
if want("F.detect png F.detect unknown") then
-- F.detect: content sniffing over every registered format (MB/s here = thousands of detects per second)
local F = require("@rosetta")
local PB = require("@tests/gen/png_bench")
local png = buffer.fromstring(PB[1].data)
local junk = buffer.create(4096)
for i = 0, 4095 do
buffer.writeu8(junk, i, (i * 7919) % 251)
end
local t0 = os.clock()
F.detect(junk)
if not filter then
print(string.format("%-8s %-36s %8.2f ms", runtime, "F.detect first call (loads modules)", (os.clock() - t0) * 1000))
end
bench("F.detect png (k/s)", 5, 1000, function()
for _ = 1, 100 do
F.detect(png)
end
end)
bench("F.detect png + name (k/s)", 5, 1000, function()
for _ = 1, 100 do
F.detect(png, { name = "a.png" })
end
end)
bench("F.detect unknown (k/s)", 5, 1000, function()
for _ = 1, 100 do
F.detect(junk)
end
end)
end
if want("meshops computeNormals gltf glb decode gltf glb encode gltf json+base64 decode obj decode obj encode") then
local MeshOps = require("@rosetta/ops/MeshOps")
local Gltf = require("@rosetta/model/gltf")
local Obj = require("@rosetta/model/obj")
local N = 400
local pos = Types.Scene.accessor(nil, "f32", 3, N * N)
local uv = Types.Scene.accessor(nil, "f32", 2, N * N)
for y = 0, N - 1 do
for x = 0, N - 1 do
local i = y * N + x
buffer.writef32(pos.data, i * 12, x / N)
buffer.writef32(pos.data, i * 12 + 4, math.sin(x * 0.1) * math.cos(y * 0.1) * 0.05)
buffer.writef32(pos.data, i * 12 + 8, y / N)
buffer.writef32(uv.data, i * 8, x / N)
buffer.writef32(uv.data, i * 8 + 4, y / N)
end
end
local ind = Types.Scene.accessor(nil, "u32", 1, (N - 1) * (N - 1) * 6)
local k = 0
for y = 0, N - 2 do
for x = 0, N - 2 do
local a = y * N + x
for _, v in { a, a + N, a + 1, a + 1, a + N, a + N + 1 } do
buffer.writeu32(ind.data, k * 4, v)
k += 1
end
end
end
local prim = { attributes = { POSITION = pos, TEXCOORD_0 = uv }, indices = ind, mode = "triangles" } :: any
MeshOps.computeNormals(prim)
local scene = Types.Scene.fromPrimitive(prim, "grid")
bench("meshops computeNormals 318k tris", 3, buffer.len(pos.data), function()
MeshOps.computeNormals(prim)
end)
local glb = assert(Gltf.encode(scene, { binary = true }))
bench("gltf glb decode", 5, buffer.len(glb), function()
Gltf.decode(Bytes.reader(glb))
end)
bench("gltf glb encode", 3, buffer.len(glb), function()
Gltf.encode(scene, { binary = true })
end)
local gltf = assert(Gltf.encode(scene, { binary = false }))
bench("gltf json+base64 decode", 3, buffer.len(gltf), function()
Gltf.decode(Bytes.reader(gltf))
end)
local obj = assert(Obj.encode(scene))
bench("obj decode", 2, buffer.len(obj), function()
Obj.decode(Bytes.reader(obj))
end)
bench("obj encode", 2, buffer.len(obj), function()
Obj.encode(scene)
end)
end
if want("zstd -3 lz4 block brotli bzip2 xz -6") then
local Zstd = require("@rosetta/codecs/compress/Zstd")
local ZB = require("@tests/gen/zstd_bench")
local z3 = buffer.fromstring(ZB.data3)
bench("zstd -3 (no checksum)", 3, ZB.size, function()
Zstd.decompress(z3, nil, nil, { verify = false })
end)
local Lz4 = require("@rosetta/codecs/compress/Lz4")
local LB = require("@tests/gen/lz4_bench")
local lz = buffer.fromstring(LB.data)
bench("lz4 block decompress (hc9)", 5, LB.size, function()
Lz4.decompress(lz, nil, nil, LB.size)
end)
local Brotli = require("@rosetta/codecs/compress/Brotli")
local BB = require("@tests/gen/brotli_bench")
local bz = buffer.fromstring(BB.data)
bench("brotli decompress (q9 w22)", 3, BB.size, function()
Brotli.decompress(bz)
end)
local Bzip2 = require("@rosetta/codecs/compress/Bzip2")
local BZ = require("@tests/gen/bzip2_bench")
local b2 = buffer.fromstring(BZ.data)
bench("bzip2 decompress -9", 3, BZ.size, function()
Bzip2.decompress(b2)
end)
local Lzma = require("@rosetta/codecs/compress/Lzma")
local XB = require("@tests/gen/lzma_bench")
local lx = buffer.fromstring(XB.xz)
bench("xz -6 (lzma2 + crc64)", 3, XB.size, function()
Lzma.xz(lx)
end)
end