So if you have a contiguous and homogeneous pool of objects, aiming cache coherence / un-fragmented memory, should you access it directly to keep those memory friendly features?
I created a pool class, but the pool (fixed array) is private, and I want my systems to iterate over the allocated elements (elements on the pool that are in use).
I dont want to make the pool public for obvious reasons (or perhaps I should?), and I cant make it friend of my systems, cause its a templated pool, Id need to put friends declaration for every system..
So after some thinking I come with something like PoolAccess class, which have pointers to the pool data, so it can access pool elements with a function T* GetAllocated(int index). So in the pool class I have a GetAccess that returns PoolAccess that the systems can get.
//basic structure
template<typename T, uint SIZE>
class Pool{
public:
class PoolAccess{
PoolAccess(T pool[], Roster * roster );
T* GetAllocated(uint index);
private:
m_pPoolRef;
m_pRosterRef;
};
PoolAccess GetAccess(){
return PoolAccess(&m_pool, &m_roster);
}
T*Alloc();
void Free(T*);
private:
Roster m_roster; // the dude that controls the free list(raped from insomniac gdc canada presentation)
T m_pool[SIZE];
};
But than I realized, doesnt that send the cache friendly goal to space?
GetAllocated basically will go trough the m_pRosterRef pointer, go to the given index on the "list" of allocated (also a contiguous array), get the index on the pool, go trough pPoolRef pointer to return the pool element address at the index..
My drawablesQueue have similar access, I have a vector of drawables and a sorted vector of indexes to the drawables, but I remember Hodgman and Spiro saying it was still better accessing that way instead of sorting the drawables itself, so I guess its the same..?almost? Not really?