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
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 DISTCAP = Huffman.capacity(32, DISTBITS, 15)
local CLCAP = Huffman.capacity(19, CLBITS, 7)
@@ -35,10 +48,10 @@ do
bfill(l, 144, 9, 112)
bfill(l, 256, 7, 24)
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)
bfill(d, 0, 5, 30)
FDISTS = Huffman.buildLsb(d, 0, 30, FDIST, DISTBITS)
FDISTS = Huffman.buildLsbMap(d, 0, 30, FDIST, DISTBITS, DMAP)
end
local Inflate = {}
@@ -240,8 +253,8 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
if readu8(lens, 256) == 0 then
return nil, "missing end-of-block code"
end
lsub = Huffman.buildLsb(lens, 0, hlit, lt, LITBITS)
dsub = Huffman.buildLsb(lens, hlit, hdist, dt, DISTBITS)
lsub = Huffman.buildLsbMap(lens, 0, hlit, lt, LITBITS, LMAP)
dsub = Huffman.buildLsbMap(lens, hlit, hdist, dt, DISTBITS, DMAP)
if lsub < 0 or dsub < 0 then
return nil, "invalid literal/length or distance code set"
end
@@ -299,13 +312,12 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
end
writeu8(out, op, sym)
op += 1
elseif sym == 256 then
break
else
sym -= 256
if sym > 29 then
return nil, "invalid literal/length code"
elseif sym < 512 then
if sym == 256 then
break
end
return nil, "invalid literal/length code"
else
if bitcnt < 20 then
if pos + 4 <= endpos then
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
local len = LBASE[sym]
local xb = LEXT[sym]
local len = rshift(sym, 3) - 64
local xb = band(sym, 7)
if xb > 0 then
len += band(bitbuf, lshift(1, xb) - 1)
bitbuf = rshift(bitbuf, xb)
@@ -348,12 +360,9 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
end
bitbuf = rshift(bitbuf, l)
bitcnt -= l
local dsym = rshift(e, 5) + 1
if dsym > 30 then
return nil, "invalid distance code"
end
local dist = DBASE[dsym]
xb = DEXT[dsym]
local dsym = rshift(e, 5)
local dist = rshift(dsym, 4)
xb = band(dsym, 15)
if xb > 0 then
if bitcnt < 13 then
if pos + 4 <= endpos then
+7 -2
View File
@@ -38,7 +38,7 @@ end
local count = 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
count[i] = 0
end
@@ -76,7 +76,7 @@ local function build(lens: buffer, off: number, n: number, tab: buffer, P: numbe
if l > 0 then
local c = nextc[l]
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 msb then
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)
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
return build(lens, off, n, tab, P, true)
end
+25 -11
View File
@@ -2,24 +2,38 @@
--!native
-- 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.
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 a0 = adler or 1
local a, b = a0 % 65536, a0 // 65536
local i, e = off, off + len
while i < e do
-- zlib NMAX: the largest n with 255 n (n + 1) / 2 + (n + 1) (BASE - 1) < 2^32
local stop = math.min(e, i + 5552)
while i < stop do
a += readu8(buf, i)
b += a
i += 1
-- 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
-- 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 e88 = e - 88
while i <= e88 do
local pl, ph, ql, qh = 0, 0, 0, 0
for j = i, i + 84, 4 do
local w = readu32(buf, j)
pl += band(w, 0x00FF00FF)
ph += band(rshift(w, 8), 0x00FF00FF)
ql += pl
qh += ph
end
a %= 65521
b %= 65521
i += 88
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
return b * 65536 + a
while i < e do
a += readu8(buf, i)
b += a
i += 1
end
return (b % 65521) * 65536 + a % 65521
end
local function range(buf: buffer, offset: number?, length: number?): (number, number)