[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Is it possible to pre-allocate a table with a specific size(i.e. pre-allocating the max memory that may be used by a table)?
- From: Philippe Verdy <verdyp@...>
- Date: Sun, 11 Oct 2020 19:10:27 +0200
I think this should not be exposed, unless you tie your Lua program to a specific implementation and version. This is basically used only in the current implementation just to allow fast startup of the existing engine but this API should not even be considered "stable" and is only needed for internal use. Any later version of Lua could change it, providing other tuning parameters (and for now there's no official stable API to tune a specific implementation, it would require IMHO defining a new type for tuning parameters, using a set of key/value property pairs, with keys inside some namespace targeting a specific class of implementations. These will only be used as hints, and not requirements, the implementations are then free to ignore these parameters.
Is it possible to pre-allocate a table with a specific size(i.e.
pre-allocating the max memory that may be used by a table)?
As per the book(Programming in Lua 4th Edition, page 37), which says that:
To represent a conventional array or a list, we simply use a table
with integer keys. There is neither a way
nor a need to declare a size.
As far as I know, there's no way.
Even the Lua C API is limited by int (MAX_INT).
In the C API, there is lua_createtable :
narr is a hint for how many elements the table
will have as a sequence;
nrec is a hint for how many other elements
the table will have.
Lua may use these hints to preallocate memory for the new table.
This preallocation may help performance when you know in advance
how many elements the table will have."
This is not exposed on the Lua side, though it would be trivial to write a small extension module to make it available.