I think those are unnecessary micro optimizations that also make your code less readable, but if you need that last percentile of performance, it might help.
Always just the current block. If you set a local variable in global scope, it acts as implicitly global for that file (called chunk)
I can't believe I've been coding all this time in the way both ugly and inefficient, under my misconception.
Just to be clear, and I don't have the link to it right now, but there was a mailing list post where the LuaJIT creator says that an upvalue has the same speed as an inner scope local.
So if you declare "local this_thing" at the top scope of a script, when you use this_thing it'll have the same speed as locals in inner scopes of that same script. It'll be visible everywhere within that script, kinda like a global, but a bit faster -- the speed cost of a global is that it's an implicit key search in table _G.
So if you want something to be visible everywhere within a single script, making it a top-level local in that script will give you some free speed improvement.
but if you need that last percentile of performance, it might help.
And maybe one can type much less in that way (also with less typos), if using text editor that does not support auto-completion.
LuaJIT creator says that an upvalue has the same speed as an inner scope local.
So if you want something to be visible everywhere within a single script, making it a top-level local in that script will give you some free speed improvement.
Oh, yes - this information is what I needed to know as well. So, within a module code file I can set some local variables for convenient coding as 'globals' for the whole scope of this file - which are still faster than real globals(in _G) in methods and loops, and won't pollute global table and effect other module files.