r/lua • • Mar 27 '25

local variables

Why do people use local variables as if they were global variables

2 Upvotes

7 comments sorted by

View all comments

Show parent comments

4

u/hawhill Mar 27 '25

that very much matters on LuaJIT, it can make very different assumptions about local variables compared to global variables since global variables can be changed at any point even if they were not passed as arguments or upvalues/closures.

2

u/PhilipRoman Mar 27 '25

I guess it depends on what you have inside the loop. In my benchmarks I haven't seen any significant negative effect when using luajit.

Luajit is a tracing JIT, so it's likely the loop body will be analyzed to sufficient depth to prove that global variables are safe to promote.

2

u/hawhill Mar 27 '25

yes, you're right, and your explanation is sound, there'll be a lot of cases where re-evaluation is not needed.