From 5b4ee7f937eeb85f500326c228fe2fca93c53c9f Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sun, 1 Nov 2026 19:46:46 -0600 Subject: [PATCH] perf: slice-by-8 MSB-first CRC-32 (Crc32.msb) for Ogg pages and bzip2 blocks; bzip2 checks each block's CRC over its output at once and moves long MTF shifts with table.move --- src/Rosetta/codecs/compress/Bzip2/Decode.luau | 19 ++++---- src/Rosetta/codecs/compress/Bzip2/init.luau | 10 ++--- src/Rosetta/containers/Ogg/Mux.luau | 12 ++---- src/Rosetta/containers/Ogg/Page.luau | 5 +-- src/Rosetta/core/Crc32.luau | 43 ++++++++++++++++++- 5 files changed, 56 insertions(+), 33 deletions(-) diff --git a/src/Rosetta/codecs/compress/Bzip2/Decode.luau b/src/Rosetta/codecs/compress/Bzip2/Decode.luau index 0ad4ab1..1017445 100644 --- a/src/Rosetta/codecs/compress/Bzip2/Decode.luau +++ b/src/Rosetta/codecs/compress/Bzip2/Decode.luau @@ -11,9 +11,6 @@ local bcreate, bcopy, bfill, blen = buffer.create, buffer.copy, buffer.fill, buf local Decode = {} --- CRC-32/BZIP2 table (polynomial 0x04C11DB7, not reflected: core/Crc32) -local CT = Crc32.MSB_TABLE -Decode.crcTable = CT local MASK = table.create(33, 0) for i = 0, 32 do @@ -413,8 +410,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff return nil, "block larger than its declared size", "corrupt" end local v = MTF[idx + 1] - for k = idx, 1, -1 do - MTF[k + 1] = MTF[k] + if idx > 8 then + table.move(MTF, 1, idx, 2) + else + for k = idx, 1, -1 do + MTF[k + 1] = MTF[k] + end end MTF[1] = v local ch = SEQ[v + 1] @@ -447,7 +448,7 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff out = grow(out, op, op + nblock + 4096) end local cap = blen(out) - local crc = 0xFFFFFFFF + local blockStart = op local tPos = rshift(readu32(T, origPtr * 4), 8) local last, run = -1, 0 local i = 0 @@ -468,9 +469,6 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff end bfill(out, op, last, ch) op += ch - for _ = 1, ch do - crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), last) + 1]) - end end run = 0 last = -1 @@ -490,13 +488,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff end writeu8(out, op, ch) op += 1 - crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), ch) + 1]) end end if op > maxBytes then return nil, "output larger than maxBytes", "limit" end - crc = bxor(crc, 0xFFFFFFFF) + local crc = bxor(Crc32.msb(out, blockStart, op - blockStart, 0xFFFFFFFF), 0xFFFFFFFF) if crc ~= storedCrc then return nil, string.format("block CRC mismatch (%08x, expected %08x)", crc, storedCrc), "checksum" end diff --git a/src/Rosetta/codecs/compress/Bzip2/init.luau b/src/Rosetta/codecs/compress/Bzip2/init.luau index 99b2728..4e479d6 100644 --- a/src/Rosetta/codecs/compress/Bzip2/init.luau +++ b/src/Rosetta/codecs/compress/Bzip2/init.luau @@ -3,6 +3,7 @@ -- Written from the format description (no third-party code); randomised blocks (bzip2 0.9.0 and older) are unsupported. local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") +local Crc32 = require("@rosetta/core/Crc32") local Decode = require("@rosetta/codecs/compress/Bzip2/Decode") local Bzip2 = {} @@ -33,14 +34,9 @@ function Bzip2.decompress(buf: buffer | string, offset: number?, length: number? end function Bzip2.crc(buf: buffer, offset: number?, length: number?): number - local CT = Decode.crcTable local o = offset or 0 - local e = o + (length or (buffer.len(buf) - o)) - local c = 0xFFFFFFFF - for i = o, e - 1 do - c = bit32.bxor(bit32.lshift(c, 8), CT[bit32.bxor(bit32.rshift(c, 24), buffer.readu8(buf, i)) + 1]) - end - return bit32.bxor(c, 0xFFFFFFFF) + local n = length or (buffer.len(buf) - o) + return bit32.bxor(Crc32.msb(buf, o, n, 0xFFFFFFFF), 0xFFFFFFFF) end return Bzip2 diff --git a/src/Rosetta/containers/Ogg/Mux.luau b/src/Rosetta/containers/Ogg/Mux.luau index 74a99f9..cbcadad 100644 --- a/src/Rosetta/containers/Ogg/Mux.luau +++ b/src/Rosetta/containers/Ogg/Mux.luau @@ -8,11 +8,9 @@ -- of a multiplexed file come first when every stream's first packet is written (with flush) before any other. -- Never throws: a bad argument returns false, Err. local Err = require("@rosetta/core/Err") -local Page = require("@rosetta/containers/Ogg/Page") +local Crc32 = require("@rosetta/core/Crc32") -local CRC = Page.CRC_TABLE -local readu8, writeu8, writeu32, writei32 = buffer.readu8, buffer.writeu8, buffer.writeu32, buffer.writei32 -local bxor, lshift, rshift = bit32.bxor, bit32.lshift, bit32.rshift +local writeu8, writeu32, writei32 = buffer.writeu8, buffer.writeu32, buffer.writei32 local Mux = {} Mux.__index = Mux @@ -63,11 +61,7 @@ end -- the CRC of a page (the crc field written as zero) local function crc(b: buffer, n: number): number - local c = 0 - for i = 0, n - 1 do - c = bxor(lshift(c, 8), CRC[bxor(rshift(c, 24), readu8(b, i)) + 1]) - end - return c + return Crc32.msb(b, 0, n, 0) end function Stream._flush(self: any, eos: boolean) diff --git a/src/Rosetta/containers/Ogg/Page.luau b/src/Rosetta/containers/Ogg/Page.luau index b35bc11..924a9cc 100644 --- a/src/Rosetta/containers/Ogg/Page.luau +++ b/src/Rosetta/containers/Ogg/Page.luau @@ -32,10 +32,7 @@ local CRC = Crc32.MSB_TABLE Page.CRC_TABLE = CRC local function crcRange(b: buffer, from: number, to: number, crc: number): number - for i = from, to - 1 do - crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, i)) + 1]) - end - return crc + return Crc32.msb(b, from, to - from, crc) end -- the Ogg CRC-32 of arbitrary data (the crc field is not zeroed) diff --git a/src/Rosetta/core/Crc32.luau b/src/Rosetta/core/Crc32.luau index 6dd928b..fb111b8 100644 --- a/src/Rosetta/core/Crc32.luau +++ b/src/Rosetta/core/Crc32.luau @@ -2,7 +2,7 @@ --!native -- core/Crc32: CRC-32 (IEEE 802.3, the zlib / gzip / zip / PNG polynomial 0xEDB88320, reflected), slice-by-8. -- Absorbed from deflate-luau src/Deflate/Checksum.luau (same author; no third-party code). Never throws. -local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift +local band, bxor, rshift, lshift, byteswap = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.byteswap local readu8, readu32 = buffer.readu8, buffer.readu32 local T0, T1, T2, T3 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0) @@ -99,10 +99,49 @@ for n = 0, 255 do end Crc32.MSB_TABLE = MSB +-- slice-by-8 for the MSB-first CRC: M[k][n] = the CRC register after byte n and k zero bytes +local M1, M2, M3 = table.create(256, 0), table.create(256, 0), table.create(256, 0) +local M4, M5, M6, M7 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0) +for n = 0, 255 do + local c = MSB[n + 1] + c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1]) + M1[n + 1] = c + c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1]) + M2[n + 1] = c + c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1]) + M3[n + 1] = c + c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1]) + M4[n + 1] = c + c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1]) + M5[n + 1] = c + c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1]) + M6[n + 1] = c + c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1]) + M7[n + 1] = c +end + function Crc32.msb(buf: buffer, off: number, len: number, init: number?): number local c = init or 0 - for i = off, off + len - 1 do + local i, e = off, off + len + local e8 = e - 8 + while i <= e8 do + c = bxor(c, byteswap(readu32(buf, i))) + local d = byteswap(readu32(buf, i + 4)) + c = bxor( + M7[rshift(c, 24) + 1], + M6[band(rshift(c, 16), 255) + 1], + M5[band(rshift(c, 8), 255) + 1], + M4[band(c, 255) + 1], + M3[rshift(d, 24) + 1], + M2[band(rshift(d, 16), 255) + 1], + M1[band(rshift(d, 8), 255) + 1], + MSB[band(d, 255) + 1] + ) + i += 8 + end + while i < e do c = bxor(lshift(c, 8), MSB[bxor(rshift(c, 24), readu8(buf, i)) + 1]) + i += 1 end return c end