<p>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.  </p>

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

<pre><code>stdin:1: 'setn' is obsolete
stack traceback:
        [C]: in function 'setn'
        stdin:1: in main chunk
        [C]: ?
</code></pre>

<p>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.</p>

<p>Do you know how to pre-size a table in Lua?</p>

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

I don't think you can - it's not an array, it's an associative array, like a perl hash or an awk array.
http://www.lua.org/manual/5.1/manual.html#2.5.5
I don't think you can preset its size meaningfully from the Lua side.
If you're allocating the array on the C side, though, the
```
```
void lua_createtable (lua_State *L, int narr, int nrec);
```
```
may be what you need.
Creates a new empty table and pushes
  it onto the stack. The new table has
  space pre-allocated for narr array
  elements and nrec non-array elements.
  This pre-allocation is useful when you
  know exactly how many elements the
  table will have. Otherwise you can use
  the function lua_newtable.