This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jay5476's profile picture

Locals

Started by jay5476, 23 October 2013 - 09:48 PM
jay5476 #1
Posted 23 October 2013 - 11:48 PM
hi, i hope that i can teach some people about the benifits of using locals in your program

intro:
(this may be a bit to much for some to understand)
Well, in lua we have enviroments and in these enviroments we have all our variables
An enviroment is basiclly a table ( {ourVariable = "This is In My Enviroment" } ) set to a function ( files are called as functions ) .

When we run any program it is automaticlly setup to have its own enviroment which stores all of its variables, it also can acsess global( _G, for functions such as print() )
So if we have a program like

ourVariable = "some string i put here"
this will go into our enviroment and make ourVariable equal "some string i put here" but it will also leave it floating around for other functions and files to acsess which could break a program.

Now using locals we can make sure our variable only goes in our enviroment
example:

local ourVariable = "some string i put here"
this will go into our enviroment and make ourVariable equal "some string i put here" but it cannot be acsessed by other programs.

Now we can also use locals to make sure only that code block can acsess that variable
example:

local x = 1 -- local to our file
print(x) -- print the x local to the file
do -- start a code block
  local x = 5 -- define another x that is only local to the 'do' block
  print(x) -- print the x local to the 'do' block
end
print(x) -- if we print x again it will be the x that is local to the file
This shows that if locals cannot be used 'outside their scope' and this allows us to make them more 'private'

Local variables are also faster and they should be used as much as possible so that there wont be a need to worry about other programs interferring
Quotes
It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.

Reference: http://www.lua.org/pil/4.2.html
Given that large number of registers, the Lua precompiler is able to store all local variables in registers. The result is that access to local variables is very fast in Lua. For instance, if a and b are local variables, a Lua statement like a = a + b generates one single instruction: ADD 0 0 1  (assuming that a and b are in registers 0 and 1, respectively). For comparison, if both a and b were globals, the code for that addition would be like this:

GETGLOBAL 0 0 ; a
GETGLOBAL 1 1 ; b
ADD			   0 0 1
SETGLOBAL 0 0 ; a

So, it is easy to justify one of the most important rules to improve the performance of Lua programs: use locals!

Reference: http://www.lua.org/gems/sample.pdf (p.17)
MudkipTheEpic #2
Posted 24 October 2013 - 09:03 PM
One thing: Local variables aren't stored it the environment, they're stored in the call stack. (iirc)

local foo="bar"
print(getfenv()["foo"]==nil)
--#Output: true