Page 1 of 1
What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 17:09
by smoth
I would like to run some checks on my gadgets and widgets. How can I check their cost?
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 17:45
by zwzsg
There is, or was, a gadget profiler in CA. Once I tried to port it to KP, but I failed because CA handler was too different from default handler (for instance it wouldn't call GameStart). I suppose with a little work, CA handler with profiler could be merged with a more standard gadget handler. Or take the principle of CA's profiler and reimplement it in your own handler.
The widget profiler you can find with forum search may still work for every mod on the other hand.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 18:20
by very_bad_soldier
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 19:25
by jK
CA has a gadget profiler (synced and unsynced) & a LuaUI profiler.
The usage of the LuaUI profiler is easy.
And the gadget profiler works via "/luarules profile" & "/luarules uprofile".
PS: the widgetprofiler by Meltrax (the one linked by vbs) just covers a few callins, while the CA one covers all.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 19:51
by FLOZi
The 'idiots-guide to writing efficient lua' is here:
http://trac.caspring.org/wiki/LuaPerformance
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 19:56
by zwzsg
Optimising is not about applying magic recipes. It's about identifying the bottlenecks.
And once you find it, rethinking your algorithm will give you better performance gain than those small tricks anyway.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 20:00
by aegis
identifying bottlenecks is a different part of optimization
you can still apply the performance hacks for a potential several hundred percent improvement
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 20:01
by FLOZi
Some bottlenecks are unavoidable, the best you can do is spread the load out over multiple frames.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 20:03
by zwzsg
aegis: Not really, because if you miss the bottleneck, you can optimising the rest all you want, it won't change a thing, since by definition it's the bottleneck that slows you down.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 20:27
by FLOZi
It'd be useful to also have a list of Call-in's which are particularly performance heavy.
[19:17:14] <[S44]FLOZi> hmmm, could rewrite the ammo gadget to use ProjectileCreated callin
[19:18:13] <[LCC]jK> bad idea
[19:18:26] <[LCC]jK> that's a very very performance critical callin
[19:18:35] <[LCC]jK> once you use it it hurts your fps
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 21:07
by Tobi
That's simple.
If it's called often, it's performance heavy.
ProjectileCreated is called for every single projectile that is created. So that could be many, many times in a single frame.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 21:08
by aegis
zwzsg, you can optimize some operations to get a decent performance boost. I have no idea why you would suggest AGAINST this.
you want drawing code executed every frame to be as fast as possible.
if you optimize the algorithms used in your bottlenecks and get a 2x speedup, that's great.
you should STILL optimize oft-used badly-performing calls (which can in fact be inside your bottlenecks!) if you can get another 2x.
following the lua performance guide offers an easy performance improvement. there's no feasible reason why anyone shouldn't apply it.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 21:25
by Tobi
If you have a single bottleneck that takes 90% of your CPU, and you waste all your time optimizing and tweaking the other 10% CPU usage, then no matter how good you are you are not going to make it more than 10% faster.
However, if you leave all your other code alone and just optimize the bottleneck a little, say 25%, then you already optimized the whole thing 22.5%!
Of course I am not arguing against using a speedier way to code something in the first place, but
only as long as it does not reduce readability. As soon as you have a tradeoff between speed and readability,
always choose readability, until you have measured that that particular code is a bottleneck.
I think that is also the point zwzsg is making

Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 22:04
by FLOZi
Tobi wrote:That's simple.
If it's called often, it's performance heavy.
ProjectileCreated is called for every single projectile that is created. So that could be many, many times in a single frame.
Does it not use Script.SetWatchWeapon like the Explosion() callin does? I guess there are some non-weapon projectiles like flying unit pieces.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 22:18
by Tobi
Hmm I don't know that, might be the case.
Re: What are some tools to benchmark lua scripts?
Posted: 29 Sep 2010, 22:18
by TheFatController
I wonder if LuaJIT would detect that most of these loops did nothing and execute them once or not at all
Re: What are some tools to benchmark lua scripts?
Posted: 30 Sep 2010, 00:08
by Argh
Does it not use Script.SetWatchWeapon like the Explosion() callin does? I guess there are some non-weapon projectiles like flying unit pieces.
No, it doesn't. It's called every time a Projectile, including Pieces, is created. My experience is that so long as you're not doing major stuff there, it's fine to use. If you need to do major logic, use if-->then to filter out most things, ofc.