• Subject: Re: Issues with the current varargs implementation (Was: Re: inadequate documentation for select and table.unpack)
• From: Dirk Laurie <dirk.laurie@...>
• Date: Thu, 23 Jun 2016 11:30:20 +0200

```2016-06-23 11:10 GMT+02:00 Viacheslav Usov <via.usov@gmail.com>:
> On Thu, Jun 23, 2016 at 10:05 AM, KHMan <keinhong@gmail.com> wrote:
>
>> I also added a roughly equivalent table passing version.
>
> I would expect that in this test the callee should still be variadic,
> collecting its arguments into and manipulating them as a table.

I tried that. My machine is considerably slower than KHMan's. :-(

It is in fact slightly faster with this example to use a variadic function
(perhaps because the table is local at a shallower level?), but not
very much.

\$ time lua bench_vararg3.lua 100000 1000
100000000

real    0m13.886s
user    0m12.233s
sys    0m1.630s

…/vararg\$ time lua bench_vararg4.lua 100000 1000
100000000

real    0m13.055s
user    0m12.810s
sys    0m0.231s

> It would
> also be nice to see the results for fewer than 10 arguments, because at
> least thinking about my Lua code, that is the dominant case for variadic
> functions.

The moment that you put an upper bound on n, there is no more any
theoretical distinction between O(1), O(n) and O(n²). If the upper bound
is small enough (and I'd say in this case that anything less than
LUA_MINSTACK qualifies on that score), there is no practical
distinction either.
```
```--
-- Dirk Laurie's vararg benchmark 2016-06-22
-- * table passing, roughly equivalent
--
function f(...)
local d = {...}
local function g(e, i)
return #e - i + 1
end
local t = {}
for k = 1, #d do
t[k] = g(d, k)
end
return t
end

-- 1st argument sets vararg size
-- 2nd argument is for outer loop
if (#arg == 0 or #arg > 2) then
print("*** run with 1-2 args\n(1) vararg size\n(2) outer loop (default=1)\n")
return
end
local n = tonumber(arg[1])
local m = tonumber(arg[2] or 1)
local total = 0

for p = 1, m do
total = total + #f(string.byte(("1"):rep(n), 1, -1))
end
print(total)
```

• Follow-Ups:
• References: