lua-users home
lua-l archive

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

On Wed, Jun 10, 2009 at 3:42 PM, Niklas Frykholm<> wrote:
> 2009/6/10 Duncan Cross <>:
>> On Wed, Jun 10, 2009 at 2:55 PM, Niklas Frykholm<> wrote:
>>> 2009/6/10 Olivier Hamel <>:
>>> Currently, if you want to create Complex or Vector3 type you have to
>>> do it as a table or a userdata. And if you do a serious ammount of
>>> processing on them, the ammount of garbage generated by simple
>>> arithmetic operations will soon put a significant strain on the
>>> system. (You could use a pool of such objects, but that would mean
>>> resorting to manual memory management with all its pains - especially
>>> when you are using it for something as simple as numbers.)
>> Perhaps I am misunderstanding, but if the pool is a table that has
>> been set to have weak values (i.e. its metatable's __mode field is set
>> to 'v'), you should not have to do any manual memory management -
>> values that only exist in the pool will be eligible for garbage
>> collection.
> My description was a bit short, but the whole point of having a pool
> was to avoid generating garbage. (By using a free object from the
> pool, rather than creating a new one, whenever a new object is
> needed.) But this requires us to manually track which objects in the
> pool are free or not (i.e., manual memory management).
> If the objects in the pool are eligible for garbage collection, then
> the pool doesn't really buy us anything. We will generate the same
> ammount of garbage with the pool as without it.
> // Niklas

Well, it depends - are you ending up with many many separate copies of
exactly the same vector? My assumption of how the pool would work was
that the key used to insert values into the pool would be a serialized
version of the object (for example using 'luaL_pushlstring(L, (const
char*)pvector, sizeof(VECTOR));' to generate the key, assuming your
vector is a simple struct stored in a full userdata) and the pool
would be queried by value to see if an existing vector object could be

But if that isn't the case, and the vectors you are dealing with are
unique in the majority of cases, then sure, it doesn't help. (It also
doesn't help if you need the vectors to be mutable rather than
immutable objects.)