[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Computed goto optimization of vanilla Lua
- From: "Soni L." <fakedme@...>
- Date: Wed, 3 Feb 2016 20:47:05 -0200
On 03/02/16 08:28 PM, Nagaev Boris wrote:
So the C equivalent of NoVM? (NoVM, in Lua and functional languages,
basically builds a linked list of functions, which use tail-calling to
go to the next instruction)
computed gotos are a feature of modern compilers . They can be used
as a faster replacement for switch-based VM . Many programming
languages have VMs implemented in computed gotos (at least, Ruby and
The computed goto version is faster because of two reasons :
* 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
. 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) .
My benchmark  shows speedup of 1.12:
$ time ./src/lua.orig ~/lang-bench/f3/test.lua
$ time ./src/lua ~/lang-bench/f3/test.lua
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.
 It's a name I came up with. Basically means a VM without what we
consider "the VM", that is a VM without an interpreter loop. (Physical
machines use what's equivalent to an interpreter loop, so it makes sense
for VMs to use interpreter loops. A loop-less VM doesn't sound much like
a real machine.) This is actually much better on JITs, yielding HUGE
performance improvements compared to interpreter loops. Tested with
LuaJIT. It also lets you add features you couldn't otherwise.
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.