lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Aug 08, 2014 at 09:27:17PM -0700, Tim Hill wrote:
> 
> On Aug 8, 2014, at 5:26 PM, William Ahern <william@25thandClement.com> wrote:
> 
> > On Fri, Aug 08, 2014 at 05:09:55PM -0700, Tim Hill wrote:
> >> 
> >> On Aug 8, 2014, at 3:29 PM, Mason Mackaman <masondeanm@aol.com> wrote:
> >> 
> >>> So why isn?t everything in the standard libraries made local by default if it?s always faster that way?
> >> 
> >> Because locals are not accessible to libraries (and should not be, that?s
> >> what makes them local). A local ?foo? is fast because the compiler
> >> converts accesses to it into direct references to the Lua stack.
> > 
> > It converts them into a fast index into an upvalue array stored as part of
> > the currently running function.
> > 
> > The stack isn't involved at all, unless you create a closure which binds to
> > a live function parameter. Then it copies the value from the stack to the
> > upvalue slot when creating the function object.
> > 
> > 
> 
> I was simplifying for the OT. And last time I looked locals and upvalues
> were distinct.

My bad. When I was writing locals I was thinking of locals declared outside
the function. Those always become upvalues of the function capturing them,
and those are the relevant kind of locals when discussing the performance of
globals versus locals, such as when asking why standard library routines
aren't magically made locals by default. One could make them "locals" by
default, but you'd make them upvalues, not push them onto the function stack
every time the function was invoked, which would be slower than making them
magic upvalues.