mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Improve cleanup() algorithm
This commit is contained in:
parent
154cf77df6
commit
bf1271de23
3 changed files with 75 additions and 32 deletions
|
|
@ -1,47 +1,56 @@
|
||||||
if not game then script = require "test/relative-string" end
|
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 ref_to_id = {} :: { [string]: number }
|
||||||
local cleanup_callbacks = {} :: { [number]: () -> () }
|
local id_to_ref = {} :: { [number]: string }
|
||||||
local cleanup_callers = {} :: { [number]: () -> () }
|
local cleanup_callbacks = {} :: { [number]: () -> () } -- always dense
|
||||||
|
local cleanup_callers = {} :: { [number]: () -> () } -- can be sparse
|
||||||
setmetatable(cleanup_callers :: any, { __mode = "vs" })
|
setmetatable(cleanup_callers :: any, { __mode = "v" })
|
||||||
|
|
||||||
-- todo: rare case where mem address is reused by another function on same line
|
-- todo: rare case where mem address is reused by another function on same line
|
||||||
|
-- does this case handle itself?
|
||||||
|
|
||||||
local function cleanup(callback: () -> ())
|
local function cleanup(callback: () -> ())
|
||||||
local caller = debug.info(2, "f") :: () -> ()
|
local caller = debug.info(2, "f") :: () -> ()
|
||||||
local line = debug.info(2, "l") :: number
|
local line = debug.info(2, "l") :: number
|
||||||
local ref = tostring(caller) .. "\0" .. line
|
local ref = tostring(caller) .. line
|
||||||
|
|
||||||
local id = ref_to_id[ref]
|
local id = ref_to_id[ref]
|
||||||
|
|
||||||
if id then
|
if id then
|
||||||
cleanup_callbacks[id]()
|
cleanup_callbacks[id]()
|
||||||
else
|
else
|
||||||
id = table.remove(free_ids) or #cleanup_callbacks + 1
|
id = #cleanup_callbacks + 1
|
||||||
id_to_ref[id :: any] = ref -- todo
|
|
||||||
ref_to_id[ref] = id
|
ref_to_id[ref] = id
|
||||||
|
id_to_ref[id :: any] = ref -- todo
|
||||||
cleanup_callers[id :: any] = caller -- todo
|
cleanup_callers[id :: any] = caller -- todo
|
||||||
end
|
end
|
||||||
|
|
||||||
cleanup_callbacks[id] = callback
|
cleanup_callbacks[id] = callback
|
||||||
end
|
end
|
||||||
|
|
||||||
-- todo: investigate behavior if cleanup called within cleanup
|
|
||||||
-- todo: verify no memory leakage
|
|
||||||
|
|
||||||
local function clean_garbage()
|
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
|
if cleanup_callers[id] == nil then -- caller was garbage collected
|
||||||
local callback = cleanup_callbacks[id]
|
local callback = cleanup_callbacks[id]
|
||||||
|
|
||||||
cleanup_callbacks[id] = nil
|
do -- swap and pop
|
||||||
table.insert(free_ids, id)
|
local max_id = #cleanup_callbacks
|
||||||
ref_to_id[id_to_ref[id]] = nil
|
|
||||||
id_to_ref[id] = nil
|
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)
|
local ok, err: string? = pcall(callback)
|
||||||
if not ok then warn(`error occured during cleanup: {err}`) end
|
if not ok then warn(`error occured during cleanup: {err}`) end
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,18 @@
|
||||||
local BENCH, START = require("test/testkit").benchmark()
|
local BENCH, START = require("test/testkit").benchmark()
|
||||||
|
|
||||||
|
-- try prevent inlining by wrapping in a closure referencing an upvalue that
|
||||||
|
-- cannot be determined at compile-time
|
||||||
|
local function NO_INLINE(fn)
|
||||||
|
local r = math.random()
|
||||||
|
return function(x)
|
||||||
|
local _ = r
|
||||||
|
fn(x)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local vide = require "src/init"
|
local vide = require "src/init"
|
||||||
|
|
||||||
local N = 2^18 -- 262144
|
local N = 2^18 -- 262144[
|
||||||
|
|
||||||
BENCH("create state", function()
|
BENCH("create state", function()
|
||||||
local cache = table.create(N)
|
local cache = table.create(N)
|
||||||
|
|
@ -247,51 +257,61 @@ BENCH("values() all remove", function()
|
||||||
state(data)
|
state(data)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
BENCH("register cleanup", function()
|
BENCH("register new cleanup", function()
|
||||||
local cleanup = vide.cleanup
|
local cleanup = vide.cleanup
|
||||||
|
|
||||||
|
local cleaner = function() end
|
||||||
|
|
||||||
local callers = {}
|
local callers = {}
|
||||||
|
|
||||||
for i = 1, N do
|
for i = 1, N do
|
||||||
callers[i] = function(fn, v)
|
callers[i] = function(fn, v)
|
||||||
fn(v)
|
fn(v)
|
||||||
return i
|
return i -- return unique upvalue to ensure unique closure
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = 1, START(N) do
|
for i = 1, START(N) do
|
||||||
callers[i](cleanup, function() end)
|
callers[i](cleanup, cleaner)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- cleanup cleanups from previous benchmark
|
||||||
|
;(collectgarbage :: any)("collect")
|
||||||
|
vide.step(0)
|
||||||
|
|
||||||
BENCH("repeat cleanup", function()
|
BENCH("repeat cleanup", function()
|
||||||
local cleanup = vide.cleanup
|
local cleanup = vide.cleanup
|
||||||
|
|
||||||
local function foo(i)
|
local foo = NO_INLINE(function(i)
|
||||||
cleanup(function() -- todo: why is this not causing allocations?
|
cleanup(function()
|
||||||
return i
|
-- return i -- uncomment to include overhead of closure creation
|
||||||
end)
|
end)
|
||||||
end
|
end)
|
||||||
|
|
||||||
for i = 1, START(N) do
|
for i = 1, START(N) do
|
||||||
foo(i)
|
foo(i)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- todo: this is sometimes never terminates
|
-- cleanup cleanups from previous benchmark
|
||||||
|
;(collectgarbage :: any)("collect")
|
||||||
|
vide.step(0)
|
||||||
|
|
||||||
BENCH("cleanup step", function()
|
BENCH("cleanup gc check", function()
|
||||||
local cleanup = vide.cleanup
|
local cleanup = vide.cleanup
|
||||||
|
|
||||||
local ref = table.create(N)
|
local cleaner = function() end
|
||||||
|
|
||||||
|
local callers = table.create(N)
|
||||||
|
|
||||||
for i = 1, N do
|
for i = 1, N do
|
||||||
ref[i] = function()
|
callers[i] = function()
|
||||||
cleanup(function() end)
|
cleanup(cleaner)
|
||||||
return i
|
return i
|
||||||
end
|
end
|
||||||
|
|
||||||
ref[i]()
|
callers[i]()
|
||||||
end
|
end
|
||||||
|
|
||||||
START(N)
|
START(N)
|
||||||
|
|
@ -299,4 +319,10 @@ BENCH("cleanup step", function()
|
||||||
vide.step(0)
|
vide.step(0)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
BENCH("cleanup gc removal", function()
|
||||||
|
START(N)
|
||||||
|
|
||||||
|
vide.step(0) -- cleanup from previous benchmark
|
||||||
|
end)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -529,6 +529,14 @@ TEST("cleanup()", function()
|
||||||
|
|
||||||
do
|
do
|
||||||
stateA = nil :: any
|
stateA = nil :: any
|
||||||
|
gc()
|
||||||
|
vide.step(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
CHECK(objA.cleaned == 2)
|
||||||
|
CHECK(objB.cleaned == 1)
|
||||||
|
|
||||||
|
do
|
||||||
stateB = nil :: any
|
stateB = nil :: any
|
||||||
gc()
|
gc()
|
||||||
vide.step(0)
|
vide.step(0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue