Thread Tools Display Modes
08-09-12, 05:05 AM   #1
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Defining "local" and performance

How I see it, it's best to define the "local" variables as early as possible to save performance, by reusing what is already made room for. For example in loops I use local outside the loop, in functions I try to group all the variables in one local definition. My question is, is it actually saving anything in lua or?

Also is it better to have one local and a comma separated list of variables or doesn't it matter if it's several lines that use local? "local a, b, c" or "local a local b local c" e.g.

For example:
for ... do
local a
local b
if x then
local c
...
end
end

I would have written it like:
local a, b, c
<the code without "local" on the variables>

Just trying to figure out what is best or commonly acceptable. I am a bit uncertain since you can use a variable and save a int or a table in it, it's not like C where you can assign so many bytes then all the data you use is the same size or less, so that way it saves performance because it doesn't have to reallocate space all the time, while in lua the data sizes vary so much that I wonder if it helps at all having the locals at start, or if it doesn't mater if they are inside the loops and what not.

Also a question about lua formatting, is there some tool to help reformat a whole file of code? Let's say you got a big file and the new lines messed up so all is now in one line...
  Reply With Quote
08-09-12, 07:40 AM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
I looked up a thread on this subject that I read some time ago.
http://forums.wowace.com/showthread.php?t=17308
It states that
Code:
do
    for k,v in pairs(largeTable) do
        local var = someFunc(k,v)
        -- do something with var
    end
end
and
Code:
do
    local var
    for k,v in pairs(largeTable) do
        var = someFunc(k,v)
        -- do something with var
    end
end
are exactly the same, according to the generated bytecode by luac -l. The general consensus seems to be that there should never be any reason to concern yourself with performance in these situations, as long as you're not dealing with tables. The real difference is that defining it outside makes it available outside the scope; anything else is a matter of preference. Personally I tend to declare them inside as to not clutter the outside scope with an extra line, and being able to tell that it's a local at a glance.

And I don't actually know, but I'm willing to bet there's no difference at all between declaring locals in a list or separately.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
08-09-12, 09:30 AM   #3
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Lombra View Post
<snip>

And I don't actually know, but I'm willing to bet there's no difference at all between declaring locals in a list or separately.
There isn't - merely stylistic.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
08-09-12, 09:39 AM   #4
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Because since last year I've been writing all the local parts outside any loops thinking it's a performance boost, but it seems it doesn't matter so it was just making it "harder" to read what variable is in what scope, hehe.

Anyway thanks for clearing it up. Seems that thread has a lot of info, even p3lim posted there, hehe. In short what I took from it; "Don't concern yourself with memory for locals, it doesn't involve GC." and that you only need to concern yourself with tables, strings and OnUpdate code.
I guess I'll keep declaring the local outside my OnUpdate functions, and things like wiping a table clean we should use table.wipe(ref) when doing it inside a OnUpdate. Actually when I think of it OnUpdate is usually what lowers FPS or eats up CPU cycles, most "dangerous" kind of code to play with. :P

About performance, any good ways to be alerted when a function eats too much CPU? Not sure if there is a built in part in the debugging tools or not for this, hmm.

If anyone knows a tool that can help format one line of lua code that would be very helpful as well! :P

Last edited by Vlad : 08-09-12 at 09:57 AM.
  Reply With Quote
08-09-12, 01:22 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
You can use debugprofilestart() and debugprofilestop() to measure the time it takes for a block of code to run. debugprofilestart() effectively resets the internal timer to zero and a successive debugprofilestop() returns the current value of that timer. The value returned seems to be measured in milliseconds with nanosecond precision. Note there are one million nanoseconds in a millisecond or one billion nanoseconds in a second.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
08-09-12, 01:47 PM   #6
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
Originally Posted by Vladinator View Post
Also a question about lua formatting, is there some tool to help reformat a whole file of code? Let's say you got a big file and the new lines messed up so all is now in one line...
I'm using Code Chameleon but it's windows and payware.
  Reply With Quote
08-09-12, 04:55 PM   #7
Farmbuyer
A Cyclonian
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 43
Originally Posted by Vladinator View Post
Just trying to figure out what is best or commonly acceptable. I am a bit uncertain since you can use a variable and save a int or a table in it, it's not like C where you can assign so many bytes then all the data you use is the same size or less, so that way it saves performance because it doesn't have to reallocate space all the time, while in lua the data sizes vary so much that I wonder if it helps at all having the locals at start, or if it doesn't mater if they are inside the loops and what not.
If you're coming at this from a C background, then the answer is: Lua draws a sharper distinction between variables and values than what you may be used to.

- All variables are the same size. Values are of different sizes.
- Most variables have names. Values never do, not even functions.
- Variables themselves have no type. Values always have a single strong type (but some values can be implictly coerced to other types, which makes the type system look weaker than it really is). The "type(var)" function tells you what the type of the value held by the variable is.
- Variables refer to values. Specifically, variables hold simple types (nil, numbers, booleans, etc) internally, and hold a pointer to object types (tables, strings, functions etc).
- [Re-]assigning values to variables can be done at any time to any combination of variables and values. Size doesn't matter, honest!


Also a question about lua formatting, is there some tool to help reformat a whole file of code? Let's say you got a big file and the new lines messed up so all is now in one line...
If it's only the line endings, then any good text editor should be able to both (a) figure out what the current ending is and adjust its display accordingly, and (b) let you specify what ending to use when saving the file.

If it's more then the line endings, you might try good old GNU Indent and its reformatting modes.
  Reply With Quote
08-09-12, 05:43 PM   #8
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Thanks for all the replies!

Also it's not just a endings issue, the endings got stripped out, so did all double+ spaces, hehe. :P
  Reply With Quote
08-11-12, 09:48 PM   #9
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Originally Posted by Dridzt View Post
I'm using Code Chameleon(Link Removed) but it's windows and payware.
AVG just alerted me that site contains the threat Blackhole Exploit Kit (type 2315).
Not sure if it's real or just another false positive.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
08-12-12, 05:38 AM   #10
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by Vladinator View Post
Also a question about lua formatting, is there some tool to help reformat a whole file of code? Let's say you got a big file and the new lines messed up so all is now in one line...
Originally Posted by Farmbuyer View Post
If it's only the line endings, then any good text editor should be able to both (a) figure out what the current ending is and adjust its display accordingly, and (b) let you specify what ending to use when saving the file.

If it's more then the line endings, you might try good old GNU Indent and its reformatting modes.
Originally Posted by Dridzt View Post
I'm using Code Chameleon but it's windows and payware.
I'm not sure actually what you all are talking about, but this got me curious. Do you all mean something that formats/autoindents a bunch of obfuscated/compressed code?

Is there also anything similar to http://jsbeautifier.org/ but then for Lua, or any other (scripting) languages?
  Reply With Quote
08-12-12, 05:40 AM   #11
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Ketho, it's like the jsbeautifier, that's what I am after. Considering to make my own at this point, if I can manage that. So far I don't see anything like this for lua.
  Reply With Quote
08-12-12, 12:32 PM   #12
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
Originally Posted by Vladinator View Post
Ketho, it's like the jsbeautifier, that's what I am after. Considering to make my own at this point, if I can manage that. So far I don't see anything like this for lua.
I wrote one a while back to play with obfuscated code back when that was allowed. It doesn't preserve comments though!
https://sites.google.com/site/wowsai.../LuaFormat-5-1
I only built it for Win32 x86, but there's source code included on the same page if you need to compile it yourself.
  Reply With Quote
08-12-12, 04:08 PM   #13
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Thank you Saiket, you always make great utilities! :P
  Reply With Quote
08-19-12, 02:51 AM   #14
brykrys
A Deviate Faerie Dragon
 
brykrys's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 13
Originally Posted by Lombra View Post
I looked up a thread on this subject that I read some time ago.
http://forums.wowace.com/showthread.php?t=17308
It states that
Code:
do
    for k,v in pairs(largeTable) do
        local var = someFunc(k,v)
        -- do something with var
    end
end
and
Code:
do
    local var
    for k,v in pairs(largeTable) do
        var = someFunc(k,v)
        -- do something with var
    end
end
are exactly the same, according to the generated bytecode by luac -l. <snip>
I had to try this, of course, and discovered that they do generate different byte code:

The 2nd version with local var declared outside the loop is 1 opcode longer, and takes up an extra register slot - there's an extra MOVE instruction inside the loop.
Whether this makes a non-trivial difference probably depends on what else you do inside the loop, though.
  Reply With Quote
08-19-12, 08:31 AM   #15
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Yeah, I actually tried it too (before I posted that), and noticed a slight difference. I just repeated what was said in the thread, and I don't know if things have changed since (I guess not) or we misunderstood each other/the results.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
08-19-12, 08:47 AM   #16
Tuller
A Warpwood Thunder Caller
 
Tuller's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 91
That's one of those cases where I'd write
Lua Code:
  1. do
  2.     for k,v in pairs(largeTable) do
  3.         local var = someFunc(k,v)
  4.         -- do something with var
  5.     end
  6. end
regardless, since var is probably only intended to exist in the scope of the for loop.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Defining "local" and performance


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off