lua-users home
lua-l archive

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


> But then I'd have to convert my switch to a series of ifs since the pointers
> to the literals are computed during runtime.

Sure. But in general there's no difference except for several pointer lookups.
What can be done by gcc can also be done in real time (maybe some JIT
way could help).

> I know switches are not great with sparse constant spaces such as when using
> hashes (i.e. only a few tenths of used values from 2^32 possible values) but
> I like the compiler doing the binary search for me (gcc does) instead of
> having to figure it out myself. I'm also experimenting hashing the hash
> (just shifts and xors, nothing fancy or costly) to make the space dense
> enough so the switch will (hopefully) become a goto table + one if for each
> case to see if it's really a match. Better than the hard-coded binary
> tree...

If switch construction is what you need, I guess there are 2 ways:

1. Write a pointer->number hash and use it like
switch (lookup (L, lua_tostring (L, 1))) {
case 0:
case 1:
...
}

2. Write a patch to Lua yourself, put it somewhere into
http://lua-users.org/wiki/LuaDirectory
and maintain to keep it up-to-date.