I'm not sure. Because of the do end block the local var will always become GCable after that block is done but I do believe it is more work to pull in the var in the 2nd one because it is also making the value of it available to the inner block instead of just creating a local and directly assigning a value to it.
You can use luac -l to create a listing of the compiled bytecode for comparison.
I think the thing that is getting people confused here is GC vs stack. That's certainly what the blizzy "makes GC cry" comment was about. Are you making lots of tables or unique strings? If not, then don't worry about it. Lua manages locals just fine, the thing you need to concern yourself with is generating garbage, which happens when you make lots of local tables or unique strings. The only thing that really matters with were locals are declared is scope, not performance.
I benchmarked this for some math-heavy OnUpdate rendering code, and upvalues were definitely faster than pushing values to the stack, but only by milliseconds over thousands of calls. Of course, this makes the code hard to maintain, ugly, and breaks recursion. Use it as a last resort, if ever.
I think the thing that is getting people confused here is GC vs stack. That's certainly what the blizzy "makes GC cry" comment was about. Are you making lots of tables or unique strings? If not, then don't worry about it. Lua manages locals just fine, the thing you need to concern yourself with is generating garbage, which happens when you make lots of local tables or unique strings. The only thing that really matters with were locals are declared is scope, not performance.
They used to define a table there and then they fixed that but the comment wasn't removed, then they removed it in 3.2 (or maybe it was 3.1). I would look up the original post, but search is terrible and so that's impossible.
luac -l gives the same bytecode for both loops. IMO there is only some differences between upvalues and stack-locals, both memory and performance wise.
No, the summary is "Don't concern yourself with memory for locals, it doesn't involve gc." ... which was my first post in the thread :P If you're optimizing, focus on things like tables, strings and OnUpdates.
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
compared to
Okay, I think I did not get it right... are these two now equivalent (functional ofc, but also performance)?
I think I read not, but I doubt this... If they are not, why exactly?
You can use luac -l to create a listing of the compiled bytecode for comparison.
They used to define a table there and then they fixed that but the comment wasn't removed, then they removed it in 3.2 (or maybe it was 3.1). I would look up the original post, but search is terrible and so that's impossible.
Both should give the exact same performance. Both code uses "var", which are located at the same location on the stack.
BTW, someone has tried to sum up memory allocation on wowwiki : http://www.wowwiki.com/Memory_usage.
Do you want to optimize? Don't.