lua-users home
lua-l archive

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


Am 22.04.2014 08:44 schröbte steve donovan:

The trouble is that it's very common to wrap a C/C++ pointer in a
small full userdata type (pointer to pointer) which is wasteful if
you're really worried about all these extra objects needing to be
GC'd.  (My question would be: yes, but it does it make much difference
in real projects?)

I use interning for such userdatas (and for enums/flags): You store a mapping of the wrapped C pointer to the full userdata in a weak-valued table. Whenever you want to create a new userdata you reuse the userdata in the cache if possible. This should reduce all those extra memory allocations. For enums/flags you need one cache per enum type (I usually put it somewhere in the metatable).

Which brings me to another topic: I'd rather have different integer types (for typesafe enums/flags, only bit operators, no arithmetic operators) than different lightuserdata types. One 32-bit subtag value and a 32-bit enum value would fit comfortably in those 64 bits we have in a Lua value ...

Philipp