Search code examples
luaawesome-wm

Why redefinition standard variables in awesome wm modules needed?


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.


Solution

  • 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.