lua-users home
lua-l archive

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


Ben Sunshine-Hill wrote:
On 12/3/05, Chris Marrin <chris@marrin.com> wrote:

will the pointer s always be equal to the pointer prop? In all the cases
I have tried it is. But can I rely on this?


It certainly is in the current implementation. The VM relies on
pointer equality for string comparisons, so you can too. But I seem to
recall that the last time this came up, it was pointed out that this
is not part of the standard, and may change in future versions (though
it's hard to think how).

A method that would clearly be compliant would be to store the strings
themselves, rather than the pointers. Because all Lua strings are
interned, comparisons are constant time.

I'm not sure what you mean by that. If you mean that I need to use lua_isequal(), then I would need to go:

    lua_pushstring(L, "foo");
    if (lua_isequal(L, -1, -2)) ...

which means hashing the string every time. I could also store atoms in the registry:

    lua_pushstring(L, "foo");
    myFoo = luaL_ref(L, LUA_REGISTRYINDEX);

then I could test like this:

    lua_rawgeti(L, LUA_REGISTRYINDEX, myFoo);
    if (lua_isequal(L, -1, -2)) ...

but this requires looking up the atom in the atom every time. In most cases, I think this:

    if (strcmp(lua_tostring(L, -1), "foo") == 0) ...

would be just as fast as those two techniques. I really want to use this:

    if (lua_tostring(L, -1) == myFooAtom) ...

which would be much faster than any of those.

Of course, the best solution of all would be for Lua to have a well-defined atom mechanism, something like:

    LUA_ATOM atom1 = lua_newatom(L, "foo");

which would return an atom handle, and:

    LUA_ATOM atom2 = lua_toatom(L, -1);

which gets a string from the stack and gives back an atom handle, guaranteed to be the same value as that returned from lua_newatom(), so you could go:

    if (atom1 == atom2) ...

and finally:

    lua_releaseatom(L, atom);

to toss the reference to that atom. This assumes that using lua_createatom() would reference the atom so it does not get gc-ed if all other refs to it go away, and lua_releaseatom() makes that atom available for GC.

Of course, I fully expect someone to say "geez, pointer comparison isn't that slow", which may be true. I'm just trying to squeeze every instruction out of the interface I can!

--
chris marrin              ,""$, "As a general rule,don't solve puzzles
chris@marrin.com        b`    $  that open portals to Hell" ,,.
        ,.`           ,b`    ,`                            , 1$'
     ,|`             mP    ,`                              :$$'     ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`