On Sun, Sep 18, 2011 at 10:21 AM, Gavin Wraith<gavin@wra1th.plus.com> wrote:
In message<4E75EDC0.10400@interfree.it> you wrote:
I still don't understand what's wrong in my reasoning, since the
implementation clearly proves me wrong as I showed in my first post.
-- Lorenzo
Consider these three snippets:
Evidently B uses one less instruction. In A the extra
instruction (LOADNIL) occurs outside the loop, in C within it.
So the ranking (< means "better") appears to be B< A< C.
The key point, I think, is whether the local declaration is part
of an assignment.
This is a good summary. Another point neglected is that Lua recycles
slots in a function when a local goes out of scope. So if you have:
do
local a = 1
-- do something with a
end
do
local a = 2
-- do something with a
end
-- repeat above do<block> end hundreds of times
In the above, you'd be ok with only 1 stack slots used (try it!). If
you try that without the do<block> end, you have 200+ slots taken up
by the repeated 'a' locals.