lua-users home
lua-l archive

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


On Wed, Jan 18, 2012 at 07:07:10AM +0100, Philipp Janda wrote:
> Hi!
> 
> On 17.01.2012 08:12, HyperHacker wrote:
> >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.

I second that. And in this case use the -G1 or -G2 switch to get really fast
matching. -G1 isn't always feasible for larger machines (table driven
machines are more compact), but perfect for this very common scenario. It
might also remove the need to quiet GCC about unused variables. Though, for
ragel compilation units I usually just turn those off with `-Wno-unused'
because the next ragel version might have changed the symbol names, breaking
the void statement hack.

<snip example machine>