[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: [ Userdata ] __index aware of original lookup value
- From: JeanHeyd Meneide <jm3689@...>
- Date: Wed, 17 Feb 2016 03:09:46 -0500
Dear Mr. Conner,
If I'm understanding how lua_get/setuservalue works, I need the userdata to make it work. Per your example, I would do it so the __index of the userdata's metatable is a function. Then I have access to the userdata and can use `lua_getuservalue` on the userdata to get the lookup table. This is more or less how I was doing it before (except instead of using a lua table with lua_getuservalue, I was looking up into a std::map/boost::map of functions/variables I had stored with the userdata).
The performance bottleneck wasn't the lookup (I had reduced the performance impact to nothing by switching from a C++ unordered_map to a map since there were a very small number of elements and then using "transparent lookup" introduced in the new C++ standard for `map` to not have to create a temporary string). The performance bottleneck for the member function implementation was having to return the retrieved function to lua so it could then call it again when doing `myfoo:blah()`. (If that makes sense).
Since I already had it working before, my goal here isn't whether or not I can do it (I know a way to do it, like the way you said, which makes me somewhat happy that I'm on the right track here!), but I'm trying to get the utmost performance possible, so that this code has no overhead compared to regular C code. This is why I'm trying not to drop into a C function unless I absolutely have to (e.g., with the uncommon case of using member variables in lua code).
As a side question, is it possible to set fields to a userdata directly? When I tried I seemed to crash lua, so I don't think I could set the member functions as "fields" on the userdata itself and then have the __index function like you described?