titan-lang / titan

The Titan programming language

Home Page:http://titan-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  1. 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

  2. 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

  3. More aggressively put temporaries out of scope with more pushd/popd pairs in the code

  4. 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

  5. Implement a checkpoint function that generates stores for all variables in the intersection of the dirty and visible sets, and call this function on the places where GC might occur: allocating a new string or table, calling a function...

  6. Profit! 😄