Sink stores to Lua stack to GC checkpoints
mascarenhas opened this issue · comments
Right now the Titan compiler pessimistically stores in the Lua stack any GC-able reference (references to strings and arrays) that it has stored in a native variable so the Lua garbage collector is assured to find them, but we can defer this store while we are sure no GC will occur, in the hope that by the time a GC can occur some of these references have gone out of scope and do not have to be stored to the Lua stack anymore.
To implement this store sinking, we need:
-
To track visibility of variables and temporaries in the code generator (no need for a full blown symbol table, a simple stack of
visible
sets will do -
To track variables and temporaries that are
dirty
, i.e., its current value (the GC reference) changed but the new value has not been stored in the Lua stack -
More aggressively put temporaries out of scope with more
pushd/popd
pairs in the code -
Change all places in the code generator where we store the current value of a GC-ed variable or temporary in the Lua stack to mark it as dirty instead
-
Implement a
checkpoint
function that generates stores for all variables in the intersection of thedirty
andvisible
sets, and call this function on the places where GC might occur: allocating a new string or table, calling a function... -
Profit!
😄