Lag/Optimization help?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Lag/Optimization help?

Post by OmarShehata »

Ok so, I'm working on my game, everything nice and dandy until the lag monster shows up.

I am currently facing a specific optimization problem but any general optimization tips about Lua and Love would be greatly appreciated.

My contribution is this nifty little link I came across: http://trac.caspring.org/wiki/LuaPerfor ... :for-loops

My problem right now is in my animation system. What I have is a system where, I give it a Sheet.png and a Sheet.json, which is the pieces of an object, dissembled like so:

Image

And then an "Anim.json" file has the information of where each piece is supposed to go on each frame. The problem is that this animation update function is terribly laggy. I've confirmed that it is *not* from the amount of objects on screen, because simply commenting out the animation update removes the lag.

I can't really see anything too CPU heavy that I'm doing in my animation update step, so hopefully someone can offer some tips.

I've attached the file below. The loop, which if commented out the lag stops, starts on line 145. If you'd rather not download anything, here's a pastebin with the loop on its own (Although format is a bit messed up):

http://pastebin.com/mCGSJYmS

The difference between the game with this loop and without it is huge. With: ~9-15 fps. Without: ~110 fps.

I have a global animation array which I go through and call this update function:

Code: Select all

for i=1,#_AnimationArray,1 do 
          _AnimationArray[i]:Update(dt * _b2Factor)
end
Any help whatsoever would be appreciated. Maybe it's in the way I create my classes with metatables? Or is it in the fact that looping on a table with key-value using pairs() is significantly slower than looping through a table that is numerically indexed? (Although I tried numerically indexing my table instead, but that didn't *seem* to make too big of a difference)

I'm under the impression that Lua is a pretty fast language, although I haven't tried doing any tests myself, so I'm guessing I must have screwed up badly to make it lag this much.

Thanks in advance!

(Again, even if you just have general tips about optimizing with Love, that'd be great. Like whether rendering images with drawq is slower than rendering it normally, or any such things)
Attachments
SolidAnimation.lua
(9.61 KiB) Downloaded 115 times
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Lag/Optimization help?

Post by Nixola »

2 things:
1) could you upload a complete .love?
2) Did you try running it with LOVEjit?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
juno
Citizen
Posts: 85
Joined: Thu May 10, 2012 4:32 pm
Location: London

Re: Lag/Optimization help?

Post by juno »

Could be that you are executing several substring commands from within a nested loop. I imagine this would rack up a bit of processing power, especially if its parsing a large amount of text for each iteration.
wat ya mean she's in another castle!?
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Lag/Optimization help?

Post by Roland_Yonaba »

Actually, I second Juno. But as no .love is provided, I'm not that sure.
You might want to rehink the way you designed your code, because you're making a lot of operations on strings inside the main for loop. And, as you might already know, strings operations are quite expensive to compute.

I also recommend profiling your code. It can help you isolate what parts of the code takes time to compute and need to be optimized.
You can use luaprofiler or the excellent and very simple Perky's Profiler.
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: Lag/Optimization help?

Post by OmarShehata »

Roland_Yonaba wrote:Actually, I second Juno. But as no .love is provided, I'm not that sure.
You might want to rehink the way you designed your code, because you're making a lot of operations on strings inside the main for loop. And, as you might already know, strings operations are quite expensive to compute.
I...did not know that. This could very well be it. The reason I was using so many string operations is mainly because I allow the system so that, if the Anim json says "Face$" then this animation applies to "FaceHappy", "FaceSad", "FaceExcited" and anything with the prefix "face", but I think I can rewrite this another way. Thanks for pointing that out! I'll do that and report back here.
Nixola wrote:2 things:
1) could you upload a complete .love?
2) Did you try running it with LOVEjit?
I didn't know there was a LoveJit. I know there's a LuaJIT. I did some searching and I only found a thread talking about an unstable build someone hacked together himself. Would it be worth attempting to implement LuaJIT into Love...however that works?

Also about uploading the .love, I'm still a bit iffy about that, it doesn't have anything top secret but mostly because there's currently 28 code files and the only really relevant one is the one I uploaded. Thanks everyone for the replies so far!
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Lag/Optimization help?

Post by Roland_Yonaba »

OmarShehata wrote: [...] if the Anim json says "Face$" then this animation applies to "FaceHappy", "FaceSad", "FaceExcited" and anything with the prefix "face",[...]
Well, didn't you want to mean "^Face" instead of "Face$" ?
Cause character "^" anchors the pattern match at the beginning of a string, while "$" anchors it at the end..

Also, I think you should consider LuaJIT as the last option. That's my point, but in most of cases where the need for speed arises, it is likely to be related to the way the code is designed. So fix that before considering using a Just In Time compiler.
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: Lag/Optimization help?

Post by OmarShehata »

Roland_Yonaba wrote:
OmarShehata wrote: [...] if the Anim json says "Face$" then this animation applies to "FaceHappy", "FaceSad", "FaceExcited" and anything with the prefix "face",[...]
Well, didn't you want to mean "^Face" instead of "Face$" ?
Cause character "^" anchors the pattern match at the beginning of a string, while "$" anchors it at the end..

Also, I think you should consider LuaJIT as the last option. That's my point, but in most of cases where the need for speed arises, it is likely to be related to the way the code is designed. So fix that before considering using a Just In Time compiler.
I'm not actually using pattern anchors because flash doesn't allow "^" and several other of the special characters to be entered as symbol names, so I just thought it'd be simpler to just do it myself. I think a pretty easy solution to this would be to look for the correct object corresponding to the names while creating the game objects, and store them somewhere so I never manipulate any strings during the loop.

Also, I'm curious as to what drawbacks there would be to using a JIT compiler. Aside from it seemingly being much harder to use or build Love with it.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Lag/Optimization help?

Post by Roland_Yonaba »

OmarShehata wrote:Also, I'm curious as to what drawbacks there would be to using a JIT compiler. Aside from it seemingly being much harder to use or build Love with it.
If your game is Love 0.8.0-compatible, then have a look at slime's Love 0.8.0 JIT
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: Lag/Optimization help?

Post by OmarShehata »

Alright so I've removed those string functions, but that wasn't really enough. What made the biggest difference was re-arranging my loop. I realized I was looping n^2 times, where n is the number of game objects on screen, which was really stupid. So I fixed that.

And here is the current build without lag. Just thought you might want to see the fruits of your help :D


http://www.youtube.com/watch?feature=pl ... qtFQWYvEgk

Only thing left right now is to optimize the shader I have, which seems to be the source of the final piece of lag, for which I'll make a new thread.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 248 guests