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

+22 -22
View File
@@ -461,8 +461,13 @@ end
do
-- math.min / math.max are branch-free in a trace; an if / elseif clamp spawned side traces and ran ~3.5x slower.
-- NaN stays NaN, as in Luau (maxsd would return lo).
-- Vararg (`...` unused): LuaJIT never starts a root trace at a vararg function but still inlines it into its
-- callers' traces. The plain clamp, called from interpreted code, started root traces that aborted on the return
-- into the caller's loop; enough aborts blacklist it, every trace that reaches a blacklisted function aborts too, and
-- in a long mixed process one codec left every clamp user interpreted (jpeg, webp, tiff 2-10x slower). Not done for
-- the others: a vararg math.sign made Monkey's Audio's sign-heavy filters ~40% slower.
local mmin, mmax = math.min, math.max
math.clamp = math.clamp or function(x, lo, hi)
math.clamp = math.clamp or function(x, lo, hi, ...)
if lo > hi then error("max must be greater than or equal to min", 2) end
if x ~= x then return x end
return mmin(mmax(x, lo), hi)
@@ -71838,7 +71843,7 @@ local __compat = __compat local buffer = __compat.buffer --!strict
-- 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 = {}
@@ -72217,17 +72222,12 @@ end
local fw = table.create(64, 0)
local function quant(coef, off, div, v, i)
local qv = readu16(div, i * 2) * 8
local h = math.floor(qv / 2
) if v < 0 then
writei16(coef, off + i * 2, -(math.floor((h - v) / qv)))
else
writei16(coef, off + i * 2, math.floor((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, off, qv, v, i)
writei16(coef, off + i * 2, math.floor((abs(v) + qv / 2) / qv )* sign(v))
end
-- quantised as jcdctmgr.c: (|v| + q * 4) // (q * 8) with the sign restored
-- 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, stride, x, y, w, h, div, coef, off)
local lastX, lastY = w - 1, h - 1
for r = 0, 7 do
@@ -72317,14 +72317,14 @@ else
) local o3 = math.floor((tmp6 + z2 + z3 + 16384) / 32768
) local o1 = math.floor((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
@@ -75187,11 +75187,11 @@ OUT = buffer.create(math.max(1024, math.floor(w * h / 2)))
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= {}