[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua strings as "atoms"???
- From: Rici Lake <lua@...>
- Date: Mon, 16 May 2005 22:43:10 -0500
On 16-May-05, at 10:37 PM, Chris Marrin wrote:
Ben Sunshine-Hill wrote:
It makes the C code a bit uglier, but I try to use upvalues for all my
Lua string constants in C functions that need to be efficient. By
pre-interning the strings, string equality is reduced to a pointer
compare. Using the registry is another approach, and one that looks a
lot cleaner, but isn't quite as efficient.
That's a good idea. But why is the registry less efficient? Can't you
store a string in the registry and get back a numeric id that can be
handed back to the registry to get pushed just as fast as an upvalue?
Or is there some optimization in upvalues that make them faster?
The other question is still outstanding, though. If I give a string to
Lua, will it always give me back the same address in every call to
lua_tostring() for that same sequence of characters? Does this work
across GC cycles, for instance?
The short answer is yes, providing the string doesn't get garbage
collected (that is, as long as you keep a reference to it somewhere).
The longer answer is that the reference manual clearly states that the
string's address will remain constant provided the string is on the
stack. This presumably gives flexibility to language designers, but the
"reference implementation" of Lua does not currently take advantage of