Re: Faster 'generic for' for raw table

云风 Cloud Wu <[email protected]> Fri, 12 Jun 2026 20:42:40 +0800
Newsgroups gmane.comp.lang.lua.general
Message-ID <CAJnYMr2Es1bf9EUiRimORCcOYsN8Hm=bYu7ZR7_nQdLACy6ejg@mail.gmail.com>
Luiz Henrique de Figueiredo <[email protected]> 于2026年6月12日周五 20:29写道:
>
> > The original lua 5.5 cost 0.97s , and patched version (using lua_next
> > instead of luaD_call) cost 0.42s . It's almost twice as fast.
>
> Add some real work in the loops like calling a function or
> concatenating strings or creating tables and this difference will
> probably decrease significantly.

True.

However, iterating tables is a fundamental operation, and considering
that tables are almost the only data structure in Lua, I believe even
a minor optimization would be valuable. In many cases, Lua's function
calls are still relatively slow. When optimizing performance hotspots,
we can use code generators to inline function calls into flat code.
However, iterating raw tables remains unavoidable. If the language
itself could provide a way to iterate raw tables, it would be very
useful

btw, there are also many cases where for loops do very little work.
Take penglight's tablex.lua as an example, most of the pairs-based
loops in that file contain almost just one or two lines of code.

https://github.com/lunarmodules/Penlight/blob/master/lua/pl/tablex.lua

-- 
http://blog.codingnow.com

-- 
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 [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAJnYMr2Es1bf9EUiRimORCcOYsN8Hm%3DbYu7ZR7_nQdLACy6ejg%40mail.gmail.com.