perf (LuaJIT): stop cross-codec trace pollution in long mixed processes; bench.sh mixed

- compat math.clamp is vararg: LuaJIT never starts a root trace at a vararg function but still inlines it. The plain
  shim, called from interpreted code, collected aborted root traces until it was blacklisted, after which every trace
  through it aborted: in one process, png (or anything earlier) left jpeg / webp / tiff / ImageOps 2-10x slower.
- Dct.fdctQuantize: branch-free quantiser and a divisor table (q * 8) from Jpeg.Encode; the sign branch among the column
  pass's live values made LuaJIT retry side traces it could not compile (register coalescing) forever. Also faster
  in luau -O2 (~5%) and native (~10%). Outputs unchanged.
- tools/bench_mixed.luau + bash tools/bench.sh mixed: core, audio, video, image in one LuaJIT process against the
  per-codec benches, marking lines more than 1.3x slower mixed.
This commit is contained in:
4DBug committed 2026-11-02 21:12:36 -06:00
1 parent dde6be117c
commit a87ad99410
12 files changed
+802 -710

No files matched your search

+14 -19
View File
@@ -5,7 +5,7 @@
-- libjpeg's islow (jidctint.c / jfdctint.c), the reduced IDCTs (jidctred.c) and jcdctmgr.c's rounding quantiser, bit-exact
-- with libjpeg-turbo. From jpeg-luau 1.0 (same author); IJG code: licenses/libjpeg-turbo-LICENSE.md.
local readi16, readu16, writeu16, writeu32, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.writeu32, buffer.readu8, buffer.writei16
local clamp = math.clamp
local clamp, abs, sign = math.clamp, math.abs, math.sign
local Dct = {}
@@ -384,18 +384,13 @@ end
local fw = table.create(64, 0)
local function quant(coef: buffer, off: number, div: buffer, v: number, i: number)
local qv = readu16(div, i * 2) * 8
local h = qv // 2
if v < 0 then
writei16(coef, off + i * 2, -((h - v) // qv))
else
writei16(coef, off + i * 2, (v + h) // qv)
end
-- (|v| + q * 4) // (q * 8) with v's sign, branch-free (a sign branch here gave LuaJIT side traces it could not compile)
local function quant(coef: buffer, off: number, qv: number, v: number, i: number)
writei16(coef, off + i * 2, (abs(v) + qv / 2) // qv * sign(v))
end
-- quantised as jcdctmgr.c: (|v| + q * 4) // (q * 8) with the sign restored
function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w: number, h: number, div: buffer, coef: buffer, off: number)
-- quantised as jcdctmgr.c: (|v| + q * 4) // (q * 8) with the sign restored; div[i + 1] = q * 8 for natural index i
function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w: number, h: number, div: { number }, coef: buffer, off: number)
local lastX, lastY = w - 1, h - 1
for r = 0, 7 do
local row = math.min(y + r, lastY) * stride
@@ -484,14 +479,14 @@ function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w:
local o3 = (tmp6 + z2 + z3 + 16384) // 32768
local o1 = (tmp7 + z1 + z4 + 16384) // 32768
local cc = c - 1
quant(coef, off, div, o0, cc)
quant(coef, off, div, o1, cc + 8)
quant(coef, off, div, o2, cc + 16)
quant(coef, off, div, o3, cc + 24)
quant(coef, off, div, o4, cc + 32)
quant(coef, off, div, o5, cc + 40)
quant(coef, off, div, o6, cc + 48)
quant(coef, off, div, o7, cc + 56)
quant(coef, off, div[c], o0, cc)
quant(coef, off, div[c + 8], o1, cc + 8)
quant(coef, off, div[c + 16], o2, cc + 16)
quant(coef, off, div[c + 24], o3, cc + 24)
quant(coef, off, div[c + 32], o4, cc + 32)
quant(coef, off, div[c + 40], o5, cc + 40)
quant(coef, off, div[c + 48], o6, cc + 48)
quant(coef, off, div[c + 56], o7, cc + 56)
end
end
+3 -3
View File
@@ -595,11 +595,11 @@ function Encode.run(px: buffer, w: number, h: number, bpp: number, o: Options):
local qt = { Tables.quant(Tables.LUMA, o.quality), Tables.quant(Tables.CHROMA, o.quality) }
local qdiv = {}
for t = 1, 2 do
local b = buffer.create(128)
local d = table.create(64, 0)
for i = 1, 64 do
writeu16(b, (i - 1) * 2, qt[t][i])
d[i] = qt[t][i] * 8
end
qdiv[t] = b
qdiv[t] = d
end
local comps: { Comp } = {}