lua-users home
lua-l archive

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


Hi!

Am 27.03.2013 22:13 schröbte Marc Lepage:
Hi, this seems like it might be a bit of a FAQ or common pattern, for those
working in C (e.g. implementing native types).

In C, I can easily get a string and do a few if statements with strcmp to
decide how to handle it. If the methods were called infrequently, I might
not be concerned.

What I'm wondering is, if there is a way to avoid the strcmp logic by
directly comparing some interned strings, or something like that.

Sort of like:

- in C, grab a copy of the few strings I'm interested in, in some sort of
reference
- in C, when methods come in, check the string args (on the stack) against
the references grabbed

So that the check is more of a pointer compare than a strcmp.

Is there a pointer to sample code (e.g. users wiki) showing how this is
commonly done, gotchas, etc.?

There are some options (with varying degrees of speed and safety) mentioned in this[1] thread.

  [1]: http://lua-users.org/lists/lua-l/2012-01/msg00396.html

Another possibility (that I think isn't mentioned in the above thread, but I only took a brief look) could be to put all your string constants into upvalues and use the Lua API (lua_upvalueindex, lua_compare/lua_rawequal) to compare those upvalues to the argument strings on the stack.

But then again, libc implementers and compiler writers have had some time to optimize strcmp.


Much appreciated,
Marc


Philipp