diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-18 15:16:26 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-18 15:16:26 -0300 |
| commit | 3e9dbe143d3338f5f13a5e421ea593adff482da0 (patch) | |
| tree | ce070c600970d216f3a68c78ae9e55d8048ec4ff | |
| parent | 4a8e48086433ad12f2991c07f3064278714fd0f1 (diff) | |
| download | lua-3e9dbe143d3338f5f13a5e421ea593adff482da0.tar.gz lua-3e9dbe143d3338f5f13a5e421ea593adff482da0.tar.bz2 lua-3e9dbe143d3338f5f13a5e421ea593adff482da0.zip | |
New function 'table.create'
Creates a table preallocating memory. (It just exports to Lua the API
function 'lua_createtable'.)
| -rw-r--r-- | ltablib.c | 9 | ||||
| -rw-r--r-- | manual/manual.of | 17 | ||||
| -rw-r--r-- | testes/sort.lua | 21 |
3 files changed, 45 insertions, 2 deletions
| @@ -58,6 +58,14 @@ static void checktab (lua_State *L, int arg, int what) { | |||
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | 60 | ||
| 61 | static int tcreate (lua_State *L) { | ||
| 62 | int sizeseq = (int)luaL_checkinteger(L, 1); | ||
| 63 | int sizerest = (int)luaL_optinteger(L, 2, 0); | ||
| 64 | lua_createtable(L, sizeseq, sizerest); | ||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 61 | static int tinsert (lua_State *L) { | 69 | static int tinsert (lua_State *L) { |
| 62 | lua_Integer pos; /* where to insert new element */ | 70 | lua_Integer pos; /* where to insert new element */ |
| 63 | lua_Integer e = aux_getn(L, 1, TAB_RW); | 71 | lua_Integer e = aux_getn(L, 1, TAB_RW); |
| @@ -390,6 +398,7 @@ static int sort (lua_State *L) { | |||
| 390 | 398 | ||
| 391 | static const luaL_Reg tab_funcs[] = { | 399 | static const luaL_Reg tab_funcs[] = { |
| 392 | {"concat", tconcat}, | 400 | {"concat", tconcat}, |
| 401 | {"create", tcreate}, | ||
| 393 | {"insert", tinsert}, | 402 | {"insert", tinsert}, |
| 394 | {"pack", tpack}, | 403 | {"pack", tpack}, |
| 395 | {"unpack", tunpack}, | 404 | {"unpack", tunpack}, |
diff --git a/manual/manual.of b/manual/manual.of index 48f396d9..42269ff4 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -3234,11 +3234,11 @@ Values at other positions are not affected. | |||
| 3234 | 3234 | ||
| 3235 | } | 3235 | } |
| 3236 | 3236 | ||
| 3237 | @APIEntry{void lua_createtable (lua_State *L, int narr, int nrec);| | 3237 | @APIEntry{void lua_createtable (lua_State *L, int nseq, int nrec);| |
| 3238 | @apii{0,1,m} | 3238 | @apii{0,1,m} |
| 3239 | 3239 | ||
| 3240 | Creates a new empty table and pushes it onto the stack. | 3240 | Creates a new empty table and pushes it onto the stack. |
| 3241 | Parameter @id{narr} is a hint for how many elements the table | 3241 | Parameter @id{nseq} is a hint for how many elements the table |
| 3242 | will have as a sequence; | 3242 | will have as a sequence; |
| 3243 | parameter @id{nrec} is a hint for how many other elements | 3243 | parameter @id{nrec} is a hint for how many other elements |
| 3244 | the table will have. | 3244 | the table will have. |
| @@ -7969,6 +7969,19 @@ If @id{i} is greater than @id{j}, returns the empty string. | |||
| 7969 | 7969 | ||
| 7970 | } | 7970 | } |
| 7971 | 7971 | ||
| 7972 | @LibEntry{table.create (nseq [, nrec])| | ||
| 7973 | |||
| 7974 | Creates a new empty table, preallocating memory. | ||
| 7975 | This preallocation may help performance and save memory | ||
| 7976 | when you know in advance how many elements the table will have. | ||
| 7977 | |||
| 7978 | Parameter @id{nseq} is a hint for how many elements the table | ||
| 7979 | will have as a sequence. | ||
| 7980 | Optional parameter @id{nrec} is a hint for how many other elements | ||
| 7981 | the table will have; its default is zero. | ||
| 7982 | |||
| 7983 | } | ||
| 7984 | |||
| 7972 | @LibEntry{table.insert (list, [pos,] value)| | 7985 | @LibEntry{table.insert (list, [pos,] value)| |
| 7973 | 7986 | ||
| 7974 | Inserts element @id{value} at position @id{pos} in @id{list}, | 7987 | Inserts element @id{value} at position @id{pos} in @id{list}, |
diff --git a/testes/sort.lua b/testes/sort.lua index 40bb2d8a..45014652 100644 --- a/testes/sort.lua +++ b/testes/sort.lua | |||
| @@ -3,6 +3,27 @@ | |||
| 3 | 3 | ||
| 4 | print "testing (parts of) table library" | 4 | print "testing (parts of) table library" |
| 5 | 5 | ||
| 6 | do print "testing 'table.create'" | ||
| 7 | collectgarbage() | ||
| 8 | local m = collectgarbage("count") * 1024 | ||
| 9 | local t = table.create(10000) | ||
| 10 | local memdiff = collectgarbage("count") * 1024 - m | ||
| 11 | assert(memdiff > 10000 * 4) | ||
| 12 | for i = 1, 20 do | ||
| 13 | assert(#t == i - 1) | ||
| 14 | t[i] = 0 | ||
| 15 | end | ||
| 16 | assert(not T or T.querytab(t) == 10000) | ||
| 17 | t = nil | ||
| 18 | collectgarbage() | ||
| 19 | m = collectgarbage("count") * 1024 | ||
| 20 | t = table.create(0, 1024) | ||
| 21 | memdiff = collectgarbage("count") * 1024 - m | ||
| 22 | assert(memdiff > 1024 * 12) | ||
| 23 | assert(not T or select(2, T.querytab(t)) == 1024) | ||
| 24 | end | ||
| 25 | |||
| 26 | |||
| 6 | print "testing unpack" | 27 | print "testing unpack" |
| 7 | 28 | ||
| 8 | local unpack = table.unpack | 29 | local unpack = table.unpack |
