Re: ffi - C array to lua table ?

  • From: demetri <demetri.spanos@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sat, 11 Jul 2015 15:57:02 -0700


This isn't time critical stuff but I'd like to know if this

normal or whether I do something wrong?


I'm hardly an expert on "normal", and I would speculate
that Lua in particular has more "weird" deployments than
most other languages -- so even the notion of "normal"
Lua/LuaJIT might not be that useful :) Having said that ...

The way I see it you're getting some additional memory
safety (by doing your work on tables instead of arrays).
And you get some (possibly a lot) more convenience from
using familiar table functions, or libraries that assume Lua
tables.

You're paying with some extra memory traffic, GC load,
and CPU cycles doing the conversions.

Depending on the size of the tables you may also care
about the increased risk of exhausting the space available
for pure Lua (vs ffi.C.malloc) data structures.

Finally, if you weren't restricting to floats (and e.g. had
int64[]) then you'd also be running the risk of range
incompatibilities converting back and forth to Lua numbers.

Whether that's a good tradeoff is entirely up to you :) But
I don't see anything wrong or inherently unusual about it.
Convenience and safety are valuable, so, if the costs listed
above are insignificant for your work, it sounds like a
perfectly reasonable choice.

Demetri

Other related posts: