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

+13 -1
View File
@@ -2,7 +2,9 @@
# Timings (bash tools/bench.sh [core [filter]]): tools/bench.luau under luau --codegen -O2 and under LuaJIT (the module build,
# build/_modules/luajit). "core" runs tools/bench_core.luau (inflate / deflate, checksums, rbxm / rbxmx, json / xml,
# F.detect, gltf / obj, the other decompressors) under luau -O2, luau --codegen -O2 and LuaJIT; a filter keeps the lines
# whose label contains it.
# whose label contains it. "mixed" (LuaJIT only) runs tools/bench_image.luau and tools/bench_video.luau each on its own,
# then tools/bench_mixed.luau: core, audio, video and image in one process, marking the lines that run more than 1.3x
# slower there (trace pollution between codecs).
cd "$(dirname "$0")/.." || exit 1
LUAU=${LUAU:-$(command -v luau || nix shell nixpkgs#luau -c sh -c 'command -v luau')}
LUAJIT=${LUAJIT:-$(command -v luajit || nix shell nixpkgs#luajit -c sh -c 'command -v luajit')}
@@ -15,5 +17,15 @@ if [ "$1" = core ]; then
"$LUAJIT" -e "$LJPATH" build/_modules/luajit/tools/bench_core.lua "$2"
exit 0
fi
if [ "$1" = mixed ]; then
tmp=$(mktemp -d)
for s in image video; do
"$LUAJIT" -e "$LJPATH" build/_modules/luajit/tools/bench_$s.lua > "$tmp/$s" || exit 1
done
"$LUAJIT" -e "$LJPATH" build/_modules/luajit/tools/bench_mixed.lua "$tmp/image" "$tmp/video"
status=$?
rm -r "$tmp"
exit $status
fi
"$LUAU" --codegen -O2 tools/bench.luau
"$LUAJIT" -e "$LJPATH" build/_modules/luajit/tools/bench.lua
+85
View File
@@ -0,0 +1,85 @@
-- Mixed-workload check (bash tools/bench.sh mixed), LuaJIT only: bench_core, bench_audio, bench_video and bench_image
-- in one process, as an app that decodes many formats runs them, the video and image lines against each bench run
-- alone (the files of its arguments, written by bench.sh). LuaJIT's traces, blacklists and penalties are per process:
-- a shared helper blacklisted by one codec, or a side trace that never compiles, slows every later caller, which the
-- per-codec benches never show. Lines more than 1.3x slower mixed than alone are marked.
local jit = (_G :: any).jit
if jit == nil then
print("bench_mixed: LuaJIT only (bash tools/bench.sh mixed)")
return
end
local G = _G :: any
local loadfile, io = G.loadfile, G.io
local imageAlone, videoAlone = ...
-- "luajit <label> <ms> ms ...": label -> ms, the first time each label is printed
local function parse(lines: { string }): ({ [string]: number }, { string })
local ms, order = {}, {}
for _, l in lines do
local label, v = string.match(l, "^luajit%s+(.-)%s+([%d%.]+) ms")
if label and ms[label] == nil then
ms[label] = tonumber(v) :: number
table.insert(order, label)
end
end
return ms, order
end
local function readLines(path: string): { string }
local f = assert(io.open(path, "r"), "bench_mixed: cannot read " .. path)
local lines = {}
for l in f:lines() do
table.insert(lines, l)
end
f:close()
return lines
end
-- runs build/_modules/luajit/tools/<name>.lua in this process and returns what it printed
local print0 = G.print
local function run(name: string): { string }
local lines = {}
G.print = function(...)
local parts = table.pack(...)
for i = 1, parts.n do
parts[i] = tostring(parts[i])
end
table.insert(lines, table.concat(parts :: any, "\t"))
end
local chunk = assert(loadfile("build/_modules/luajit/tools/" .. name .. ".lua"))
local ok, err = pcall(chunk)
G.print = print0
assert(ok, err)
return lines
end
jit.opt.start("maxtrace=8000", "maxmcode=65536")
print0("bench_mixed: core, audio, video, image in one process")
run("bench_core")
run("bench_audio")
local video = run("bench_video")
local image = run("bench_image")
local marked, total = 0, 0
local function compare(title: string, alonePath: string?, mixedLines: { string })
if not alonePath then
return
end
local alone = parse(readLines(alonePath))
local mixed, order = parse(mixedLines)
print0(string.format("%-8s %-40s %10s %10s %7s", title, "", "alone ms", "mixed ms", "ratio"))
for _, label in order do
local a, m = alone[label], mixed[label]
if a and a > 0 then
local r = m / a
total += 1
if r > 1.3 then
marked += 1
end
print0(string.format("%-8s %-40s %10.2f %10.2f %6.2fx%s", "luajit", label, a, m, r, if r > 1.3 then " <<" else ""))
end
end
end
compare("image", imageAlone, image)
compare("video", videoAlone, video)
print0(string.format("bench_mixed: %d of %d lines more than 1.3x slower mixed than alone", marked, total))