Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua with C++ performance issues/lag spikes

Tags:

c++

lua

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

like image 284
user3076190 Avatar asked Dec 06 '13 22:12

user3076190


1 Answers

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

like image 127
Ryan Rohrer Avatar answered Sep 30 '22 17:09

Ryan Rohrer