lua-users home
lua-l archive

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


On Jun 26, 2014, at 4:32 PM, Justin Cormack <justin@specialbusservice.com> wrote:

> On Thu, Jun 26, 2014 at 6:55 AM, Paige DePol <lual@serfnet.org> wrote:
>> On Jun 11, 2014, at 3:29 AM, Axel Kittenberger <axkibe@gmail.com> wrote:
>> 
>>> But a switch statement in most script languages (like in Javascript) is syntactic sugar for if/else! That has always been the argument from the Lua team to why there is no switch statement in Lua. Not like for example in C where it is in fact a computed goto.
>> 
>> I have created a switch statement patch[1] for Lua, it uses a jump table (aka computed goto) and is considerably faster than the standard if/else structures... however, applying it to vanilla Lua does present one large issue.
>> 
>> Lua itself has no support for symbolic constants, thus the case values of the switch statement my patch adds have to be literal constants. This leads to the use of magic numbers which hurts overall code quality.
>> 
>> This, I believe, is one of the major reasons Lua does not yet have a switch statement. Without symbolic constants any switch statement added to vanilla Lua could only ever be syntactic sugar for an if/else chain.
> 
> I always assumed it was because a table of functions is as good as a
> switch statement...
> 
> Justin

Except that a jump table is faster than a table of functions, there is overhead for calling a function, the only overhead for a jump table is a single table lookup, and of course the storage of the jump table itself.

For my custom variant of Lua I am working on making things as efficient as possible as I plan to use the language as a scripting engine for a 3D game engine. I also eventually plan to work on creating a custom clang filter to allow direct compilation of scripts by using the objective-c runtime... much like Apple has done with their new Swift language.

I chose to use Lua because it is already fast, and I do not intend to slow it down with any of my patches. If anything, I hope my patches can make some aspects of Lua more efficient, though some of my patches are just syntax changes I admit (like ~= to !=, for example).

~pmd