lua-users home
lua-l archive

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


computed gotos are a feature of modern compilers [1]. They can be used
as a faster replacement for switch-based VM [2]. Many programming
languages have VMs implemented in computed gotos (at least, Ruby and

The computed goto version is faster because of two reasons [2]:

  * The switch does a bit more per iteration because of bounds checking.
  * The effects of hardware branch prediction.

I have applied this optimization to VM of Lua 5.3.2, file src/lvm.c
[3]. It was very easy, because VM uses macros vmdispatch, vmcase and
vmbreak. I have redefined these macros and created a dispatch table.

It passes Lua basic tests (path/lua -e"_U=true" all.lua) [4].

My benchmark [5] shows speedup of 1.12:

$ time ./src/lua.orig ~/lang-bench/f3/test.lua

real    0m28.208s
user    0m28.166s
sys     0m0.004s

$ time ./src/lua ~/lang-bench/f3/test.lua

real    0m25.066s
user    0m25.030s
sys     0m0.000s

This commit is a draft, not a final contribution. If a compiler
doesn't support computed gotos, the switch based implementation should
be used, as before.



Best regards,
Boris Nagaev