Question: Direct-Threaded Interpreter

115 views
Skip to first unread message

Eleanor Bartle

unread,
Jun 11, 2025, 5:54:24 AMJun 11
to lua-l
I saw recently that Python 3.14 is rewriting the core interpreter loop to take advantage of tail calls, for dramatic performance improvements. I also remember that as of 5.0, Lua uses a plain while-switch as its core loop, not reaping these benefits.

My question is, has this changed since that article was written, and if not, are there plans to change it in the future? If not with tail calls, then perhaps with computed goto, or some other equivalent method.

Pierre Chapuis

unread,
Jun 11, 2025, 6:04:18 AMJun 11
to lu...@googlegroups.com
This interpreter relies on the compiler supporting tail calls in C with __attribute__((musttail))

A strength of PUC Lua is being written in a very portable dialect of C. This wouldn't fit with that property.

However this Python interpreter was actually inspired by this project: https://github.com/luajit-remake/luajit-remake so if you want such an interpreter for Lua (5.1 so far...) you already have one.

-- 
Pierre Chapuis

On Wed, Jun 11, 2025, at 11:54, Eleanor Bartle wrote:
I saw recently that Python 3.14 is rewriting the core interpreter loop to take advantage of tail calls, for dramatic performance improvements. I also remember that as of 5.0, Lua uses a plain while-switch as its core loop, not reaping these benefits.

My question is, has this changed since that article was written, and if not, are there plans to change it in the future? If not with tail calls, then perhaps with computed goto, or some other equivalent method.


--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.

Roberto Ierusalimschy

unread,
Jun 13, 2025, 12:38:39 PMJun 13
to lu...@googlegroups.com
> I saw recently that Python 3.14 is rewriting the core interpreter loop
> <https://docs.python.org/3.14/whatsnew/3.14.html#whatsnew314-tail-call> to
> take advantage of tail calls, for dramatic performance improvements. I also
> remember that as of 5.0, Lua uses a plain while-switch
> <https://www.lua.org/doc/jucs05.pdf> as its core loop, not reaping these
> benefits.
>
> My question is, has this changed since that article was written, and if
> not, are there plans to change it in the future? If not with tail calls,
> then perhaps with computed goto, or some other equivalent method.

As Pierre pointed out, the standard source uses while-switch, but since
version 5.4.0 it also offers the option to use computed goto's (jump
tables) when available. That is the default when compiling with gcc; see
the macro LUA_USE_JUMPTABLE in 'lvm.c' and the file 'ljumptab.h'.

At least for Lua, that change brings performance improvements, but they
are far from "dramatic" :-) Something in the order of 5~10%...

-- Roberto
Reply all
Reply to author
Forward
0 new messages