[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] State of the math branch



On Nov 16, 2009, at 1:15 PM, Lorenz Quack wrote:

The reason I allocated the memory dynamically is that I didn't want to waste the 2*sizeof(double) on Vector2 instances. but maybe we don't have to be that cheep in this day and age. especially if we can gain some speed. Another consideration is that this way we can *maybe* reuse vectors as views on rows or columns of matrices. I'm not convinced that thats a good idea or feasible but it is a
thought to keep in mind.

Bear in mind that there is some overhead to the allocation itself, plus many implementations allocate memory in minimum-sized, aligned chunks, so I suspect it would not save any memory allocating it separately, in fact it may use more, though it's largely a wash.

In any case I'd say the best reasons not to do it are:

1. Less memory management means less bugs. And memory mgmt bugs are never very nice 8^)

2. Allocation can be relatively expensive. For small objects like this which can be created as a side effect of certain operators in expressions, it can be a measurable win to do it less.

3. It will improve the friendliness of the data structure to the processor cache. Indirection is your enemy in this regard.

I like Rene's suggestion of having a pointer that normally points to a static array in the structure, but can be pointed somewhere else to retain that flexibility. That will still allow you to remove the memory management. In fact you can't use that pointer to index another data structure currently because the implementation assumes that it owns the memory it points to. With Rene's suggestion the pointer is just opaque and the vector code doesn't have to worry about releasing it.

-Casey