1

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
  • 2
    what does your test program look like, how does it benchmark, and why is that not fast enough? :-) ! Commented May 9, 2010 at 11:36
  • (also: only 3 function calls are needed per entry: push key, push value, rawset) Commented May 9, 2010 at 11:53
  • @ kaizer.se, rawset leaves the table on the stack? Commented May 9, 2010 at 20:17
  • Don't ask me, ask lua.org/manual/5.1/manual.html#lua_rawset Commented May 9, 2010 at 22:50

4 Answers 4

4

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."

Sign up to request clarification or add additional context in comments.

Comments

1

For string keys, you can use lua_setfield.

Comments

0

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

Comments

0

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);
}

2 Comments

you don't need to duplicate the table on the stack? rawset doens't consume the table?
No. Rawset only consumes the key and value. Else, how could you ever set more than one k, v pair into one table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.