Replace the client's six luaL_Buffer functions so full 512-byte chunks go to a C-heap buffer and only the final Lua string is created, instead of a Lua string per chunk plus every merge. Fixes all users at once with identical output (argument checks, getn/setn, number conversion and errors remain the client's code). All-or-nothing install, refused if another DLL patched the functions; strbuf=0 in ducielperf.cfg switches it off. tests/test_strbuf: the same core on real Lua 5.0.3 (512-byte buffer), ~26,000 generated cases per run byte-identical to the original, abandoned builds released, and allocation/time benchmarks (table.concat 500 lines: 110 KB -> 25 KB allocated, 28 -> 20 us). Adds config_int (ducielperf.cfg) and hook_remove to the core, and DucielPerf_GetStringBufferStats. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
248 lines
9.4 KiB
Lua
248 lines
9.4 KiB
Lua
-- Test cases for the garbage-free luaL_Buffer (tests/test_strbuf.cpp).
|
|
-- Lua 5.0. run(seed) returns results (one string per case: the output, or
|
|
-- "ERR:" .. message) and allocated bytes per category. The driver runs it with
|
|
-- the original buffer functions and with the replacement, and compares.
|
|
|
|
local seed
|
|
local function rnd(n) -- 0 .. n-1, Park-Miller
|
|
seed = math.mod(seed * 16807, 2147483647)
|
|
return math.mod(seed, n)
|
|
end
|
|
|
|
local CHARS = {}
|
|
for c = 0, 255 do CHARS[c] = string.char(c) end
|
|
|
|
-- Random string of len bytes (from alphabet if given). Built from chunks of
|
|
-- at most 64 characters with string.char (well under the 512-byte buffer)
|
|
-- joined with .., so it doesn't depend on the long-build path under test.
|
|
local codes = {}
|
|
local function randstr(len, alphabet)
|
|
local s, alen = "", alphabet and string.len(alphabet)
|
|
local left = len
|
|
while left > 0 do
|
|
local n = left > 64 and 64 or left
|
|
for k = 1, n do
|
|
codes[k] = alphabet and string.byte(alphabet, rnd(alen) + 1) or rnd(256)
|
|
end
|
|
for k = n + 1, 64 do codes[k] = nil end
|
|
s = s .. string.char(unpack(codes)) -- Lua 5.0: unpack stops at the first nil
|
|
left = left - n
|
|
end
|
|
return s
|
|
end
|
|
|
|
local function bigstr(len) -- fast, varied, contains \0
|
|
local s = randstr(64)
|
|
while string.len(s) < len do s = s .. s end
|
|
return string.sub(s, 1, len)
|
|
end
|
|
|
|
local SPECIAL = { 1 / 0, -1 / 0, 0, -0, 1e300, 1e-300, 123456789012345678, 0.1, -2.5, 3 }
|
|
|
|
local function randvalue()
|
|
local k = rnd(20)
|
|
if k < 8 then return randstr(rnd(20)) end
|
|
if k < 10 then return randstr(rnd(20), "abc\0") end
|
|
if k < 12 then return bigstr(rnd(2000)) end
|
|
if k < 14 then return rnd(100000) - 50000 end
|
|
if k < 16 then return (rnd(100000) - 50000) / 7 end
|
|
if k < 18 then return SPECIAL[rnd(table.getn(SPECIAL)) + 1] end
|
|
if k == 18 then return "" end
|
|
return bigstr(512 + rnd(3) - 1) -- around the buffer size
|
|
end
|
|
|
|
local results, alloc, cat
|
|
local function case(category, f)
|
|
cat = category
|
|
local a0 = allocated()
|
|
local ok, r = pcall(f)
|
|
alloc[category] = (alloc[category] or 0) + allocated() - a0
|
|
if ok then
|
|
results[table.getn(results) + 1] = type(r) == "string" and r or ("VAL:" .. tostring(r))
|
|
else
|
|
results[table.getn(results) + 1] = "ERR:" .. tostring(r)
|
|
end
|
|
end
|
|
|
|
local function concatCases(n)
|
|
for c = 1, n do
|
|
local size = rnd(5) == 0 and rnd(2000) or rnd(60)
|
|
local t = {}
|
|
for k = 1, size do t[k] = randvalue() end
|
|
local bad = rnd(25)
|
|
if bad == 0 and size > 0 then t[rnd(size) + 1] = {} end
|
|
if bad == 1 and size > 0 then t[rnd(size) + 1] = true end
|
|
if bad == 2 then table.setn(t, size + rnd(3)) end
|
|
if bad == 3 then table.setn(t, rnd(size + 1)) end
|
|
if bad == 4 then t.n = rnd(size + 1) end
|
|
local seps = { nil, "", ",", ", ", "\0", bigstr(600), randstr(rnd(8)), 7, 0.5 }
|
|
local sep = seps[rnd(9) + 1]
|
|
local form = rnd(6)
|
|
local i = rnd(size + 6) - 3
|
|
local j = rnd(4) == 0 and 0 or rnd(size + 6) - 3
|
|
case("concat", function()
|
|
if form == 0 then return table.concat(t) end
|
|
if form == 1 then return table.concat(t, sep) end
|
|
if form == 2 then return table.concat(t, sep, i) end
|
|
if form == 3 then return table.concat(t, sep, i, j) end
|
|
if form == 4 then return table.concat(t, sep, nil, j) end
|
|
return table.concat(t, sep, i, "x") -- bad argument
|
|
end)
|
|
end
|
|
case("concat", function() return table.concat({}) end)
|
|
case("concat", function() return table.concat({}, ",") end)
|
|
case("concat", function() return table.concat({ "a" }, ",", 1, 0) end)
|
|
case("concat", function() return table.concat("not a table") end)
|
|
case("concat", function() return table.concat() end)
|
|
end
|
|
|
|
local function repCases(n)
|
|
for c = 1, n do
|
|
local s = rnd(4) == 0 and bigstr(rnd(800)) or randstr(rnd(10))
|
|
local count = rnd(300) - 3
|
|
case("rep", function() return string.rep(s, count) end)
|
|
end
|
|
case("rep", function() return string.rep("ab", 300000) end)
|
|
case("rep", function() return string.rep("", 100000) end)
|
|
case("rep", function() return string.rep("x") end)
|
|
end
|
|
|
|
local PATTERNS = { "a", "(a)(b)", "%s+", ".", "", "^a", "b*", "(%w+)", "%z", "[ab]+", "c", "(a*(.)%w(%s*))" }
|
|
|
|
local function gsubCases(n)
|
|
for c = 1, n do
|
|
local subject = rnd(3) == 0 and bigstr(rnd(4000)) or randstr(rnd(3000), "abc %\0\n")
|
|
local pat = PATTERNS[rnd(table.getn(PATTERNS)) + 1]
|
|
local kind = rnd(8)
|
|
local max = rnd(3) == 0 and rnd(50) or nil
|
|
local repl
|
|
if kind == 0 then
|
|
repl = randstr(rnd(6), "xy%01")
|
|
elseif kind == 1 then
|
|
repl = "[%0]"
|
|
elseif kind == 2 then
|
|
repl = bigstr(700)
|
|
elseif kind == 3 then
|
|
repl = { a = "A", b = false, c = 12.5, ab = bigstr(600) }
|
|
elseif kind == 4 then
|
|
local calls = 0
|
|
repl = function(x)
|
|
calls = calls + 1
|
|
local m = math.mod(calls, 5)
|
|
if m == 0 then return nil end
|
|
if m == 1 then return false end
|
|
if m == 2 then return calls end
|
|
if m == 3 then return bigstr(300 + calls) end
|
|
return "<" .. tostring(x) .. ">"
|
|
end
|
|
elseif kind == 5 then
|
|
local calls = 0
|
|
local limit = rnd(20) + 1
|
|
repl = function(x)
|
|
calls = calls + 1
|
|
if calls == limit then error("stop at " .. calls) end -- abandons the build
|
|
return bigstr(200)
|
|
end
|
|
elseif kind == 6 then
|
|
-- nested buffers: the callback builds strings while gsub's buffer is live
|
|
repl = function(x)
|
|
local t = {}
|
|
for k = 1, 40 do t[k] = bigstr(50 + k) end
|
|
return string.sub(table.concat(t, "|"), 1, 100) .. string.upper(bigstr(600))
|
|
end
|
|
else
|
|
repl = "%9" -- invalid capture
|
|
end
|
|
case("gsub", function()
|
|
local r, count = string.gsub(subject, pat, repl, max)
|
|
return r .. "#" .. count
|
|
end)
|
|
end
|
|
end
|
|
|
|
local function formatCases(n)
|
|
local FMTS = { "%s", "[%s]", "%q", "%d", "%5.2f", "%x", "%c", "%%", "%10s", "%-20s|", "%.3s", "%s%s%s", "%g" }
|
|
for c = 1, n do
|
|
local parts, args = {}, {}
|
|
local nparts = rnd(6) + 1
|
|
for k = 1, nparts do
|
|
local f = FMTS[rnd(table.getn(FMTS)) + 1]
|
|
parts[k] = f
|
|
if f == "%d" or f == "%x" or f == "%c" then
|
|
args[table.getn(args) + 1] = rnd(256)
|
|
elseif f == "%5.2f" or f == "%g" then
|
|
args[table.getn(args) + 1] = randvalue()
|
|
elseif f == "%s%s%s" then
|
|
for m = 1, 3 do args[table.getn(args) + 1] = randvalue() end
|
|
elseif f ~= "%%" then
|
|
args[table.getn(args) + 1] = randvalue()
|
|
end
|
|
end
|
|
local fmt = ""
|
|
for k = 1, nparts do fmt = fmt .. parts[k] .. randstr(rnd(4), "ab ") end
|
|
case("format", function() return string.format(fmt, unpack(args)) end)
|
|
end
|
|
case("format", function() return string.format("%q", "a\0b\nc\rd\"e\\f" .. bigstr(3000)) end)
|
|
case("format", function() return string.format("%s", bigstr(100000)) end)
|
|
case("format", function() return string.format("%d") end)
|
|
end
|
|
|
|
local function caseCases(n)
|
|
for c = 1, n do
|
|
local s = rnd(3) == 0 and bigstr(rnd(6000)) or randstr(rnd(50))
|
|
case("lower/upper", function() return string.lower(s) .. "/" .. string.upper(s) end)
|
|
end
|
|
end
|
|
|
|
local function charCases(n)
|
|
for c = 1, n do
|
|
local t = {}
|
|
for k = 1, rnd(1500) do t[k] = rnd(256) end
|
|
case("char", function() return string.char(unpack(t)) end)
|
|
end
|
|
end
|
|
|
|
function run(s)
|
|
seed = s
|
|
results, alloc = {}, {}
|
|
concatCases(3000)
|
|
repCases(1500)
|
|
gsubCases(1500)
|
|
formatCases(1500)
|
|
caseCases(800)
|
|
charCases(400)
|
|
return results, alloc
|
|
end
|
|
|
|
-- Benchmarks: allocated bytes and time per call for typical builds.
|
|
function bench()
|
|
seed = 777
|
|
local out = {}
|
|
-- A full GC before each call, so no string from an earlier call can be
|
|
-- reused (Lua interns strings): allocation = result + garbage of one call.
|
|
local function b(name, iters, f)
|
|
local a, t, len = 0, 0, 0
|
|
for k = 1, iters do
|
|
collectgarbage()
|
|
local a0, t0 = allocated(), clock_us()
|
|
len = string.len(f())
|
|
t = t + clock_us() - t0
|
|
a = a + allocated() - a0
|
|
end
|
|
out[table.getn(out) + 1] = { name, len, a / iters, t / iters }
|
|
end
|
|
local lines = {}
|
|
for k = 1, 500 do lines[k] = "line " .. k .. ": " .. bigstr(40) end
|
|
local small = {}
|
|
for k = 1, 20 do small[k] = "w" .. k end
|
|
local subject = string.rep("abc def ", 2500) -- 20 KB, 2500 matches
|
|
local big = bigstr(5000)
|
|
b("table.concat 500 lines x ~50 B", 200, function() return table.concat(lines, "\n") end)
|
|
b("table.concat 20 short strings", 2000, function() return table.concat(small, " ") end)
|
|
b("string.rep 100 KB", 200, function() return string.rep("abcd", 25000) end)
|
|
b("string.gsub 20 KB, 2500 matches", 200, function() return (string.gsub(subject, "a", "xy")) end)
|
|
b("string.format %s 5 KB x3", 300, function() return string.format("%s|%s|%s", big, big, big) end)
|
|
b("string.upper 5 KB", 300, function() return string.upper(big) end)
|
|
return out
|
|
end
|