Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pre-size an array in Lua?

Tags:

I've got a Lua program that seems to be slower than it ought to be. I suspect the issue is that I'm adding values to an associative array one at a time and the table has to allocate new memory each time.

There did seem to be a table.setn function, but it fails under Lua 5.1.3:

stdin:1: 'setn' is obsolete
stack traceback:
        [C]: in function 'setn'
        stdin:1: in main chunk
        [C]: ?

I gather from the Google searching I've done that this function was depreciated in Lua 5.1, but I can't find what (if anything) replaced the functionality.

Do you know how to pre-size a table in Lua?

Alternatively, is there some other way to avoid memory allocation when you add an object to a table?