|
On Jun 11, 2014, at 11:52 AM, Lars Ruoff <lars.ruoff@gmail.com> wrote:
This comes down to how much efficiency you need, and if you want the vectors to be opaque or transparent to Lua code If you want Lua to act as a custodian of opaque vectors that are operated on via a C library, then userdata is your friend. You can, of course, expose the C API through a metatable and then have a nice OO style vector interface to your C library. This is pretty fast and compact, but perhaps cumbersome if you want to make the vectors more transparent to Lua. If you go the table route, be aware that Lua tables do have more overhead than a userdata. Also, it’s typically somewhat faster (and more compact) to store the X,Y parts in the array part of the table (so, use v = {1, 2} instead of v = { x = 1, y = 2 } ), effectively treating them more like a tuple. If you have a LOT of vectors to work with in arrays, storing them in column form is much more efficient (that is, two arrays, one of X values, one of Y values). For a million vectors, this approach uses two tables instead of one million, and is MUCH more efficient in memory usage. —Tim |