lua-users home
lua-l archive

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


Javier Guerra wrote:
...
the static int variable is used as a key in the registry, to avoid collisions. to use it, just register any constant string you'd like to use (at module initialization time):

	regString (L, "foo");
	regString (L, "bar");

and, in your __index() metamethod (or anywhere you'd like to test a Lua string against several C strings), first convert it to the registered pointer:

const char *k = *toStringReg (L, n);   /* n is a stack position */
if (k == "foo") {
	do_something ();
} else if (k == "bar") {
	do_someelse ():
} else {
	not_found ();
}

of course, this depends on the compiler optimizing equal constant strings to the same location in memory.

a little variation would be to store small integers (from an enum type) instead of the const char pointers. this would make possible to use switch(getreg(L,n)) {.....}

Thanks for the new solution! The problem with this, or any technique is that none of it is guaranteed. You are assuming the C compiler optimizes string constants from different modules to have the same address. C does not make this guarantee, just like Lua does not make the guarantee that a string returned from lua_tostring() will never change. And this technique requires two table lookups to get the string pointer. It seems unlikely that this would be faster than a strcmp() or other string tests, especially if you did this optimization:

    const char *k = lua_tostring(L, n);   /* n is a stack position */
    if (k[0]=='f' && k[1]=='o' && k[2]=='o' && k[3]=='\0') {
        do_something ();
    } else if (k[0]=='b' && k[1]=='a' && k[2]=='r' && k[3]=='\0') {
        do_someelse ():
    } else {
        not_found ();
    }

Now it is one simple call and 5 inline comparisons to (for instance) determine that the string is "bar". In fact, for testing against lots of strings, a lex-like comparison tree might come in handy here.

At any rate, my hope in this thread was to see if there was any way that Lua can guarantee, or could be made to guarantee, that lua_tostring() would ALWAYS return the same pointer. But there is no such guarantee and it does not look like one is in the cards for the future, for good reasons. So I will just try to make the string comparisons as efficient as possible.


--
chris marrin                ,""$,
chris@marrin.com          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             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`
"As a general rule,don't solve puzzles that open portals to Hell"'