Improve cleanup() algorithm

This commit is contained in:
Aaron Smith 2023-08-15 17:54:54 +01:00
parent 154cf77df6
commit bf1271de23
3 changed files with 75 additions and 32 deletions

View file

@ -1,47 +1,56 @@
if not game then script = require "test/relative-string" end
-- todo: verify correct behavior in non-standard usage
local free_ids = {} :: { number }
local id_to_ref = {} :: { [number]: string }
local ref_to_id = {} :: { [string]: number }
local cleanup_callbacks = {} :: { [number]: () -> () }
local cleanup_callers = {} :: { [number]: () -> () }
setmetatable(cleanup_callers :: any, { __mode = "vs" })
local id_to_ref = {} :: { [number]: string }
local cleanup_callbacks = {} :: { [number]: () -> () } -- always dense
local cleanup_callers = {} :: { [number]: () -> () } -- can be sparse
setmetatable(cleanup_callers :: any, { __mode = "v" })
-- todo: rare case where mem address is reused by another function on same line
-- does this case handle itself?
local function cleanup(callback: () -> ())
local caller = debug.info(2, "f") :: () -> ()
local line = debug.info(2, "l") :: number
local ref = tostring(caller) .. "\0" .. line
local ref = tostring(caller) .. line
local id = ref_to_id[ref]
if id then
cleanup_callbacks[id]()
else
id = table.remove(free_ids) or #cleanup_callbacks + 1
id_to_ref[id :: any] = ref -- todo
id = #cleanup_callbacks + 1
ref_to_id[ref] = id
id_to_ref[id :: any] = ref -- todo
cleanup_callers[id :: any] = caller -- todo
end
cleanup_callbacks[id] = callback
end
-- todo: investigate behavior if cleanup called within cleanup
-- todo: verify no memory leakage
local function clean_garbage()
for id = 1, #cleanup_callbacks do
for id = #cleanup_callbacks, 1, -1 do
if cleanup_callers[id] == nil then -- caller was garbage collected
local callback = cleanup_callbacks[id]
cleanup_callbacks[id] = nil
table.insert(free_ids, id)
ref_to_id[id_to_ref[id]] = nil
id_to_ref[id] = nil
do -- swap and pop
local max_id = #cleanup_callbacks
cleanup_callbacks[id] = cleanup_callbacks[max_id]
cleanup_callbacks[max_id] = nil
cleanup_callers[id] = cleanup_callers[max_id]
cleanup_callers[max_id] = nil
local ref = id_to_ref[id]
local max_ref = id_to_ref[max_id]
id_to_ref[id] = max_ref
id_to_ref[max_id] = nil
ref_to_id[max_ref] = id
ref_to_id[ref] = nil
end
local ok, err: string? = pcall(callback)
if not ok then warn(`error occured during cleanup: {err}`) end