lua-users home
lua-l archive

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


Zhu Ya Dong wrote:
> ffi.cdef[[typedef struct point{int x; int y;};]]
> pt = ffi.new('struct point', {1, 2})

It's more efficient to write that as:

  pt = ffi.new('struct point', 1, 2)

> If we then want change 'pt' to (10, 20), instead do staff like
> "pt.x=10;pt.y=20",
> can we do it like this:
> --start--
> ffi.renew(pt, {10, 20})
> --end--

You can already do that if you put the struct into a 1-element
array. Or better yet, keep many small structs in a big array:

  pts = ffi.new('struct point[?]', 100)
  ...
  pts[42] = {10, 20}

But please note that table initializers for FFI data structures
are not JIT compiled, yet. It's also not always possible to remove
the table allocation.

So it may not be as convenient to write, but it's much faster to
use individual assignments. Or use a parallel assignment:

  pt.x, pt.y = 10, 20

--Mike