lua-users home
lua-l archive

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


2016-06-20 21:04 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> A point that does rate some extra explanation, but perhaps iin PiL
>> rather than the reference manual, is that 'select' is in fact an
>> extremely efficient operation, involving no actual copying of
>> arguments. There is no API equivalent, because none is needed.
>> It returns the stack exactly as it found it, but reporting a possibly
>> different number of return values.
>
> That is not true. 'select' can be quite slow for a large number of
> parameters (where "large" starts in the order of a few dozen). In
> particular, a typical loop in a vararg function using 'select' is
> quadratic with the number of arguments, while creating a table and
> traversing it is linear.

Could you explain why that happens? luaB_select is clearly O(1).
Does VARARG copy the arguments? If so, why is that necessary?

$ luac -p -l -
function bump(...)
return select(3,...)
end

main <stdin:0,0> (3 instructions at 0x87f780)
0+ params, 2 slots, 1 upvalue, 0 locals, 1 constant, 1 function
    1    [3]    CLOSURE      0 0    ; 0x87f980
    2    [1]    SETTABUP     0 -1 0    ; _ENV "bump"
    3    [3]    RETURN       0 1

function <stdin:1,3> (6 instructions at 0x87f980)
0+ params, 3 slots, 1 upvalue, 0 locals, 2 constants, 0 functions
    1    [2]    GETTABUP     0 0 -1    ; _ENV "select"
    2    [2]    LOADK        1 -2    ; 3
    3    [2]    VARARG       2 0
    4    [2]    TAILCALL     0 0 0
    5    [2]    RETURN       0 0
    6    [3]    RETURN       0 1