lua-users home
lua-l archive

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


2009/1/8 Raymond Jacobs <raymondj@gmail.com>:
> Any thoughts on the boxing and unboxing of 10,000 elements (say 32 bytes
> each) into a lua table, and processing them with a lua function?

I'm assuming all elements are some structured objects. The easiest way
to manipulate in Lua them is to build a table of tables, and then
convert them back to a C array.

Converting them to tables would be rather expensive. What you can do
in a first optimization step is wrap them in a userdata. You could
wrap the whole array in a first userdata, with an __index that would
create sub-userdata that point to the array elements. These second
userdata __index/__newindex would then allow you to access individual
fields of the structure.

Creating a lot of full userdata is not free either. If that's not fast
enough, you could then create a couple more specialized function in a
second optimization step, something like that:

setfield(udata, index, field, value)
getfield(udata, index, field)

where udata is the big array userdata (that would no longer need a
metatable), index is the index of the object you're accessing and
field is the name of the field in the element you want to access.

If you think one or two optimization steps ahead, without implementing
them, you can write your 'slow' code to make these optimizations easy
to implement later, without making that 'slow' version of the code any
more complex.