1
votes

I know of: http://lua-users.org/wiki/SimpleLuaApiExample

It shows me how to build up a table (key, value) pair entry by entry.

Suppose instead, I want to build a gigantic table (say something a 1000 entry table, where both key & value are strings), is there a fast way to do this in lua (rather than 4 func calls per entry:

push
key
value
rawset
4
what does your test program look like, how does it benchmark, and why is that not fast enough? :-) ! - u0b34a0f6ae
(also: only 3 function calls are needed per entry: push key, push value, rawset) - u0b34a0f6ae
@ kaizer.se, rawset leaves the table on the stack? - anon

4 Answers

4
votes

What you have written is the fast way to solve this problem. Lua tables are brilliantly engineered, and fast enough that there is no need for some kind of bogus "hint" to say "I expect this table to grow to contain 1000 elements."

1
votes

For string keys, you can use lua_setfield.

0
votes

Unfortunately, for associative tables (string keys, non-consecutive-integer keys), no, there is not.

For array-type tables (where the regular 1...N integer indexing is being used), there are some performance-optimized functions, lua_rawgeti and lua_rawseti: http://www.lua.org/pil/27.1.html

0
votes

You can use createtable to create a table that already has the required number of slots. However, after that, there is no way to do it faster other than

for(int i = 0; i < 1000; i++) {
    lua_push... // key
    lua_push... // value
    lua_rawset(L, tableindex);
}