From dbca9d5670fe27c8c33527ea6e17bc2eb65200a0 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sun, 1 Nov 2026 19:49:51 -0600 Subject: [PATCH] perf: zstd sequence extra bits and state updates read inline --- src/Rosetta/codecs/compress/Zstd/Decode.luau | 46 +++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Rosetta/codecs/compress/Zstd/Decode.luau b/src/Rosetta/codecs/compress/Zstd/Decode.luau index 4c334d7..afe967f 100644 --- a/src/Rosetta/codecs/compress/Zstd/Decode.luau +++ b/src/Rosetta/codecs/compress/Zstd/Decode.luau @@ -659,14 +659,24 @@ local function block(b: buffer, p: number, e: number, st: any): (string?, string local ml = ML_BASE[mlCode] local nb = ML_BITS[mlCode] if nb > 0 then - ml += peek(b, s, off, nb) - off -= nb + local lo = off - nb + if lo >= 0 and s + rshift(lo, 3) <= fastEnd then + ml += band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1) + else + ml += peek(b, s, off, nb) + end + off = lo end local ll = LL_BASE[llCode] nb = LL_BITS[llCode] if nb > 0 then - ll += peek(b, s, off, nb) - off -= nb + local lo = off - nb + if lo >= 0 and s + rshift(lo, 3) <= fastEnd then + ll += band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1) + else + ll += peek(b, s, off, nb) + end + off = lo end -- repeat offsets local offset @@ -699,15 +709,31 @@ local function block(b: buffer, p: number, e: number, st: any): (string?, string end -- state updates (not after the last sequence): LL, ML, OF if i < nbSeq then + local lo nb = band(rshift(llE, 8), 255) - llS = rshift(llE, 16) + peek(b, s, off, nb) - off -= nb + lo = off - nb + if lo >= 0 and s + rshift(lo, 3) <= fastEnd then + llS = rshift(llE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1) + else + llS = rshift(llE, 16) + peek(b, s, off, nb) + end + off = lo nb = band(rshift(mlE, 8), 255) - mlS = rshift(mlE, 16) + peek(b, s, off, nb) - off -= nb + lo = off - nb + if lo >= 0 and s + rshift(lo, 3) <= fastEnd then + mlS = rshift(mlE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1) + else + mlS = rshift(mlE, 16) + peek(b, s, off, nb) + end + off = lo nb = band(rshift(ofE, 8), 255) - ofS = rshift(ofE, 16) + peek(b, s, off, nb) - off -= nb + lo = off - nb + if lo >= 0 and s + rshift(lo, 3) <= fastEnd then + ofS = rshift(ofE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1) + else + ofS = rshift(ofE, 16) + peek(b, s, off, nb) + end + off = lo end -- execute if lp + ll > litEnd then