lua-users home
lua-l archive

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


Mark Hamburg <mhamburg.ml@gmail.com> wrote:

> I don't know what the Lua 5.4 implementation is doing, but since it isn't
> clear whether arguments are varargs or regular args until one gets into
> the function itself, it certainly seems like if this were an issue it
> would be straightforward for the compiler to recognize when the function
> just passes the varargs along without accessing them in any other way and
> take appropriate steps to optimize for this case. In other words, at the
> very least, it doesn't seem like this has to be a concern.

A quick look at the code on GitHub for Lua 5.4 shows that the use of "..."
as a function parameter will create a table and populate it with the vargs
on the stack[1] before the call or tail call. 

Then, any "..." encountered in subsequent function calls will simply unpack
the table back onto the stack[2], to be wrapped up in a table once again
for the newly called function if it has varg parameters. If not, then the
varg table will be unpacked and used for potential function parameters.

The compiler could create a "static vargs" flag for functions which only
pass on the varg parameters without accessing or modifying the parameters.
In such cases the vargs could just be treated as they were in previous
versions of Lua, and thus would only be converted into a table at the
point of the forwarded function call.

As it does seem that people do use functions to simply forward variable
arguments, without access or modification, it may be worth doing some tests
to determine the extra overhead of table creation for those use cases.

~Paige

[1] luaT_adjustvarargs in ltm.c at line 218
     Called from luaD_call (line 442) and luaD_pretailcall (line 414) in ldo.c

[2] luaT_getvarargs in ltm.c at line 237
     Called from the OP_VARARG case in lvm.c at line 1702