Re: Possible to store a reference to a lua table in an ffi struct?

  • From: demetri <demetri.spanos@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 21 Mar 2014 23:04:48 -0700

Assuming you only want to pass *references* and don't need to access the
Lua table from C/C++ code directly, take advantage of the fact that tables
can be table keys:

1. Create global tables tbl2uid and uid2tbl
2. Define a function registerTable(tbl) that puts tbl as a key into
tbl2uid, with value = next successive integer. Do the reverse mapping in
uid2tbl.
3. Pass around table references as integers rather than pointer types

I know this will seem inefficient at first (two globals + double
indirection!). But we write high performance numerical computing code and
this has never been a bottleneck for us. If this ends up being your
bottleneck you are probably already very close to optimality anyway (or,
alternatively, need to ditch hash maps as your record structure and start
using C structs).

Demetri

Other related posts: