6
votes

I'm developing a game where I use (SFML) C++ for the core and Lua for the actor scripts. However, I'm running into some performance issues and I'm not sure what could be wrong. I've created a test program that shows the issue I'm having.

Basically, sometimes when I call a Lua function from C++, it takes much longer to return than usual. I need my game to run at 60 fps, and it does most of the time, but occasionally one or more of the function calls will take much longer than usual.

My first thought was that it was the memory manager, but turning it off didn't seem to get rid of the spikes. I know that there are several games that use Lua, and I imagine this is not a problem for them.

People have suggested that using LuaJIT could fix the issue, so I downloaded and set up LuaJIT (with lua 5.1). I got significantly improved average times, but the spikes were just as prevalent as ever:

enter image description here

Gallery of 2 examples of the console results (in microseconds; for reference a frame at 60 fps is ~16700 I think):

gc off gc disabled gc on gc enabled

C++ test program - http://pastebin.com/RhYnnLm3
Lua test script - http://pastebin.com/NBnAXcVD

2
What kind of hardware are you running on? My first guess would be the OS needs the CPU for some other purpose. - David Schwartz
What hardware information do you need? My processor is Intel Core i7 3.2 GHz - user3076190
Have you tried not calling lua at all? Are you sure it is indeed inside lua? If indeed it is coming from just lua running at all, then the next step would be to run a profiler, because that seems like pretty strange behavior. - kazagistar
Do you observe the same spiking patterns with a different timer, such as clock? - Ryan Stein
If it's not GC a classic thing that could be happening is that you keep appending to a table.. whenever it doubles (or so) you get a reallocation hit. - starmole

2 Answers

1
votes

It looks like you're experiencing LUAs garbage collector rolling in on some of your calls. Typically in games you want to pay very close attention to how the GC operates, and how often it runs. A simple solution may be to manually run the GC once per frame. Some popular game engines I know of do that. http://lua-users.org/wiki/GarbageCollectionTutorial

0
votes

The first suspect when having those spikes is the garbage collector. The easiest "fix" to try first is not disabling it, but rather calling it at predictable times.

Try calling collectgarbage() or collectgarbate('step') once just at the end of your frame processing.