lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Jun 16, 2009 at 7:33 AM, Marco Antonio Abreu
<mabreu.ti@gmail.com> wrote:
>
> Hi Guys,
>
> My problem starts when I need to load thousands of data in a table thousands of times during an execution.
>
> Some time ago we had talked about an native function to create tables with predefined number of elements direct in Lua.
>
> My basic question is why there is no function in the table library to mirror lua_createtable() from the C API?
> Like our friend Jason Santos said, "I would like to be able to make them dynamically and in pure lua. With this new function we will have opportunities to reduce the amount of table.inserts." I suppose it will be very useful if we could create a table with all occurrencies we need direct from Lua, without using loadstring for a concatenated string or thousands of table.inserts.
>
> A binding for lua_createtable should be nice, but I thought in something beyond. I suppose it could be interesting if we could give the default value to the occurrencies created by the function and, in case of this value be a function, it would be called to return the value and the key of the element. What do you think about this?

I don't think this is an entirely unreasonable request. There are
other instances in Lua where the language provides functions that
offer a performance advantage over doing the same task in Lua. For
example, unpack can be implemented in Lua:

local function aux_unpack (list, i, j, ...)
  if i > j then
    return ...
  else
    return aux_unpack(list, i, j-1, list[j], ...);
  end
end

function unpack (list, i, j))
  i, j = i or 1, j or #list
  return aux_unpack(list, i, j)
end

Unfortunately this is extremely slow for many (internal) reasons. In
contrast, the C implementation of unpack is quite fast.

As has been demonstrated in the past, it is very difficult to create a
table of specific size without using distasteful methods such as
compiling a script that returns a table with dummy variables to
enforce a certain size. Here is an example implementation:

function createtable(narr, nrec)
  local t = loadstring("return {"..("1,"):rep(narr)..("a =
1,"):rep(nrec).."}")();
  for k in pairs(t) do t[k] = nil end
  return t;
end

Obviously this will render terrible performance.

Perhaps the Lua authors would be inclined to offer a standard C
function wrapper for lua_createtable that will alleviate the need for
hacks similar to the above.

--
-Patrick Donnelly

"Let all men know thee, but no man know thee thoroughly: Men freely
ford that see the shallows."

- Benjamin Franklin