Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 735
eonil Avatar asked Oct 31 '10 18:10

eonil


3 Answers

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

like image 92
deft_code Avatar answered Oct 14 '22 07:10

deft_code


The correct way to handle this is:

  1. Write a small prototype with just the core things that you want to test.
  2. Profile it a lot, reproducing the different scenarios that could happen in your game (lots of memory available, little memory available, different numbers of threads, that kind of thing)
  3. If you don't find a visible bottleneck, you can use Lua. Otherwise, you will have to look for alternative solutions (maybe Lisp or Javascript)
like image 3
kikito Avatar answered Oct 14 '22 07:10

kikito


You can patch the Lua GC so as to time-limit each collection cycle. E.g: http://www.altdevblogaday.com/2011/07/23/predictable-garbage-collection-with-lua/

I believe that it is still possible to have long GC step times when collecting very large tables. Therefore you need to adopt a programming style that avoids large tables.

The following article discusses two strategies for using Lua for real-time robot control (1. don't generate garbage, or 2. using an O(1) allocator and tune when GC collection is run): https://www.osadl.org/?id=1117

like image 2
Ross Bencina Avatar answered Oct 14 '22 05:10

Ross Bencina