lua-users home
lua-l archive

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

On Sun, 30 Jan 2005 23:05:48 -0000, Alex Evans <> wrote:
> I was wondering, could I just directly compare the key string's *pointer* (by burrowing inside the VM's string data structure) with some string pointer stored away at initialization time? (I would make sure that this original string was never gc'ed, of course)

Yep, you could. You wouldn't even have to burrow too deeply; the
current VM implementation returns a pointer to the actual string
buffer, so you could just check the equality of the two char*s. But
even that's too clever for it's own good.
Keep each of the strings to test against in the registry, or as
upvalues to the C function, and use lua_equal or lua_rawequal to test.

It's doubtful, though, that any of this is going to measurably affect
the speed of your program.... how many string comparisons do you
expect to do per second? Using something like luaL_findstring, despite
its O(m*n) performance (m = avg string len; n = number of strings), is
likely to be sufficient in all but the most processor intensive

Finally, using a Lua table for the lookups is always an easy and
elegant option, as long as you have something reasonable to stick into
the table's values.