Implementing something like CPython's gc.freeze()
in the C API?
#1301
Replies: 7 comments 2 replies
-
Hmm, now that I look at it, |
Beta Was this translation helpful? Give feedback.
-
I got something basic working (not in PR-able form,) and the results are promising. Using my Branch is here https://github.com/luau-lang/luau/compare/master...HaroldCindy:luau:experiments/gc_freezing?expand=1. One thing I'm having trouble with is marking |
Beta Was this translation helpful? Give feedback.
-
@petrihakkinen This may be of interest, since i was initially looking at what would be involved in implementing your Light C Function suggestion, and arrived at this as a less invasive and perhaps more upstreamable solution.
While the alloc overhead is of course unavoidable without something like light C functions, a strategy of marking "simple" C functions and their enclosing (sandboxed / readonly) modules as fixed and avoiding traversing them in the GC seems like a viable avenue of reducing the associated GC overhead. |
Beta Was this translation helpful? Give feedback.
-
Made a little heap graphing utility that outputs to graphviz using the |
Beta Was this translation helpful? Give feedback.
-
Did some graph generation to show the paths that would have to be traversed during marking. I eliminated any fixed nodes that didn't have a downstream reference to a non-fixed object (other than function envs,) since they wouldn't have to be traversed. The results look promising, now that Green-bordered objects are fixed, red-bordered objects are unfixed. native_userdata.lua with
|
Beta Was this translation helpful? Give feedback.
-
This is interesting. I'll try to find some time to test this in a game project. Note that this not only decreases the GC overhead of C functions but also fixed strings which can be significant for some applications. For example, we use string atoms for enums in our binding layer and all those strings live forever... |
Beta Was this translation helpful? Give feedback.
-
@HaroldCindy we don't have plans to support/accept freezing objects with references (only string are supported). |
Beta Was this translation helpful? Give feedback.
-
My list of GC'd objects is generally dominated by
Proto
s andClosure
s that are always added to the script environment by my binding code. I add a lot of helper functions, and GC traversal time for them adds up, even though they will never be collected, and they should never have references to data that should be collected before state destruction (I've removedsetfenv()
and they have no GC'd upvalues.)CPython has a
gc.freeze()
function that moves all objects at the time of calling to a "permanent generation" where the objects are never traversed or considered for GC. That model fits well with a generational GC, but doing this with Luau's GC seems less trivial. It seems that I could mark walk the heap and mark all protos and closures with thefixedbit
used for strings, and add a guard against traversing such objects inreallymarkobject()
andpropagatemark()
. Ideally that would allow me to ignore any proto from the "initial" script state, but allow collecting protos generated byloadstring()
and such later on.I'm concerned about the validity of this approach given the various
gclist
fields andg->gray
andg->grayagain
lists. Is there a sensible way to reconstruct those linked lists once I've "frozen" everything, is there a better approach? I already have a traversal function for all GC'd objects that can mark allProto
s andClosure
s as fixed appropriately.Beta Was this translation helpful? Give feedback.
All reactions