r/lua • u/Bright-Historian-216 • Jul 01 '25
Help realistically, how much faster is binding globals to a local? is it even noticeable?
29
Upvotes
r/lua • u/Bright-Historian-216 • Jul 01 '25
23
u/[deleted] Jul 01 '25
It's not, unless you're doing tight and heavily optimized loops don't care about it, if anything, use set metatable on _ENV to your favor.
13
u/DapperCow15 Jul 01 '25
Best way to check is to create and run a benchmark of the two.
6
u/Anabela_de_Malhadas Jul 01 '25
for small things, not that important, but for massive loops it's good to local things such as "next" or "math.something", etc, as improves speed by good % incrementally
5
u/drcforbin Jul 02 '25
If it isn't worth benchmarking, which one is faster doesn't matter. Write the code how you want to write it, don't waste your time optimizing unless you actually need to save processor time, and always benchmark potential optimizations in your use case.
4
u/Denneisk Jul 02 '25
To preface this post, I do not really work with the Lua source code. I just look at it from time to time and hear hearsay.
Locals are stored as a sequential, integral index in an array of "registers" as part of the chunk's scope (a chunk being a function or Lua file). In comparison, global access always needs to do a string hash operation on the global table to retrieve the requested object. The small difference between using these registers versus having to do a table hashmap retrieval is probably the largest contributor of performance difference, although it is typically very insignificant.
4
u/SkyyySi Jul 02 '25
IMO the main advantage is that you can prevent some other, external piece of code from changing the behaviour of your script. If you cache each function as a local, you can be certain* that your code won't suddenly break because someone descided it'd be a great idea to override standard library functions.
If you use LuaJIT, there is another advantage: LuaJIT can much more easily inline functions if they are locally cached, in particular those loaded via FFI (in that specific case, make sure to only cache the namespace and not the function). With locals, LuaJIT knows exactly where you're allowed to use a given variable, whereas this isn't possible with globals. It can still optimize those, too, but it can only happen after the code has executed enough for LuaJIT's tracing optimizations to kick in.
*Technically, that's not quite right, because the
debuglibrary exists. However, any use of that is unstable, which is why LuaJIT generally ignores its existance in favor of better optimizations.2
u/That-Cpp-Girl Jul 02 '25
If performance matters, benchmark. You may find the local definition to be a pessimisation or an optimisation, it really depends on the use-case. If performance doesn't matter, then just use whatever is easier/faster to write.
2
u/hawhill Jul 03 '25
Don’t worry. Up to the point to when you have to.
E.g. I’ve once wrote a Blitting function library for mixed pixel value variants in pure Lua, targeting LuaJIT admittedly (and crucially). When you intend to do millions of runs per second, then it really pays off to be able to reason about path cost and do optimizations like this.
It really was faster than carefully crafted C. Probably on par with very well coded C++, but oh so exciting and concise.
1
u/Skagon_Gamer Jul 03 '25
It doesn't matter, the natural compiling randomness would absolutely out do the increase of such a thing, if you really need to have such small efficiency increases then use cpp or a language meant for that. But also global variables are held inside of a table (you can actually access it as a table, its "_G") so technically writing to it is slower since youre accessing a table, but again its at worst a couple instructions per call and doesn't matter at all.
0
u/no_brains101 Jul 01 '25 edited Jul 02 '25
It isn't in this case I don't think.
next is a global upvalue which means it is already in basically the fastest table to index.
If you had like someval.blah.thing.value then you might want to local val = someval.blah.thing.value
Likewise, require is slow on the first call on a single thing cause it has too look it up and whatnot, but afterwards may be basically as fast as a table lookup like the local val above
Edit: DeKwaak is correct
0
u/[deleted] Jul 01 '25
[deleted] — view deleted comment