Fastest way to transfer data from C to Lua ?

classic Classic list List threaded Threaded
36 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Fastest way to transfer data from C to Lua ?

Geoff Smith
Hi
 
I have an application where I have generated a bunch of ADC samples from a chip that I need to transfer over to Lua.
Each sample comprises of a sample time and an adc value, I have approximately 5000 samples to transfer to Lua.
 
As my CPU is orders of magnitude slower than a typical PC, I am pondering what would be the best way of doing this, as regards simplicity, minimum memory usage, and fastest speed.
 
I discounted userdata as I only ever need to get data in one direction C --> Lua (and also I don't understand userdata too well)
 
So I am looking at doing it via the C lua table api functions. I would have nested tables, but should the inner table be a hash type table or an array type table ?
 
An inner Hash table would give me on the Lua side this nice readable type of syntax
 
myTime =  mySamples[434].time
myValue =  mySamples[434].value
 
 
An Array type inner table would give me  (not quite so readable)
 
myTime =  mySamples[434][1]
myValue =  mySamples[434][2]
 
The second method sounds like it would be faster, and less memory to store on the Lua side. But I wouldn't be surprised if Lua did some tricks for the hash table example where it optimises its key string storage so it doesn't need to store the key strings "time" and "value" 5000 times. So maybe it would be of similar memory cost, and maybe only slightly slower ?
 
Would be interesting if someone more knowlegable could speculate on how they compare ?
 
 
 
 
Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Javier Guerra Giraldez
On Thu, Jun 6, 2013 at 9:32 PM, Geoff Smith <[hidden email]> wrote:
> I wouldn't be surprised if Lua did some tricks for the hash table example
> where it optimises its key string storage so it doesn't need to store the
> key strings "time" and "value" 5000 times.


string are interned immutable values in Lua, so they're never stored
more than once.  still, a hash table _is_ more complex than an array,
so there might be a small advantage (both in time and memory space) to
use arrays.

as always, you have to test on your particular case.

one extra point: in the C API it's easy to use char* constants for
table keys, but then you could easily find yourself interning the same
string 5000 times, where only the first one was needed. the other 4999
it recognizes and reuses the first time, but only after hashing it
each time.  since you can populate the whole table in a single C
function call, you can intern the constant string before your main
loop, and keep it in the stack to use as table key.

--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Rena
In reply to this post by Geoff Smith

On 2013-06-06 5:33 PM, "Geoff Smith" <[hidden email]> wrote:
>
> Hi
>  
> I have an application where I have generated a bunch of ADC samples from a chip that I need to transfer over to Lua.
> Each sample comprises of a sample time and an adc value, I have approximately 5000 samples to transfer to Lua.
>  
> As my CPU is orders of magnitude slower than a typical PC, I am pondering what would be the best way of doing this, as regards simplicity, minimum memory usage, and fastest speed.
>  
> I discounted userdata as I only ever need to get data in one direction C --> Lua (and also I don't understand userdata too well)
>  
> So I am looking at doing it via the C lua table api functions. I would have nested tables, but should the inner table be a hash type table or an array type table ?
>  
> An inner Hash table would give me on the Lua side this nice readable type of syntax
>  
> myTime =  mySamples[434].time
> myValue =  mySamples[434].value
>  
>  
> An Array type inner table would give me  (not quite so readable)
>  
> myTime =  mySamples[434][1]
> myValue =  mySamples[434][2]
>  
> The second method sounds like it would be faster, and less memory to store on the Lua side. But I wouldn't be surprised if Lua did some tricks for the hash table example where it optimises its key string storage so it doesn't need to store the key strings "time" and "value" 5000 times. So maybe it would be of similar memory cost, and maybe only slightly slower ?
>  
> Would be interesting if someone more knowlegable could speculate on how they compare ?
>  
>  
>  
>  

Lua does intern strings and reuse them. This not only prevents having duplicate strings in memory, but allows fast string equality tests (only pointers need to be compared).

As for your application, I wonder if stuffing your data into a string (or a C array, with functions to allow Lua to read it, so as to avoid string internalizing overhead) would be faster. Ideally you should benchmark a few methods.

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Geoff Leyland
In reply to this post by Geoff Smith
On 7/06/2013, at 9:32 AM, Geoff Smith <[hidden email]> wrote:

> Hi
>  
> I have an application where I have generated a bunch of ADC samples from a chip that I need to transfer over to Lua.
> ...
> The second method sounds like it would be faster, and less memory to store on the Lua side. But I wouldn't be surprised if Lua did some tricks for the hash table example where it optimises its key string storage so it doesn't need to store the key strings "time" and "value" 5000 times. So maybe it would be of similar memory cost, and maybe only slightly slower ?
>  
> Would be interesting if someone more knowlegable could speculate on how they compare ?

I'm not more knowledgeable, and I won't speculate, but here's a rough test of something that is not the same as what you plan to do.  Your time to fill the array will be different because you're doing it from the API, not in Lua, but this might give an indication?

$ lua test.lua
Hash: 0.690967 s, 153.327474 MB
Array: 0.621593 s, 107.552795 MB


test.lua:

local start = os.clock()
local mem = collectgarbage("count")

local data = {}
for i = 1, 1000000 do
  data[i] = { time = math.random(), value = math.random() }
end

collectgarbage("collect")
io.stdout:write(("Hash: %f s, %f MB\n"):format(os.clock() - start, (collectgarbage("count") - mem) / 1024))

data = nil
collectgarbage("collect")
local start = os.clock()
local mem = collectgarbage("count")

data = {}
for i = 1, 1000000 do
  data[i] = { math.random(), math.random() }
end

collectgarbage("collect")
io.stdout:write(("Array: %f s, %f MB\n"):format(os.clock() - start, (collectgarbage("count") - mem) / 1024))




Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Coda Highland
In reply to this post by Geoff Smith
On Thu, Jun 6, 2013 at 2:32 PM, Geoff Smith <[hidden email]> wrote:

> Hi
>
> I have an application where I have generated a bunch of ADC samples from a
> chip that I need to transfer over to Lua.
> Each sample comprises of a sample time and an adc value, I have
> approximately 5000 samples to transfer to Lua.
>
> As my CPU is orders of magnitude slower than a typical PC, I am pondering
> what would be the best way of doing this, as regards simplicity, minimum
> memory usage, and fastest speed.
>
> I discounted userdata as I only ever need to get data in one direction C -->
> Lua (and also I don't understand userdata too well)
>
> So I am looking at doing it via the C lua table api functions. I would have
> nested tables, but should the inner table be a hash type table or an array
> type table ?
>
> An inner Hash table would give me on the Lua side this nice readable type of
> syntax
>
> myTime =  mySamples[434].time
> myValue =  mySamples[434].value
>
>
> An Array type inner table would give me  (not quite so readable)
>
> myTime =  mySamples[434][1]
> myValue =  mySamples[434][2]
>
> The second method sounds like it would be faster, and less memory to store
> on the Lua side. But I wouldn't be surprised if Lua did some tricks for the
> hash table example where it optimises its key string storage so it doesn't
> need to store the key strings "time" and "value" 5000 times. So maybe it
> would be of similar memory cost, and maybe only slightly slower ?
>
> Would be interesting if someone more knowlegable could speculate on how they
> compare ?
>

This is one of the classic examples of where LuaJIT shines -- in
LuaJIT you can actually use a pointer to an array of C structures and
get C-like performance out of it. Yes, you're penalized by having a
C->Lua call in there, but after the handoff you're golden.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Tim Hill
In reply to this post by Geoff Smith

One thing to be aware of if you are running on a *really* resource constrained system is that you are better off with fewer, bigger, tables. Consider storing 1000 (x,y) pairs. The obvious way to do this is to store 1000 {x,y} tables in an array. However, although it's not as elegant, you are better off storing this as two simple arrays of 1000 elements, one containing the x values, one the y. This second method uses only two tables instead of 1,001.

And yes, the code is FAR less readable. Also, of course, if you do use this method, the whole keys vs indices argument goes away.

--Tim

On Jun 6, 2013, at 2:32 PM, Geoff Smith <[hidden email]> wrote:

The second method sounds like it would be faster, and less memory to store on the Lua side. But I wouldn't be surprised if Lua did some tricks for the hash table example where it optimises its key string storage so it doesn't need to store the key strings "time" and "value" 5000 times. So maybe it would be of similar memory cost, and maybe only slightly slower ?

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Coda Highland
On Thu, Jun 6, 2013 at 3:36 PM, Tim Hill <[hidden email]> wrote:

>
> One thing to be aware of if you are running on a *really* resource
> constrained system is that you are better off with fewer, bigger, tables.
> Consider storing 1000 (x,y) pairs. The obvious way to do this is to store
> 1000 {x,y} tables in an array. However, although it's not as elegant, you
> are better off storing this as two simple arrays of 1000 elements, one
> containing the x values, one the y. This second method uses only two tables
> instead of 1,001.
>
> And yes, the code is FAR less readable. Also, of course, if you do use this
> method, the whole keys vs indices argument goes away.
>
> --Tim

Alternatively, one array of 2000 elements, interleaved odd/even, which
could potentially have performance benefits beyond just memory
consumption. (Could. Not guaranteed. Profile, profile, profile.)

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Rena
In reply to this post by Tim Hill

On 2013-06-06 6:36 PM, "Tim Hill" <[hidden email]> wrote:
>
>
> And yes, the code is FAR less readable.

This is where accessor functions can be a worthwhile tradeoff. For a little extra overhead (still less than using another data structure, possibly none if you use macros or convince the compiler to inline) you can abstract away the messiness of accessing elements, and even be able to switch the underlying data structure transparently.

(Or you can do what my phone's touch keyboard seems to be insisting and access wieners instead of elements.)

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Chris Emerson
In reply to this post by Geoff Smith
Hi,

On Thu, Jun 06, 2013 at 10:32:50PM +0100, Geoff Smith wrote:
> As my CPU is orders of magnitude slower than a typical PC, I am pondering
> what would be the best way of doing this, as regards simplicity, minimum
> memory usage, and fastest speed.

What are you doing with the data once you've got it into Lua?  I would guess
you'd want to factor in how efficient it is to use it later, not just getting
it in to Lua in any form.

Regards,

Chris

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

David Heiko Kolf-2
In reply to this post by Tim Hill
Tim Hill wrote:

> One thing to be aware of if you are running on a *really* resource
> constrained system is that you are better off with fewer, bigger,
> tables. Consider storing 1000 (x,y) pairs. The obvious way to do this is
> to store 1000 {x,y} tables in an array. However, although it's not as
> elegant, you are better off storing this as two simple arrays of 1000
> elements, one containing the x values, one the y. This second method
> uses only two tables instead of 1,001.
>
> And yes, the code is FAR less readable. Also, of course, if you do use
> this method, the whole keys vs indices argument goes away.

Storing it in a few big arrays would also be my suggestion and I guess
it could also be quite readable:

   myTime = mySamples.time[434]
   myValue = mySamples.value[434]

Best regards,

David Kolf


Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Tim Hill

Agreed, and you could even wrap it up into a nice virtual table with a metatable binding/unbinding the pairs so they LOOKED like (x,y) pairs to a consumer. Ain't Lua great? :)

--Tim

On Jun 7, 2013, at 9:10 AM, David Heiko Kolf <[hidden email]> wrote:

> Tim Hill wrote:
>>
>> And yes, the code is FAR less readable. Also, of course, if you do use
>> this method, the whole keys vs indices argument goes away.
>
> Storing it in a few big arrays would also be my suggestion and I guess
> it could also be quite readable:
>
>   myTime = mySamples.time[434]
>   myValue = mySamples.value[434]
>
> Best regards,
>
> David Kolf
>
>


Reply | Threaded
Open this post in threaded view
|

RE: Fastest way to transfer data from C to Lua ?

Geoff Smith
In reply to this post by David Heiko Kolf-2
Hi Guys
 
Thanks for all the prompt and interesting replies.
 
To answer the one question, I am probably going to use the data on the Lua side to construct a CSV file and spit it out on a serial port or maybe FTP it as a file. I might even have a crack at plotting a graph using the Lua Google Data Visualisation library. It all rather depends how badly my puny little processor complains about having too much work to do !
 
This is one example where Lua not having an option for 8 or 16 bit number sizes will cost me badly on execution time. I hope Roberto is still thinking optional smaller number types is a good feature for 5.3. I am more enthusiastic about that one possible change than all the features in 5.2 put together. Anyway I digress.
 
Now I have read the replies, I am favouring the individual big tables approach
 
myTime = mySamples.time[434]
myValue = mySamples.value[434]
 
This is quite readable, so I will code it up soon and see how fast it is. I will post some numbers when I have done it for reference.
 
Regards Geoff
 
> Date: Fri, 7 Jun 2013 18:10:13 +0200

> From: [hidden email]
> To: [hidden email]
> Subject: Re: Fastest way to transfer data from C to Lua ?
>
> Tim Hill wrote:
> > One thing to be aware of if you are running on a *really* resource
> > constrained system is that you are better off with fewer, bigger,
> > tables. Consider storing 1000 (x,y) pairs. The obvious way to do this is
> > to store 1000 {x,y} tables in an array. However, although it's not as
> > elegant, you are better off storing this as two simple arrays of 1000
> > elements, one containing the x values, one the y. This second method
> > uses only two tables instead of 1,001.
> >
> > And yes, the code is FAR less readable. Also, of course, if you do use
> > this method, the whole keys vs indices argument goes away.
>
> Storing it in a few big arrays would also be my suggestion and I guess
> it could also be quite readable:
>
> myTime = mySamples.time[434]
> myValue = mySamples.value[434]
>
> Best regards,
>
> David Kolf
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

David Heiko Kolf-2
Geoff Smith wrote:
> This is one example where Lua not having an option for 8 or 16 bit
> number sizes will cost me badly on execution time. I hope Roberto is
> still thinking optional smaller number types is a good feature for 5.3.
> I am more enthusiastic about that one possible change than all the
> features in 5.2 put together. Anyway I digress.

There is already a compile-time option in luaconf.h to use other number
formats, but that would affect all numbers used by Lua.  (And it might
disturb the math library).

If you want to use different numbers just for your array you would need
to go the slightly harder way of implementing the array as userdata.
(Or I guess LuaJIT would be an alternative).  An example for such an
userdata can be found in <http://www.lua.org/pil/28.1.html>.

> Now I have read the replies, I am favouring the individual big tables
> approach
>  
> myTime = mySamples.time[434]
> myValue = mySamples.value[434]
>  
> This is quite readable, so I will code it up soon and see how fast it
> is. I will post some numbers when I have done it for reference.

One further hint for speed, if you are creating the table in C and you
know the total number of elements when creating the table, you can use
the function lua_createtable(L, narr, 0) where narr would be your
variable containing your array size.  Then the necessary memory would be
allocated right from the start and Lua doesn't need to realloc the
memory (which might involve lots of copying).

Best regards,

David Kolf

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Rena
In reply to this post by Geoff Smith
On Fri, Jun 7, 2013 at 2:08 PM, Geoff Smith <[hidden email]> wrote:
Hi Guys
 
Thanks for all the prompt and interesting replies.
 
To answer the one question, I am probably going to use the data on the Lua side to construct a CSV file and spit it out on a serial port or maybe FTP it as a file. I might even have a crack at plotting a graph using the Lua Google Data Visualisation library. It all rather depends how badly my puny little processor complains about having too much work to do !

If all you need is to generate a CSV file, I wonder if it's even worth getting Lua involved?


--
Sent from my Game Boy.
Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Sean Conner
In reply to this post by Geoff Smith
It was thus said that the Great Geoff Smith once stated:
>
> This is one example where Lua not having an option for 8 or 16 bit number
> sizes will cost me badly on execution time. I hope Roberto is still
> thinking optional smaller number types is a good feature for 5.3. I am
> more enthusiastic about that one possible change than all the features in
> 5.2 put together. Anyway I digress.

  You mention having some 5,000 samples, which works out to be around 80k
(5,000 * 8 (sizeof double) * 2 (two samples)) which would certainly be too
large to handle on my first computer (8-bit 6809 with 64K RAM) all my
computers since then would be able to handle that amount just fine.  

  For reference, until about mid-2003 I was using a 486 with 32M RAM for
serving up my website, and I was using a similar box for development until
2009.  

  So now I'm curious---what *are* the specs of the system you are using?

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Jorge Visca
On 07/06/13 22:32, Sean Conner wrote:
> You mention having some 5,000 samples, which works out to be around
> 80k (5,000 * 8 (sizeof double) * 2 (two samples)) which would
> certainly be too large to handle on my first computer (8-bit 6809 with
> 64K RAM) all my computers since then would be able to handle that
> amount just fine. For reference, until about mid-2003 I was using a
> 486 with 32M RAM for serving up my website, and I was using a similar
> box for development until 2009. So now I'm curious---what *are* the
> specs of the system you are using? -spc

Personally, the smallest available RAM i've had when using Lua was a
couple Mb worth.
On the other hand, I was very close to using pbLua (Lua on Lego NXT
controller), and that would've driven the low bar way lower.
And then there are the eLua guys...

Reply | Threaded
Open this post in threaded view
|

RE: Fastest way to transfer data from C to Lua ?

Geoff Smith
Hi Guys
 
Thanks for the further feedback. A few assorted replies to the comments.....
 
I need some pretty precise maths elsewhere in my code, so it was never an option to change conf.h to reduce Lua's default number definition.
 
Yep, I was aware of the feature with lua_createtable() to pre-create the correct number of storage elements, I have used that a fair bit already in smaller tables I have made. If things look too slow, I will have to look at doing it with userdata, to try and optimise the number of bytes passed.
 
To answer the question on specs. I am using an 80Mhz Freescale Coldfire chip, it has got a few Megs of ram allocated for Lua, cant remember the exact figure, but well less than 10 Megs. I am using just bog standard Lua 5.1.5
 
 Why was I passing it to Lua,  when  I said all I wanted was to make a CSV file ? That one is easy to answer. Flexibility !
I have no idea what other folks might want to do with the data ? Heck, I usually don't know what I will end up doing with features and stuff myself a few months down the road, let alone other people :)
 
Getting embedded firmware changed for small tweaks here and there is a pain in the ass, changing a text file to run a tweaked Lua script is  way less hassle.
 
Geoff
 
 
> Date: Sat, 8 Jun 2013 00:53:39 -0300

> From: [hidden email]
> To: [hidden email]
> Subject: Re: Fastest way to transfer data from C to Lua ?
>
> On 07/06/13 22:32, Sean Conner wrote:
> > You mention having some 5,000 samples, which works out to be around
> > 80k (5,000 * 8 (sizeof double) * 2 (two samples)) which would
> > certainly be too large to handle on my first computer (8-bit 6809 with
> > 64K RAM) all my computers since then would be able to handle that
> > amount just fine. For reference, until about mid-2003 I was using a
> > 486 with 32M RAM for serving up my website, and I was using a similar
> > box for development until 2009. So now I'm curious---what *are* the
> > specs of the system you are using? -spc
>
> Personally, the smallest available RAM i've had when using Lua was a
> couple Mb worth.
> On the other hand, I was very close to using pbLua (Lua on Lego NXT
> controller), and that would've driven the low bar way lower.
> And then there are the eLua guys...
>
Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Tim Hill
<base href="x-msg://6/">If you are REALLY memory constrained you could store the information in a userdata and provide helper C functions to extract portions of it (in effect, treating it as an array. You mentioned userdata as being complex; it really isn't:

Full userdata (the original version) is just a block of memory allocated for you by Lua, and stored in a Lua value. The C Api provides a way to access that memory block, and so in C you can treat it like a normal heap allocated block of memory (except that you cannot free it, that is Luas job).

Light userdata is just Lua keeping a wrapper to a C pointer; you must allocate the memory, and free it when no longer needed (and make sure Lua has no stray pointers left).

In either case, you keep the array in the userdata and write a couple of C functions that you can call from Lua to access elements of the array (get/set, probably all you need). You will need to understand the Lua stack, but that's not all that hard, PiL does a good job of explaining it. If you want to be really fancy, and you use full userdata, you can even attach a metatable, override __index, and then you can use Lua [] syntax to access the array (read only).

--Tim


On Jun 8, 2013, at 3:00 AM, Geoff Smith <[hidden email]> wrote:

Hi Guys
 
Thanks for the further feedback. A few assorted replies to the comments.....
 
I need some pretty precise maths elsewhere in my code, so it was never an option to change conf.h to reduce Lua's default number definition.
 
Yep, I was aware of the feature with lua_createtable() to pre-create the correct number of storage elements, I have used that a fair bit already in smaller tables I have made. If things look too slow, I will have to look at doing it with userdata, to try and optimise the number of bytes passed.
 
To answer the question on specs. I am using an 80Mhz Freescale Coldfire chip, it has got a few Megs of ram allocated for Lua, cant remember the exact figure, but well less than 10 Megs. I am using just bog standard Lua 5.1.5
 
 Why was I passing it to Lua,  when  I said all I wanted was to make a CSV file ? That one is easy to answer. Flexibility !
I have no idea what other folks might want to do with the data ? Heck, I usually don't know what I will end up doing with features and stuff myself a few months down the road, let alone other people :)
 
Getting embedded firmware changed for small tweaks here and there is a pain in the ass, changing a text file to run a tweaked Lua script is  way less hassle.
 
Geoff
 
 
> Date: Sat, 8 Jun 2013 00:53:39 -0300

> From: [hidden email]
> To: [hidden email]
> Subject: Re: Fastest way to transfer data from C to Lua ?
> 
> On 07/06/13 22:32, Sean Conner wrote:
> > You mention having some 5,000 samples, which works out to be around
> > 80k (5,000 * 8 (sizeof double) * 2 (two samples)) which would
> > certainly be too large to handle on my first computer (8-bit 6809 with
> > 64K RAM) all my computers since then would be able to handle that
> > amount just fine. For reference, until about mid-2003 I was using a
> > 486 with 32M RAM for serving up my website, and I was using a similar
> > box for development until 2009. So now I'm curious---what *are* the
> > specs of the system you are using? -spc 
> 
> Personally, the smallest available RAM i've had when using Lua was a
> couple Mb worth.
> On the other hand, I was very close to using pbLua (Lua on Lego NXT
> controller), and that would've driven the low bar way lower.
> And then there are the eLua guys...
>
Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to transfer data from C to Lua ?

Sean Conner
In reply to this post by Geoff Smith
It was thus said that the Great Geoff Smith once stated:
>
> To answer the question on specs. I am using an 80Mhz Freescale Coldfire
> chip, it has got a few Megs of ram allocated for Lua, cant remember the
> exact figure, but well less than 10 Megs. I am using just bog standard Lua
> 5.1.5

  Don't forget you can always check Lua's memory usage with

        x = collectgarbage('count')

which returns the amount of memory Lua is using in kilobytes.

  But 10M is quite a bit of memory.  

  -spc



Reply | Threaded
Open this post in threaded view
|

light userdata (Was: Fastest way to transfer data from C to Lua ?)

David Heiko Kolf-2
In reply to this post by Tim Hill
Tim Hill wrote:
> Light userdata is just Lua keeping a wrapper to a C pointer; you must
> allocate the memory, and free it when no longer needed (and make sure
> Lua has no stray pointers left).

In my opinion the term "light userdata" is a bit misleading, as I see
only a very limited value in using it for storing actual data.  You
cannot assign different metatables to each light userdata.  That means
it is later very hard to find out what was actually stored in it.  And I
don't see any way of making sure that Lua has no stray pointers left --
except by closing the entire Lua state.  So even when I need to do
memory handling in C I would store a pointer in a full userdata.

I mainly use light userdata as keys in the registry or similar tables to
map C objects back to Lua values and my light userdata values are never
seen by Lua (without using the debug interface) but just used by the C side.

Instead of "light userdata" maybe "typeless user pointer" would be a
clearer term.

Best regards,

David Kolf


12