lua-users home
lua-l archive

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


On Sun, Apr 13, 2014 at 10:33 PM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> Ah, but Lua never had pack()/unpack() like you describe - they were
> just put into the table namespace.

I mean bring back the function names, but change their functionality
-- I wouldn't want these called anything else, I mean to say.

> That is, pack makes a table from varargs.
>
> Are you thinking of the pack defined in that varargs library?

I do mean the functionality of the pack() function in the vararg
library: https://github.com/moteus/lua-vararg/blob/master/vararg.c#L78

It looks like it stores varargs as upvalues in a closure -- if one
were to 'share' the function the closure is derived from then memory
could be kept low, but I believe this could be made lighter if
"slices" of the lua stack could be packed into a userdata.

> I can't see how existing pack can be made more efficient - the varargs
> need to be stored, and a table is a natural container for them.

The underlying struct for a table and its nodes is pretty extensive,
as it can mold itself into an array or hash table, and keeps track of
the length of each portion:
http://www.lua.org/source/5.2/lobject.h.html#Table

What I'm proposing would be like taking 3 items on the top of the
stack and rolling them into a userdata -- I'm not sure how that can be
done, but it would seem to be lighter on memory if you are storing
500, 3-item varargs vs 500, 3-item sequences/tables.