diff --git a/main.lua b/main.lua index 43215d5..c806814 100644 --- a/main.lua +++ b/main.lua @@ -53,7 +53,7 @@ function love.load() up = {'key:up', 'key:w'}, down = {'key:down', 'key:s'}, action = {'key:z', 'key:space'}, - f1 = {'key:f1'}, f2 = {'key:f2'}, f3 = {'key:f3'} + f1 = {'key:f1'}, f2 = {'key:f2'}, f3 = {'key:f3'}, f12 = {'key:f12'} }, pairs = { move = {'left', 'right', 'up', 'down'} @@ -69,7 +69,16 @@ function love.update(dt) input:update(dt) if input:pressed('f1') then gotoRoom('CircleRoom') end if input:pressed('f3') then camera:shake(4, 60, 1) end - + if input:pressed('f12') then + print("-------------------------------------") + print("Before collection: " .. collectgarbage("count")/1024) + collectgarbage() + print("After collection: " .. collectgarbage("count")/1024) + print("Object count: ") + local counts = type_count() + for k, v in pairs(counts) do print(k, v) end + print("-------------------------------------") + end camera:update(dt) end @@ -151,3 +160,56 @@ function random(min, max) local min, max = min or 0, max or 1 return (min > max and (love.math.random()*(min - max) + max)) or (love.math.random()*(max - min) + min) end + +------------------------ +-- garbage collection -- +------------------------ + +---Counts all globals and applies function +---@param f function +function count_all(f) + local seen = {} + local count_table + count_table = function(t) + if seen[t] then return end + f(t) + seen[t] = true + for k,v in pairs(t) do + if type(v) == "table" then + count_table(v) + elseif type(v) == "userdata" then + f(v) + end +end + end + count_table(_G) +end + +---Count all types of globals +---@return table +function type_count() + local counts = {} + local enumerate = function (o) + local t = type_name(o) + counts[t] = (counts[t] or 0) + 1 + end + count_all(enumerate) + return counts +end + +global_type_table = nil +---Resolves an object's true class name via metatable lookup. +---Caches results in `global_type_table` for speed. +---Fallbacks: "table" (no metatable), "Unknown" (uncached). +---@param o any Any object (table, userdata, etc.) +---@return string Class name (e.g., "Player", "Camera", "ImageData") +function type_name(o) + if global_type_table == nil then + global_type_table = {} + for k,v in pairs(_G) do + global_type_table[v] = k + end +global_type_table[0] = "table" + end + return global_type_table[getmetatable(o) or 0] or "Unknown" +end