Lua with C ++ performance issues / lagging spikes

I am developing a game in which I use C ++ (SFML) for the kernel and Lua for actor scripts. However, I run into some performance issues, and I'm not sure what might be wrong. I created a test program that shows the problem I am having.

Basically, sometimes when I call a Lua function from C ++, it takes a lot more time to return than usual. I need my game to run at 60 frames per second, and this happens most of the time, but sometimes one or more function calls take much longer than usual.

My first thought was that it was a memory manager, but disabling it did not seem to get rid of the spikes. I know that there are several games that use Lua, and I believe that this is not a problem for them.

People suggested that using LuaJIT might solve the problem, so I downloaded and installed LuaJIT (with lua 5.1). I significantly improved the average time, but the spikes were as common as before:

enter image description here

Gallery of 2 examples of console results (in microseconds, for reference: a frame at a speed of 60 frames per second is ~ 16700):

gc off gc disabled gc on gc enabled

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

+6
source share
2 answers

It looks like you are facing a LUAs garbage collector that goes into some of your calls. As a rule, in games you should pay very close attention to how the GC works and how often this happens. A simple solution may be to manually start the GC once for each frame. Some popular game engines that I know about this. http://lua-users.org/wiki/GarbageCollectionTutorial

+1
source

The first suspect in the presence of these spikes is the garbage collector. The simplest “fix” is to try first, not turn it off, but rather call it at a predictable time.

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

0
source

Source: https://habr.com/ru/post/959501/


All Articles