Search code examples
garbage-collectionluareal-time

Lua's GC and realtime game


As I know, the tracing GC can't avoid thread blocking during complete GC.

I had used XNA+C#, and GC time were impossible to remove. So I switched to lower level language C, but I realized I need scripting language. I'm considering Lua, but I'm worrying about Lua's GC mechanism. Lua is using incremental tracing GC, and thread blocking should be too.

So how should I handle this in realtime game?


Solution

  • The power of Lua is that it gets out of your way. Want classes? That can be build with metatables. Want sandboxing? use lua_setfenv.

    As for the garbage collector. Use it as is first. If later you find performance issues use lua_gc to fine tune its behavior.

    Some examples:

    • Disable the garbage collector during those times when the slow down would be a problem.

    • Leave the garbage collector disabled and only step it when game logic says you've got some head room on your FPS count. You could pre-tune the step size, or discover the optimal step size at runtime.

    • Disable the collector and perform full collection at stopping points, ie a load screen or cut scene or at turn change in a hot seat game.

    You might also consider an alternative scripting language. Squirrel tries very hard to be a second generation Lua. It tries to keep all of Lua's good features, while ditching any of its design mistakes. One of the big differences between the two is squirrel uses reference counting instead of garbage collection. It turns out that reference counting can be a bit slower than garbage collection but it is very deterministic(AKA realtime).