6

Any awesome wm module starts from redefinition standard variables to local. Something like that

local table = table
local string = string
local tostring = tostring

What does it do? All code still working fine after deleting this lines.

2 Answers2

5

It's purely an optimization thing. Local variables are faster to read/write than global variables. This is in part because globals are hash table lookups (e.g. foo => _G["foo"]) and locals are VM register lookups. So it's not uncommon for modules that are going to be using a global a lot to alias it via a local variable.

For your code, unless you know something is going to be called a ton and is going to be a bottleneck, I wouldn't bother with this technique. Lua isn't C. You're trading performance for brevity and clarity. Don't trade it back until you know you have to.

Mud
  • 28,277
  • 11
  • 59
  • 92
  • I agree with everything here except the "don't do it because trade-off" bit. It doesn't, in any way beyond this initial question, hurt clarity, and only marginally harms brevity (especially when multiple assignments are on the same line) and often does have real measurable (though usually small) impact in code that is run often or in a loop. It also helps ingrain the pattern of using locals over globals which, in general, avoids many potential pitfalls and problems. – Etan Reisner Apr 01 '14 at 01:16
  • @EtanReisner If you're worrying about small performance increases in non-critical-path Lua code, you're using the wrong language. The vast majority of Lua code should not be aliasing all the library routines they use into locals. The logical conclusion of that mindset is avoiding, say, `s:gsub(...)` because not only is it a table lookup, it's a *metatable* lookup (oh dear!), so one should write 'local gsub = string.gsub;` at the top of your file then `gsub(s,...)`, right? No. You should be focused on solving your problem, then clarity, and not on the overhead of using Lua. – Mud Apr 01 '14 at 01:59
  • Did I say worrying? I said it has real impact. I also didn't say you should do that to the exclusion of all sanity or lua standard operations. I said that arguing that you shouldn't do it because it is premature and actively hurts clarity and brevity is an argument that doesn't hold water and that using it reinforces otherwise good lua practice. – Etan Reisner Apr 01 '14 at 13:22
  • "I said it has real impact." Doing it as a matter of habit is premature. If you've identified that "it has real impact" (your words), then "you should do it" (my words), because it's *not premature*. I've been active on the Lua mailing list for a decade, and have coded in Lua professionally for years; it's not a matter of "good Lua practice" to habitually, reflexively alias library routines when you don't have to. – Mud Apr 01 '14 at 14:42
  • No, the good practice is using locals. Which, as you've been around so long, you will know is one of the things lua newcomers get wrong very very often. As such, a simple practice, which is both beneficial to runtime in some percentage of cases, has no real clarity or brevity costs and which instills in them the habit of using local for variables is, as far as I am concerned a Good Thing(tm). That being said I absolutely don't offer it as a first suggestion/comment but that's a far cry from actively recommending people away from it. – Etan Reisner Apr 01 '14 at 14:46
  • Using locals is a good habit because it reduces coupling of distant state, reduces name collisions, helps prevent concurrency issues, etc. It has nothing to do with minimizing hash table lookups by aliasing values in VM registers, which is the only reason the practice under discussion is done. By confusing the two, you turn "avoid globals" into a cargo cult where a behavior is mindlessly emulated for the wrong reason because the purpose of the behavior is not understood. – Mud Apr 01 '14 at 15:11
1

"What does it do" is already answered.

For "why is it done": Back before awesome supported lua 5.2 (without deprecated functions), all modules used lua's module() function to set themselves up. This meant that values from the global variable became inaccessible and this "local trick" actually was necessary.

Uli Schlachter
  • 9,337
  • 1
  • 23
  • 39