lua-users home
lua-l archive

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


On Sun, Oct 11, 2020 at 6:20 AM Ranier Vilela <ranier.vf@gmail.com> wrote:
Em dom., 11 de out. de 2020 às 04:58, 孙世龙 sunshilong <sunshilong369@gmail.com> escreveu:
Hi, list

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 [1]:

"Parameter narr is a hint for how many elements the table will have as a sequence; parameter 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.

[1] https://www.lua.org/manual/5.4/manual.html#lua_createtable