[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Explain?
- From: Ben Sunshine-Hill <sneftel@...>
- Date: Thu, 28 Oct 2004 11:30:02 -0700
> local a = MakeAnEnormousTable()
> local function foo(x)
> return a[x]
> end
> -- no more references to a, but foo is still available.
> local b = "Hello, world"
> print(foo(b))
That code snippet wouldn't really impact lifetime-detection, since the
declaration of foo there would simply create an upvalue and
immediately close it (freeing up the stack space).
To answer the original question: Internally, temporary data and local
variables are both stored on the stack. In the case of the string,
what is stored on the stack is a pointer to a garbage-collected string
buffer--which is the same in either case. That means that the memory
consumption of the two original code fragments is the same. In terms
of speed, however, the cached version is likely to be faster. Moving a
string over from C to Lua requires that the string be hashed and
interned; not slow by any means, but slower than only doing it once.
In contrast, the code fragment which caches ensures that each string
comparison is equivalent to a pointer comparison.
Ben