perf: deflate compressor 1.5x (same output): 4-byte match compares, counted chain walk, hoisted filter bytes, inline hash inserts, symbols emitted through a local 16-bit accumulator
This commit is contained in:
1 parent
3320339507
commit
097a4340b9
1 file changed
+82
-20
@@ -5,7 +5,7 @@
|
||||
-- block the cheapest of stored / fixed / dynamic Huffman, as zlib's levels.
|
||||
local Huffman = require("@rosetta/codecs/entropy/Huffman")
|
||||
|
||||
local readu8, writeu8, writeu16 = buffer.readu8, buffer.writeu8, buffer.writeu16
|
||||
local readu8, readu32, writeu8, writeu16 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu16
|
||||
local bcreate, bcopy, blen = buffer.create, buffer.copy, buffer.len
|
||||
local floor = math.floor
|
||||
|
||||
@@ -136,27 +136,72 @@ local function storedBlock(o: Out, src: buffer, s: number, e: number, final: boo
|
||||
end
|
||||
|
||||
local function emitSymbols(o: Out, L: { number }, D: { number }, nsym: number, lcode: { number }, llen: { number }, dcodes: { number }, dlen: { number })
|
||||
ensure(o, nsym * 6 + 8) -- at most 15 + 5 + 15 + 13 bits a symbol
|
||||
local buf, op, acc, cnt = o.buf, o.op, o.acc, o.cnt
|
||||
-- acc holds cnt < 16 pending bits; each field is at most 15 bits, so acc stays below 2^31
|
||||
for i = 1, nsym do
|
||||
local l = L[i]
|
||||
if l < 256 then
|
||||
put(o, lcode[l + 1], llen[l + 1])
|
||||
acc += lcode[l + 1] * POW[cnt + 1]
|
||||
cnt += llen[l + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
else
|
||||
local len = l - 256
|
||||
local c = LCODE[len]
|
||||
put(o, lcode[c + 1], llen[c + 1])
|
||||
acc += lcode[c + 1] * POW[cnt + 1]
|
||||
cnt += llen[c + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
local xb = LEXTRA[len]
|
||||
if xb > 0 then
|
||||
put(o, len - LBASEOF[len], xb)
|
||||
acc += (len - LBASEOF[len]) * POW[cnt + 1]
|
||||
cnt += xb
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
end
|
||||
local d = D[i]
|
||||
local dc = dcode(d)
|
||||
put(o, dcodes[dc + 1], dlen[dc + 1])
|
||||
local dc = if d <= 256 then DSMALL[d] else DLARGE[(d - 1) // 128 + 1]
|
||||
acc += dcodes[dc + 1] * POW[cnt + 1]
|
||||
cnt += dlen[dc + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
xb = DEXT[dc + 1]
|
||||
if xb > 0 then
|
||||
put(o, d - DBASE[dc + 1], xb)
|
||||
acc += (d - DBASE[dc + 1]) * POW[cnt + 1]
|
||||
cnt += xb
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
while cnt >= 8 do
|
||||
writeu8(buf, op, acc % 256)
|
||||
acc //= 256
|
||||
op += 1
|
||||
cnt -= 8
|
||||
end
|
||||
o.op, o.acc, o.cnt = op, acc, cnt
|
||||
put(o, lcode[257], llen[257]) -- end of block
|
||||
end
|
||||
|
||||
@@ -319,11 +364,6 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local function hash(p: number): number
|
||||
return ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE
|
||||
end
|
||||
local function insert(p: number)
|
||||
local h = hash(p) + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
end
|
||||
local function find(p: number, steps: number): (number, number)
|
||||
local maxLen = math.min(MAXMATCH, e - p)
|
||||
if maxLen < 3 then
|
||||
@@ -332,9 +372,20 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local best, bestDist = 2, 0
|
||||
local c = head[hash(p) + 1]
|
||||
local limit = p - WSIZE
|
||||
while c >= 0 and c > limit and steps > 0 do
|
||||
if readu8(src, c + best) == readu8(src, p + best) and readu8(src, c) == readu8(src, p) then
|
||||
local p0, pb = readu8(src, p), readu8(src, p + 2)
|
||||
local max4 = maxLen - 4
|
||||
if limit < -1 then
|
||||
limit = -1
|
||||
end
|
||||
for _ = 1, steps do
|
||||
if c <= limit then
|
||||
break
|
||||
end
|
||||
if readu8(src, c + best) == pb and readu8(src, c) == p0 then
|
||||
local k = 1
|
||||
while k <= max4 and readu32(src, c + k) == readu32(src, p + k) do
|
||||
k += 4
|
||||
end
|
||||
while k < maxLen and readu8(src, c + k) == readu8(src, p + k) do
|
||||
k += 1
|
||||
end
|
||||
@@ -343,6 +394,7 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
if k >= nice or k >= maxLen then
|
||||
break
|
||||
end
|
||||
pb = readu8(src, p + k)
|
||||
end
|
||||
end
|
||||
local nc = prev[c % WSIZE + 1]
|
||||
@@ -350,7 +402,6 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
break
|
||||
end
|
||||
c = nc
|
||||
steps -= 1
|
||||
end
|
||||
if best < 3 then
|
||||
return 0, 0
|
||||
@@ -369,7 +420,9 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local mlen, mdist = 0, 0
|
||||
if p <= stop3 then
|
||||
mlen, mdist = find(p, maxChain)
|
||||
insert(p)
|
||||
local h = ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
if mlen >= 3 and mlen < lazyMax and p + 1 <= stop3 then
|
||||
local l2, d2 = find(p + 1, if mlen >= good then maxChain // 4 else maxChain)
|
||||
if l2 > mlen then
|
||||
@@ -377,7 +430,9 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
L[nsym] = readu8(src, p)
|
||||
D[nsym] = 0
|
||||
p += 1
|
||||
insert(p)
|
||||
h = ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
mlen, mdist = l2, d2
|
||||
end
|
||||
end
|
||||
@@ -388,9 +443,16 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
D[nsym] = mdist
|
||||
local q = p + 1
|
||||
local qe = math.min(p + mlen, stop3 + 1)
|
||||
while q < qe do
|
||||
insert(q)
|
||||
q += 1
|
||||
if q < qe then
|
||||
local b1, b2 = readu8(src, q), readu8(src, q + 1)
|
||||
while q < qe do
|
||||
local b3 = readu8(src, q + 2)
|
||||
local h = ((b1 * 251 + b2) * 251 + b3) % HSIZE + 1
|
||||
prev[q % WSIZE + 1] = head[h]
|
||||
head[h] = q
|
||||
b1, b2 = b2, b3
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
p += mlen
|
||||
else
|
||||
|
||||
Reference in new issue
Block a user