Reduced Lua GC pressure by a lot, reuse cobjects/pointers

This commit is contained in:
MysterD 2023-06-24 23:43:03 -07:00
parent aaa23a8394
commit aeb8817f0d
7 changed files with 134 additions and 17 deletions

View file

@ -1,5 +1,7 @@
math.randomseed(get_time()) math.randomseed(get_time())
_CObjectPool = {}
_CObject = { _CObject = {
__index = function (t,k) __index = function (t,k)
return _get_field(t['_lot'], t['_pointer'], k, t) return _get_field(t['_lot'], t['_pointer'], k, t)
@ -7,11 +9,33 @@ _CObject = {
__newindex = function (t,k,v) __newindex = function (t,k,v)
_set_field(t['_lot'], t['_pointer'], k, v, t) _set_field(t['_lot'], t['_pointer'], k, v, t)
end, end,
__tostring = function(t)
return 'CObject: ' .. t['_lot'] .. ', [' .. string.format('0x%08X', t['_pointer']) .. ']'
end,
__eq = function (a, b) __eq = function (a, b)
return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil
end end
} }
function _NewCObject(lot, pointer)
if _CObjectPool[lot] == nil then
_CObjectPool[lot] = {}
end
if _CObjectPool[lot][pointer] == nil then
local obj = {}
rawset(obj, '_pointer', pointer)
rawset(obj, '_lot', lot)
setmetatable(obj, _CObject)
_CObjectPool[lot][pointer] = obj
return obj
end
return _CObjectPool[lot][pointer]
end
local _CPointerPool = {}
_CPointer = { _CPointer = {
__index = function (t,k) __index = function (t,k)
return nil return nil
@ -26,6 +50,23 @@ _CPointer = {
end end
} }
function _NewCPointer(lvt, pointer)
if _CPointerPool[lvt] == nil then
_CPointerPool[lvt] = {}
end
if _CPointerPool[lvt][pointer] == nil then
local obj = {}
rawset(obj, '_pointer', pointer)
rawset(obj, '_lvt', lvt)
setmetatable(obj, _CPointer)
_CPointerPool[lvt][pointer] = obj
return obj
end
return _CPointerPool[lvt][pointer]
end
_SyncTable = { _SyncTable = {
__index = function (t,k) __index = function (t,k)
local _table = rawget(t, '_table') local _table = rawget(t, '_table')

View file

@ -2,6 +2,8 @@
math.randomseed(get_time()) math.randomseed(get_time())
_CObjectPool = {}
_CObject = { _CObject = {
__index = function (t,k) __index = function (t,k)
return _get_field(t['_lot'], t['_pointer'], k, t) return _get_field(t['_lot'], t['_pointer'], k, t)
@ -9,11 +11,33 @@ _CObject = {
__newindex = function (t,k,v) __newindex = function (t,k,v)
_set_field(t['_lot'], t['_pointer'], k, v, t) _set_field(t['_lot'], t['_pointer'], k, v, t)
end, end,
__tostring = function(t)
return 'CObject: ' .. t['_lot'] .. ', [' .. string.format('0x%08X', t['_pointer']) .. ']'
end,
__eq = function (a, b) __eq = function (a, b)
return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil
end end
} }
function _NewCObject(lot, pointer)
if _CObjectPool[lot] == nil then
_CObjectPool[lot] = {}
end
if _CObjectPool[lot][pointer] == nil then
local obj = {}
rawset(obj, '_pointer', pointer)
rawset(obj, '_lot', lot)
setmetatable(obj, _CObject)
_CObjectPool[lot][pointer] = obj
return obj
end
return _CObjectPool[lot][pointer]
end
local _CPointerPool = {}
_CPointer = { _CPointer = {
__index = function (t,k) __index = function (t,k)
return nil return nil
@ -28,6 +52,23 @@ _CPointer = {
end end
} }
function _NewCPointer(lvt, pointer)
if _CPointerPool[lvt] == nil then
_CPointerPool[lvt] = {}
end
if _CPointerPool[lvt][pointer] == nil then
local obj = {}
rawset(obj, '_pointer', pointer)
rawset(obj, '_lvt', lvt)
setmetatable(obj, _CPointer)
_CPointerPool[lvt][pointer] = obj
return obj
end
return _CPointerPool[lvt][pointer]
end
_SyncTable = { _SyncTable = {
__index = function (t,k) __index = function (t,k)
local _table = rawget(t, '_table') local _table = rawget(t, '_table')

BIN
lib/lua/linux/liblua54.a Executable file

Binary file not shown.

View file

@ -338,6 +338,9 @@ function mario_local_update(m)
local s = gPlayerSyncTable[m.playerIndex] local s = gPlayerSyncTable[m.playerIndex]
local e = gMarioStateExtras[m.playerIndex] local e = gMarioStateExtras[m.playerIndex]
if (m.controller.buttonDown & Z_TRIG) ~= 0 then
mario_fire_flower_use(m)
end
-- decrease cooldown -- decrease cooldown
if e.attackCooldown > 0 then if e.attackCooldown > 0 then
e.attackCooldown = e.attackCooldown - 1 e.attackCooldown = e.attackCooldown - 1

View file

@ -1,5 +1,6 @@
char gSmluaConstants[] = "" char gSmluaConstants[] = ""
"math.randomseed(get_time())\n" "math.randomseed(get_time())\n"
"_CObjectPool = {}\n"
"_CObject = {\n" "_CObject = {\n"
" __index = function (t,k)\n" " __index = function (t,k)\n"
" return _get_field(t['_lot'], t['_pointer'], k, t)\n" " return _get_field(t['_lot'], t['_pointer'], k, t)\n"
@ -7,10 +8,28 @@ char gSmluaConstants[] = ""
" __newindex = function (t,k,v)\n" " __newindex = function (t,k,v)\n"
" _set_field(t['_lot'], t['_pointer'], k, v, t)\n" " _set_field(t['_lot'], t['_pointer'], k, v, t)\n"
" end,\n" " end,\n"
" __tostring = function(t)\n"
" return 'CObject: ' .. t['_lot'] .. ', [' .. string.format('0x%08X', t['_pointer']) .. ']'\n"
" end,\n"
" __eq = function (a, b)\n" " __eq = function (a, b)\n"
" return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil\n" " return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil\n"
" end\n" " end\n"
"}\n" "}\n"
"function _NewCObject(lot, pointer)\n"
" if _CObjectPool[lot] == nil then\n"
" _CObjectPool[lot] = {}\n"
" end\n"
" if _CObjectPool[lot][pointer] == nil then\n"
" local obj = {}\n"
" rawset(obj, '_pointer', pointer)\n"
" rawset(obj, '_lot', lot)\n"
" setmetatable(obj, _CObject)\n"
" _CObjectPool[lot][pointer] = obj\n"
" return obj\n"
" end\n"
" return _CObjectPool[lot][pointer]\n"
"end\n"
"local _CPointerPool = {}\n"
"_CPointer = {\n" "_CPointer = {\n"
" __index = function (t,k)\n" " __index = function (t,k)\n"
" return nil\n" " return nil\n"
@ -24,6 +43,20 @@ char gSmluaConstants[] = ""
" return a['_pointer'] == b['_pointer'] and a['_pointer'] ~= nil and a['_lvt'] ~= nil\n" " return a['_pointer'] == b['_pointer'] and a['_pointer'] ~= nil and a['_lvt'] ~= nil\n"
" end\n" " end\n"
"}\n" "}\n"
"function _NewCPointer(lvt, pointer)\n"
" if _CPointerPool[lvt] == nil then\n"
" _CPointerPool[lvt] = {}\n"
" end\n"
" if _CPointerPool[lvt][pointer] == nil then\n"
" local obj = {}\n"
" rawset(obj, '_pointer', pointer)\n"
" rawset(obj, '_lvt', lvt)\n"
" setmetatable(obj, _CPointer)\n"
" _CPointerPool[lvt][pointer] = obj\n"
" return obj\n"
" end\n"
" return _CPointerPool[lvt][pointer]\n"
"end\n"
"_SyncTable = {\n" "_SyncTable = {\n"
" __index = function (t,k)\n" " __index = function (t,k)\n"
" local _table = rawget(t, '_table')\n" " local _table = rawget(t, '_table')\n"

View file

@ -420,14 +420,14 @@ void smlua_push_object(lua_State* L, u16 lot, void* p) {
// add to allowlist // add to allowlist
smlua_cobject_allowlist_add(lot, (u64)(intptr_t)p); smlua_cobject_allowlist_add(lot, (u64)(intptr_t)p);
lua_newtable(L); // get a cobject from a function
int t = lua_gettop(L); lua_getglobal(L, "_NewCObject"); // Get the function by its global name
smlua_push_integer_field(t, "_lot", lot); lua_pushinteger(L, lot);
smlua_push_integer_field(t, "_pointer", (u64)(intptr_t)p); lua_pushinteger(L, (u64)(intptr_t)p);
lua_pushglobaltable(L);
lua_getfield(gLuaState, -1, "_CObject"); if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
lua_setmetatable(L, -3); LOG_ERROR("Error calling Lua function: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // pop global table }
} }
void smlua_push_pointer(lua_State* L, u16 lvt, void* p) { void smlua_push_pointer(lua_State* L, u16 lvt, void* p) {
@ -438,14 +438,13 @@ void smlua_push_pointer(lua_State* L, u16 lvt, void* p) {
smlua_cpointer_allowlist_add(lvt, (u64)(intptr_t)p); smlua_cpointer_allowlist_add(lvt, (u64)(intptr_t)p);
lua_newtable(L); // get a cpointer from a function
int t = lua_gettop(L); lua_getglobal(L, "_NewCPointer"); // Get the function by its global name
smlua_push_integer_field(t, "_lvt", lvt); lua_pushinteger(L, lvt);
smlua_push_integer_field(t, "_pointer", (u64)(intptr_t)p); lua_pushinteger(L, (u64)(intptr_t)p);
lua_pushglobaltable(L); if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
lua_getfield(gLuaState, -1, "_CPointer"); LOG_ERROR("Error calling Lua function: %s\n", lua_tostring(L, -1));
lua_setmetatable(L, -3); }
lua_pop(L, 1); // pop global table
} }
void smlua_push_integer_field(int index, char* name, lua_Integer val) { void smlua_push_integer_field(int index, char* name, lua_Integer val) {

View file

@ -198,7 +198,7 @@ void produce_interpolation_frames_and_delay(void) {
} }
f32 fps = frames / (clock_elapsed_f64() - sFrameTimeStart); f32 fps = frames / (clock_elapsed_f64() - sFrameTimeStart);
sAvgFps = sAvgFps * 0.6 + fps * 0.4; sAvgFps = sAvgFps * 0.95 + fps * 0.05;
sAvgFrames = sAvgFrames * 0.9 + frames * 0.1; sAvgFrames = sAvgFrames * 0.9 + frames * 0.1;
sFrameTimeStart = sFrameTargetTime; sFrameTimeStart = sFrameTargetTime;
sFrameTargetTime += sFrameTime * gGameSpeed; sFrameTargetTime += sFrameTime * gGameSpeed;