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>
211 lines
8.9 KiB
C++
211 lines
8.9 KiB
C++
// Proof for the garbage-free luaL_Buffer (src/modules/strbuf_core.h).
|
|
//
|
|
// Real Lua 5.0.3 (tests/lua50, 32-bit, 512-byte LUAL_BUFFERSIZE like the
|
|
// client) with its six luaL_Buffer functions detoured with MinHook to the same
|
|
// core the DLL uses. tests/strbuf_cases.lua generates thousands of
|
|
// table.concat / string.rep / gsub / format / lower / upper / char cases; they
|
|
// run once with the original functions and once with the replacement, and
|
|
// every result (or error message) must be byte-identical. Allocated bytes per
|
|
// category and benchmarks are reported for both.
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <windows.h>
|
|
|
|
extern "C" {
|
|
#include "lauxlib.h"
|
|
#include "lua.h"
|
|
#include "lualib.h"
|
|
}
|
|
#include "../src/modules/strbuf_core.h"
|
|
#include "MinHook.h"
|
|
|
|
// --- Allocation counter (lmem.c is built with l_realloc -> dp_test_realloc) -----------
|
|
static uint64_t g_allocated;
|
|
extern "C" void *dp_test_realloc(void *b, size_t os, size_t s) {
|
|
if (s > os) g_allocated += s - os;
|
|
return realloc(b, s);
|
|
}
|
|
|
|
// --- The replacement, wired to real Lua ------------------------------------------------
|
|
struct TestApi {
|
|
static size_t strlen(void *L, int i) { return lua_strlen((lua_State *)L, i); }
|
|
static const char *tostring(void *L, int i) { return lua_tostring((lua_State *)L, i); }
|
|
static void pop1(void *L) { lua_settop((lua_State *)L, -2); }
|
|
static void pushlstring(void *L, const char *s, size_t l) { lua_pushlstring((lua_State *)L, s, l); }
|
|
static void outOfMemory(void *L) { luaL_error((lua_State *)L, "not enough memory"); }
|
|
static void *alloc(size_t n) { return malloc(n); }
|
|
static void *grow(void *p, size_t n) { return realloc(p, n); }
|
|
static void release(void *p) { free(p); }
|
|
};
|
|
static strbuf::Engine<TestApi> g_engine;
|
|
static_assert(sizeof(strbuf::Buffer) == sizeof(luaL_Buffer), "luaL_Buffer layout");
|
|
|
|
#define B(b) ((strbuf::Buffer *)(b))
|
|
static void d_buffinit(lua_State *L, luaL_Buffer *b) { g_engine.buffinit(L, B(b)); }
|
|
static char *d_prepbuffer(luaL_Buffer *b) { return g_engine.prepbuffer(B(b)); }
|
|
static void d_addlstring(luaL_Buffer *b, const char *s, size_t l) { g_engine.addlstring(B(b), s, l); }
|
|
static void d_addstring(luaL_Buffer *b, const char *s) { g_engine.addstring(B(b), s); }
|
|
static void d_addvalue(luaL_Buffer *b) { g_engine.addvalue(B(b)); }
|
|
static void d_pushresult(luaL_Buffer *b) { g_engine.pushresult(B(b)); }
|
|
|
|
static void *g_originals[6];
|
|
static void *const g_targets[6] = {(void *)&luaL_buffinit, (void *)&luaL_prepbuffer, (void *)&luaL_addlstring,
|
|
(void *)&luaL_addstring, (void *)&luaL_addvalue, (void *)&luaL_pushresult};
|
|
static void *const g_detours[6] = {(void *)&d_buffinit, (void *)&d_prepbuffer, (void *)&d_addlstring,
|
|
(void *)&d_addstring, (void *)&d_addvalue, (void *)&d_pushresult};
|
|
|
|
static void setReplacement(bool on) {
|
|
for (void *t : g_targets) on ? MH_EnableHook(t) : MH_DisableHook(t);
|
|
}
|
|
|
|
// --- Lua helpers -------------------------------------------------------------------------
|
|
static int l_allocated(lua_State *L) {
|
|
lua_pushnumber(L, (lua_Number)g_allocated);
|
|
return 1;
|
|
}
|
|
static int l_clock_us(lua_State *L) {
|
|
LARGE_INTEGER t, f;
|
|
QueryPerformanceCounter(&t);
|
|
QueryPerformanceFrequency(&f);
|
|
lua_pushnumber(L, (lua_Number)t.QuadPart * 1e6 / (lua_Number)f.QuadPart);
|
|
return 1;
|
|
}
|
|
|
|
static int g_failures;
|
|
static void check(const char *what, bool ok) {
|
|
printf(" %-62s %s\n", what, ok ? "ok" : "FAIL");
|
|
if (!ok) g_failures++;
|
|
}
|
|
|
|
static bool call(lua_State *L, const char *fn, int nargs, double arg, int nres) {
|
|
lua_getglobal(L, fn);
|
|
if (nargs) lua_pushnumber(L, arg);
|
|
if (lua_pcall(L, nargs, nres, 0) != 0) {
|
|
printf(" %s failed: %s\n", fn, lua_tostring(L, -1));
|
|
g_failures++;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// One fresh Lua state per mode: Lua interns strings, so a state that already
|
|
// holds the original run's results would allocate nothing for equal results.
|
|
static lua_State *newState() {
|
|
lua_State *L = lua_open();
|
|
luaopen_base(L);
|
|
luaopen_table(L);
|
|
luaopen_string(L);
|
|
luaopen_math(L);
|
|
lua_settop(L, 0);
|
|
lua_register(L, "allocated", l_allocated);
|
|
lua_register(L, "clock_us", l_clock_us);
|
|
if (luaL_loadfile(L, "tests/strbuf_cases.lua") || lua_pcall(L, 0, 0, 0)) {
|
|
printf("cannot load tests/strbuf_cases.lua: %s\n", lua_tostring(L, -1));
|
|
exit(1);
|
|
}
|
|
return L;
|
|
}
|
|
|
|
static bool sameString(lua_State *a, int ia, lua_State *b, int ib) {
|
|
size_t la = lua_strlen(a, ia), lb = lua_strlen(b, ib);
|
|
return la == lb && memcmp(lua_tostring(a, ia), lua_tostring(b, ib), la) == 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
int seeds = argc > 1 ? atoi(argv[1]) : 3;
|
|
MH_Initialize();
|
|
for (int i = 0; i < 6; i++) MH_CreateHook(g_targets[i], g_detours[i], &g_originals[i]);
|
|
char line[96];
|
|
|
|
printf("Identical results (original vs replacement), %d seeds:\n", seeds);
|
|
long total_cases = 0, mismatches = 0;
|
|
for (int s = 1; s <= seeds; s++) {
|
|
double seed = 12345.0 * s;
|
|
lua_State *L0 = newState(), *L1 = newState();
|
|
setReplacement(false);
|
|
if (!call(L0, "run", 1, seed, 2)) return 1; // L0: R A
|
|
setReplacement(true);
|
|
if (!call(L1, "run", 1, seed, 2)) return 1; // L1: R A
|
|
setReplacement(false);
|
|
int n0 = luaL_getn(L0, 1), n1 = luaL_getn(L1, 1);
|
|
if (n0 != n1) mismatches++;
|
|
for (int i = 1; i <= n0 && i <= n1; i++) {
|
|
lua_rawgeti(L0, 1, i);
|
|
lua_rawgeti(L1, 1, i);
|
|
if (!sameString(L0, -1, L1, -1)) {
|
|
if (mismatches < 5)
|
|
printf(" MISMATCH case %d (seed %d): %.80s | %.80s\n", i, s, lua_tostring(L0, -1),
|
|
lua_tostring(L1, -1));
|
|
mismatches++;
|
|
}
|
|
lua_pop(L0, 1);
|
|
lua_pop(L1, 1);
|
|
}
|
|
total_cases += n0;
|
|
if (s == 1) {
|
|
printf(" Lua heap allocated per category (inputs + results + garbage):\n");
|
|
printf(" %-12s %14s %14s %8s\n", "", "original", "replacement", "less");
|
|
lua_pushnil(L0);
|
|
while (lua_next(L0, 2)) {
|
|
// L0: key value; look the same key up in L1's table
|
|
lua_pushlstring(L1, lua_tostring(L0, -2), lua_strlen(L0, -2));
|
|
lua_gettable(L1, 2);
|
|
double a0 = lua_tonumber(L0, -1), a1 = lua_tonumber(L1, -1);
|
|
printf(" %-12s %14.0f %14.0f %7.1fx\n", lua_tostring(L0, -2), a0, a1, a1 > 0 ? a0 / a1 : 0);
|
|
lua_pop(L1, 1);
|
|
lua_pop(L0, 1);
|
|
}
|
|
}
|
|
lua_close(L0);
|
|
setReplacement(true); // L1's buffers were built by the replacement; closing frees no buffer state
|
|
lua_close(L1);
|
|
setReplacement(false);
|
|
}
|
|
snprintf(line, sizeof line, "%ld cases, %ld mismatches", total_cases, mismatches);
|
|
check(line, mismatches == 0 && total_cases > 0);
|
|
|
|
// Abandoned builds (errors in gsub callbacks) must be released by later builds.
|
|
lua_State *L = newState();
|
|
setReplacement(true);
|
|
lua_dostring(L, "local t = {} for k = 1, 10 do t[k] = string.rep('z', 300) end _G.x = table.concat(t)");
|
|
setReplacement(false);
|
|
snprintf(line, sizeof line, "no C-heap buffer left live after a later build (%u)", (unsigned)g_engine.live());
|
|
check(line, g_engine.live() == 0);
|
|
printf(" replacement stats: %llu results, %llu over 512 bytes (%.1f MB)\n",
|
|
(unsigned long long)g_engine.results, (unsigned long long)g_engine.spilled_results,
|
|
g_engine.spilled_bytes / 1048576.0);
|
|
lua_close(L);
|
|
|
|
printf("Benchmarks (per call, full GC before each):\n");
|
|
printf(" %-34s %9s %12s %12s %9s %9s\n", "", "result", "alloc orig", "alloc new", "us orig", "us new");
|
|
lua_State *B0 = newState(), *B1 = newState();
|
|
setReplacement(false);
|
|
if (!call(B0, "bench", 0, 0, 1)) return 1;
|
|
setReplacement(true);
|
|
if (!call(B1, "bench", 0, 0, 1)) return 1;
|
|
setReplacement(false);
|
|
int nb = luaL_getn(B0, 1);
|
|
for (int i = 1; i <= nb; i++) {
|
|
double v[2][4];
|
|
lua_State *S[2] = {B0, B1};
|
|
for (int p = 0; p < 2; p++) {
|
|
lua_rawgeti(S[p], 1, i);
|
|
for (int k = 0; k < 4; k++) {
|
|
lua_rawgeti(S[p], -1, k + 1);
|
|
v[p][k] = k == 0 ? 0 : lua_tonumber(S[p], -1);
|
|
if (k == 0 && p == 0) snprintf(line, sizeof line, "%s", lua_tostring(S[p], -1));
|
|
lua_pop(S[p], 1);
|
|
}
|
|
lua_pop(S[p], 1);
|
|
}
|
|
printf(" %-34s %9.0f %12.0f %12.0f %9.1f %9.1f\n", line, v[0][1], v[0][2], v[1][2], v[0][3], v[1][3]);
|
|
if (v[0][1] > 600) { // results that don't fit the 512-byte buffer
|
|
snprintf(line, sizeof line, " garbage-free: allocation <= result size + 1%% + 64 B");
|
|
check(line, v[1][2] <= v[0][1] * 1.01 + 64 + 24);
|
|
}
|
|
}
|
|
printf("%s\n", g_failures ? "FAIL" : "PASS");
|
|
return g_failures ? 1 : 0;
|
|
}
|