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