[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Question about Lua performance (locals vs globals)
- From: Thomas Lauer <thomas.lauer@...>
- Date: Thu, 07 Aug 2008 15:03:50 +0100
Bryan White <email@example.com> wrote:
> David Given wrote:
> > They actually work by totally different mechanisms. Lua locals are
> > lexically scoped true variables, and exist on the Lua stack
> > internally. When you access a local, it's referred to directly
> > without any table lookup happening at all. Lua globals aren't really
> > variables at all; they're simply syntactic sugar for doing table
> > accesses on _G. This:
> It seems that asking script coders to be aware of such issues and
> optimize their code for them is less than optimal.
As David pointed out, "global" is a clear misnomer here. The Lua
documentation could usefully give better explanations as well as some
guidance as to what "local" and "global" really means in a Lua context
(and how both concepts could/should be used -- or not used). Someone
coming from other languages (or even someone with no preconceptions)
won't necessarily spot the interesting pitfalls hidden in these
Many, if not most, of my scripts these days don't use globals at all.
Not mainly because they're slightly faster that way, but because they're
easier to understand and less error-prone.