Post Mortem: The Maw - Lua & LuaBind

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Post Mortem: The Maw - Lua & LuaBind

Post by trufun202 »

I'm not sure if any of you subscribe to Game Developer Magazine, but each month they publish a Post Mortem for a popular/current game in the industry. Each post mortem gives an overall summary of the project, then lists 5 things that went right, and 5 things that went wrong. (I actually started doing this for my team's projects at work)

A month or so ago, they covered "The Maw", an indie game created by 7 developers for Xbox Live Arcade. One of the 5 things that went wrong was their Lua and LuaBind implementation.

I thought I'd share it because it's an interesting read, but I'm kinda surprised they thought they could get away with having a separate Lua VM for EVERY object in-game. :shock:
Despite the great benefits of using Lua and Luabind in our engine has given us, it took us a few tries to get them working smoothly in our engine. Using Lua and Luabind for all of our gameplay, UI, and generally everything specific to The Maw has been a great help in development. It has helped shorten our iteration time and made it so that some of our designers are really doing the work of programmers. The flexibility and ease of use of Lua combined with the ease of binding C++ classes to Lua using Luabind really put the power of creating the game into the designer's hands. The problems came once we started paying attention to what was happening in memory.

At first, we went with a design that gave each actor its own virtual machine to run Lua. Our plan with this design was to keep each actor in its own isolated environment in order to enforce that all actors would communicate with each other only through our messaging system. This proved to be a mistake because there is a certain amount of overhead with having each VM plus there's the fact that many of the Lua scripts were duplicated across VMs which wasted alot of memory. The other big issue was the performance hit caused by having to run the garbage collector on each VM.

We realized halfway through the project that these issues needed to be addressed so we reworked our scripting system so that all actors and entities in the game used the same VM. This was a great boon to performance and memory usage; however there was still more work to be done. Because we had so many objects in the same VM, it took even longer for the garbage collector to check all of the objects for collection and perform the necessary work to clean up dead objects, not to mention all of the fragmentation caused by all of this.

To address this issue, we did some logging to see what was causing the bulk of the memory allocations and with a fairly small effort we were able to rework our scripts a bit so that many fewer allocations were occurring each frame. We went from about 350-450 allocations made by Lua and Luabind per frame to roughly 0 to 15 depending on what was going on at any given time. Obviously, this is a great improvement, but it still caused some issues.

We still had to run the garbage collector when too much memory was in use by Lua, even though this could easily cause a 40 second or more hang while the garbage collector did it's work. Obviously, this is unacceptable when a user is playing the game. We tried several things to help improve this, finally implementing a solution where we made modifcations to Lua's garbage collector so that it continuously runs on another of the Xbox 360's hardware threads, taking into account thread safety and minimizing the number of required critical sections.

We still had an issue with fragmentation, but roping off an area of memory just for use by Lua safely kept the fragmentation from contaminating the rest of the system.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Post Mortem: The Maw - Lua & LuaBind

Post by dandymcgee »

That's pretty interesting, post mortems make it easier to learn from other people's mistakes and successes. I read the one for Diablo II on GameDev.net. They've got a whole collection of em over there.
Last edited by dandymcgee on Tue Feb 17, 2009 2:31 pm, edited 1 time in total.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Post Mortem: The Maw - Lua & LuaBind

Post by Falco Girgis »

No offense to them, because they did make the game, but their scripting implementation sounds noobish. I can't believe that they even considered using a separate thread for every entity.

I think that the group of us who have been successfully and efficiently using Lua (me, M_D_K, trufun, sparda?, and the rest of us) should feel good about ourselves. What they did honestly sounds sloppy.
wacko
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 12
Joined: Sat Oct 25, 2008 1:36 am

Re: Post Mortem: The Maw - Lua & LuaBind

Post by wacko »

I do think having a new allocation of the lua VM for each entity was the wrong thing to do... as it generally does not work well that way, but pushing the GC out to another xbox thread is not unreasonable. Most games do not even use all the threads available to them so why not, does not affect the main thread with the GC.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Post Mortem: The Maw - Lua & LuaBind

Post by Falco Girgis »

Actually. Looking at my post, that's what I meant to say. Yeah, we use separate threads for everything. What I meant to say was how STUPID using a separate VM for everything was.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Post Mortem: The Maw - Lua & LuaBind

Post by M_D_K »

wacko wrote:I do think having a new allocation of the lua VM for each entity was the wrong thing to do... as it generally does not work well that way, but pushing the GC out to another xbox thread is not unreasonable. Most games do not even use all the threads available to them so why not, does not affect the main thread with the GC.
For hundreds of entities, now Lua has got a small footprint but that is a bit retarded. If they wanted to move the GC to another thread they should make a main lua state in another thread and set it up that way. Then have all entities run as co-routines(lua threads). Its a lot smarter to do that, then chew up memory on a shitload of VMs.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
Post Reply