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())
_CObjectPool = {}
_CObject = {
__index = function (t,k)
return _get_field(t['_lot'], t['_pointer'], k, t)
@ -7,11 +9,33 @@ _CObject = {
__newindex = function (t,k,v)
_set_field(t['_lot'], t['_pointer'], k, v, t)
end,
__tostring = function(t)
return 'CObject: ' .. t['_lot'] .. ', [' .. string.format('0x%08X', t['_pointer']) .. ']'
end,
__eq = function (a, b)
return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil
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 = {
__index = function (t,k)
return nil
@ -26,6 +50,23 @@ _CPointer = {
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 = {
__index = function (t,k)
local _table = rawget(t, '_table')

View File

@ -2,6 +2,8 @@
math.randomseed(get_time())
_CObjectPool = {}
_CObject = {
__index = function (t,k)
return _get_field(t['_lot'], t['_pointer'], k, t)
@ -9,11 +11,33 @@ _CObject = {
__newindex = function (t,k,v)
_set_field(t['_lot'], t['_pointer'], k, v, t)
end,
__tostring = function(t)
return 'CObject: ' .. t['_lot'] .. ', [' .. string.format('0x%08X', t['_pointer']) .. ']'
end,
__eq = function (a, b)
return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil
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 = {
__index = function (t,k)
return nil
@ -28,6 +52,23 @@ _CPointer = {
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 = {
__index = function (t,k)
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 e = gMarioStateExtras[m.playerIndex]
if (m.controller.buttonDown & Z_TRIG) ~= 0 then
mario_fire_flower_use(m)
end
-- decrease cooldown
if e.attackCooldown > 0 then
e.attackCooldown = e.attackCooldown - 1

View File

@ -1,5 +1,6 @@
char gSmluaConstants[] = ""
"math.randomseed(get_time())\n"
"_CObjectPool = {}\n"
"_CObject = {\n"
" __index = function (t,k)\n"
" return _get_field(t['_lot'], t['_pointer'], k, t)\n"
@ -7,10 +8,28 @@ char gSmluaConstants[] = ""
" __newindex = function (t,k,v)\n"
" _set_field(t['_lot'], t['_pointer'], k, v, t)\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"
" return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil\n"
" end\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"
" __index = function (t,k)\n"
" return nil\n"
@ -24,6 +43,20 @@ char gSmluaConstants[] = ""
" return a['_pointer'] == b['_pointer'] and a['_pointer'] ~= nil and a['_lvt'] ~= nil\n"
" end\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"
" __index = function (t,k)\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
smlua_cobject_allowlist_add(lot, (u64)(intptr_t)p);
lua_newtable(L);
int t = lua_gettop(L);
smlua_push_integer_field(t, "_lot", lot);
smlua_push_integer_field(t, "_pointer", (u64)(intptr_t)p);
lua_pushglobaltable(L);
lua_getfield(gLuaState, -1, "_CObject");
lua_setmetatable(L, -3);
lua_pop(L, 1); // pop global table
// get a cobject from a function
lua_getglobal(L, "_NewCObject"); // Get the function by its global name
lua_pushinteger(L, lot);
lua_pushinteger(L, (u64)(intptr_t)p);
if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
LOG_ERROR("Error calling Lua function: %s\n", lua_tostring(L, -1));
}
}
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);
lua_newtable(L);
int t = lua_gettop(L);
smlua_push_integer_field(t, "_lvt", lvt);
smlua_push_integer_field(t, "_pointer", (u64)(intptr_t)p);
lua_pushglobaltable(L);
lua_getfield(gLuaState, -1, "_CPointer");
lua_setmetatable(L, -3);
lua_pop(L, 1); // pop global table
// get a cpointer from a function
lua_getglobal(L, "_NewCPointer"); // Get the function by its global name
lua_pushinteger(L, lvt);
lua_pushinteger(L, (u64)(intptr_t)p);
if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
LOG_ERROR("Error calling Lua function: %s\n", lua_tostring(L, -1));
}
}
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);
sAvgFps = sAvgFps * 0.6 + fps * 0.4;
sAvgFps = sAvgFps * 0.95 + fps * 0.05;
sAvgFrames = sAvgFrames * 0.9 + frames * 0.1;
sFrameTimeStart = sFrameTargetTime;
sFrameTargetTime += sFrameTime * gGameSpeed;