lua-users home
lua-l archive

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


On 11/06/2014 10:47 PM, Roberto Ierusalimschy wrote:
Probably I am missing something, because I fail to see what in a
conventional switch statement (for a language like Lua) could be
different than sugar for a chain of if-elseif-else (unless you
are talking about "fall through", in which case you should use
gotos).

Hello Roberto,

To answer "what could be different than sugar for if-elseif-else?":

A linear chain of if-elseif-else conditions has N comparisons. I would expect a switch to use one or both of the following strategies:

a) generate a balanced tree of tests to give log(N) comparisons (if the value range is sparse)

or

b) a single lookup (ie a jump table to program locations indexed by the switch value).

or maybe even:

c) a perfect hash combined with a jump table.


Perhaps what you are saying is that as there are no symbolic constants in Lua, it is not possible to generate optimized switch statements for all but the simplest scenario where the switch cases are literals?

Ross.