perf: base64 2.3x: whole 4-character groups decoded from one u32, encode through a 12-bit pair table

This commit is contained in:
4DBug committed 2026-11-01 19:22:29 -06:00
1 parent 097a4340b9
commit 3a4279dd1f
1 file changed
+53 -3
+53 -3
View File
@@ -29,7 +29,18 @@ for _, c in { 9, 10, 11, 12, 13, 32 } do
end
buffer.writeu8(DEC, string.byte("="), 65)
local readu8, writeu8 = buffer.readu8, buffer.writeu8
local readu8, writeu8, readu16, readu32, writeu16 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.readu32, buffer.writeu16
local band, rshift, byteswap = bit32.band, bit32.rshift, bit32.byteswap
-- two output characters per 12 input bits (u16, first character in the low byte)
local function pairs12(alpha: string): buffer
local t = buffer.create(8192)
for i = 0, 4095 do
buffer.writeu16(t, i * 2, string.byte(alpha, i // 64 + 1) + string.byte(alpha, i % 64 + 1) * 256)
end
return t
end
local ENC2, ENC2_URL = pairs12(ALPHA), pairs12(ALPHA_URL)
function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: number?, length: number?): string
local buf: buffer = if type(data) == "string" then buffer.fromstring(data) else data
@@ -47,6 +58,24 @@ function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: numb
local out = buffer.create(textLen + breaks)
local p = 0
local col = 0
if wrap == 0 then
local enc2 = if opts and opts.url then ENC2_URL else ENC2
local fast = if o + full * 3 + 1 <= total then full else full - 1 -- readu32 reads one byte past the group
for i = 0, fast - 1 do
local v = rshift(byteswap(readu32(buf, o + i * 3)), 8)
writeu16(out, p, readu16(enc2, rshift(v, 12) * 2))
writeu16(out, p + 2, readu16(enc2, band(v, 4095) * 2))
p += 4
end
for i = math.max(fast, 0), full - 1 do
local q = o + i * 3
local v = readu8(buf, q) * 65536 + readu8(buf, q + 1) * 256 + readu8(buf, q + 2)
writeu16(out, p, readu16(enc2, rshift(v, 12) * 2))
writeu16(out, p + 2, readu16(enc2, band(v, 4095) * 2))
p += 4
end
full = 0
end
for i = 0, full - 1 do
if wrap > 0 then
if col == wrap then
@@ -69,7 +98,7 @@ function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: numb
writeu8(out, p, 10)
p += 1
end
local q = o + full * 3
local q = o + (n - rest)
local v = readu8(buf, q) * 65536 + (if rest == 2 then readu8(buf, q + 1) * 256 else 0)
writeu8(out, p, readu8(enc, v // 262144))
writeu8(out, p + 1, readu8(enc, (v // 4096) % 64))
@@ -91,7 +120,27 @@ function Base64.decode(s: string | buffer): (buffer?, string?)
local out = buffer.create((n // 4 + 1) * 3)
local acc, bits, p = 0, 0, 0
local padded = false
for i = 0, n - 1 do
local i = 0
while i < n do
if bits == 0 and not padded then
-- whole groups of four alphabet characters
while i + 4 <= n do
local w = readu32(src, i)
local a, b = readu8(DEC, band(w, 255)), readu8(DEC, band(rshift(w, 8), 255))
local c, d = readu8(DEC, band(rshift(w, 16), 255)), readu8(DEC, rshift(w, 24))
if a >= 64 or b >= 64 or c >= 64 or d >= 64 then
break
end
writeu8(out, p, a * 4 + rshift(b, 4))
writeu8(out, p + 1, band(b, 15) * 16 + rshift(c, 2))
writeu8(out, p + 2, band(c, 3) * 64 + d)
p += 3
i += 4
end
if i >= n then
break
end
end
local d = readu8(DEC, readu8(src, i))
if d < 64 then
if padded then
@@ -112,6 +161,7 @@ function Base64.decode(s: string | buffer): (buffer?, string?)
elseif d ~= 64 then
return nil, string.format("bad base64 character 0x%02x at %d", readu8(src, i), i)
end
i += 1
end
if bits >= 6 then
return nil, "truncated base64 (a single character in the last group)"