lua-users home
lua-l archive

Re: Using Lua's string interning for faster compares?

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


When I write modules to wrap C++ objects, they usually have a __index
method that does a lot of string comparing, like:
if(!strcmp(key, "foo")) lua_pushcfunction(L, obj_method_foo);
else if(!strcmp(key, "bar")) lua_pushcfunction(L, obj_method_bar);
etc... I'm not sure if compilers can optimize this, but doing all
those strcmp()s every time a method/field is looked up seems terribly
inefficient.
Try Ragel[1].
[1]: http://www.complang.org/ragel/
I've attached the template I use for such occasions.
Alternatively, re2c does the same thing: http://re2c.org/ (for simple use cases like matching a string against one of several predefined constants).
 - Peter

AltStyle によって変換されたページ (->オリジナル) /