Cache friendly dynamic allocation

Started by
6 comments, last by Mussi 12 years, 4 months ago
Hi there,

I'm working on a component entity system(seems to be popular these days) and I'm looking for a way to have components of the same type aligned in memory. How can I achieve this without the system knowing about the different components? A code sample for more insight:
[source]typedef std::vector<Component*> Components;
typedef boost::unordered_map<std::string, Components> ComponentMap;
/*..*/
template <class T>
T* CreateComponent()
{
T* t = new T; //components will not be aligned in memory
componentMap[ typeid(T*).name() ].push_back(t);
return t;
}[/source]
Is there a relatively easy way to do this?
Advertisement
How can I achieve this without the system knowing about the different components?
It would be much easier if you could use information about the components.
E.g. when you parse a level you see it has 6 hand-placed monsters and 10 monster spawners, each spawner says it can spawn a maximum of 10 alive monsters at a time, so you know your monster pool has to be of size 106, and you create a pool allocator of this size for use by the monster component system.
Thank you for your reply, I'd rather have a general solution though. A friend of mine informed me that most of the time the compiler does this for you, news of the same type are usually aligned in memory. Can anyone confirm this?
In any case, isn't it possible to store a pointer to a dynamically allocated vector and use casting to push back new elements?

Thank you for your reply, I'd rather have a general solution though. A friend of mine informed me that most of the time the compiler does this for you, news of the same type are usually aligned in memory. Can anyone confirm this?
In any case, isn't it possible to store a pointer to a dynamically allocated vector and use casting to push back new elements?

Components shouldn't have any state so there for you should only have one component of each type and thus allignment is not that much of a matter in the componenent manager, and thus x-byte allignment will be fine.

The entity is the one that encodes all the state in the attributes that are attached to it.


The following line should force alignment for each variable declared that way, MSVC only I think GCC might have another definition
[source=cpp]__declspec(align(SIZE)) <VarName>class __declspec(align(16)) AllignedClass{int m_data;}[/source]

If you put that between class and the name of your class you should get a class that is alligned on SIZE, your best bet would be 4,16 or 32 ofcourse, D3DX defines there matrix classes as 16 byte aligned. Be aware that this can increase the memory usage of your application if you haven't layed out the data in your classes properly.

The compiler doesn't do any allignment for you, I think your friend get confused and meant that the offset within a class for a variable is based on the address of the previous one, which is true except when that variable is an alligned one. If it is both the offset and the allignment come into play in the fact that if the previous variable doesn't end on the allignment vaboundarty the compiler will pad it so that it reaches the alignment boundary, if it does nothing happens. When it has to pad you are going to waste space so becareful with aligned data types and try to place them on the alignment boundaries if you can. This sounds like a premature optimisation but it is not one, it will avoid you running out of memory when using this in restriced pools.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

The compiler itself won't do that for you. The memory allocator used by the libraries that come with the compiler may do that automatically to some extent for small allocations.

If you want manual control of where they are allocated, the simplest option is to override operator new and delete on each component and use a pooled allocator.

However my recommendation would be to do nothing until you find out it's a problem from profiling. It's very easy to spend lots of time optimizing for things which may not turn out to be a problem, and would be no harder to fix later on if there is a problem.
I think there's an issue with the use of the word "alignment" in your post.
That word has a slightly different meaning than what you're considering. It's more to do with the exact placement of something within the memory banks of a machine along certain boundaries (byte, word, page).

If I read you right, then you don't really care about exactly where a thing is placed.
You're more concerned that all the things you make are placed in an orderly manner, and tightly packed, to get better cache utilization.

If that's the case, then you were going along the right path with your thoughts of using a vector to store your components.
The problem with a vector is that all the types in a vector must be the same. And from your code-snippet, you were thinking more of using a common interface to a set of different derived classes (components).

Your code snippet shows that you want to store pointers to various types of components. That's a pretty common method. But I'm afraid, that it is unlikely that those components will ever be near each other in memory (cache coherent).

The best you're likely to get, is having all of your components of a certain type together in memory. For instance, the "Health" components can all be pre-allocated in a vector/array, and can be tightly packed together.
Your entities will store pointers to those components (as in your boost:unordered data structure).
Then, if you can structure your code to run all of the entitiy's "health" processing at the same time, your cache-utilization will be very good.
Do this for every component type, and you should get most of what you're looking for.

Now, managing a whole bunch of vectors for every component type might be a bit daunting. Every time an entity is destroyed, you will have to remove it's components from their storage vectors, or somehow mark them as free. That can be a bit of a pain, and could lead to a lot of bugs. My advice is to use a freelist or some pooling allocator to do the job for you.
(Game Programming Gems 4 has a good article on freelists, and boost::pool has a nice implementation that you could use to help manage the lifetimes of the components

Once you have all the components in their homogeneous vectors, you might start looking for a way to just iterate over all the components within those vectors, without even considering what entity they belong to.
Like the health example, you could check all the healths to see if any are <0, and if so look up that health's entity ID, and create a message for that entity to "die".
Lots of flexibility in this setup, it's a lot like what I've been toying around with for a while.

good luck.
First of all, thank you for all the replies. I might have been using the wrong terminology here, I think I meant contiguous instead of aligned. Quoting from Wikipedia:
Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory.[/quote] This is actually not what I meant :P. What I meant was related to spatial locality, data being close to each other storage location wise.

Alignment is a very interesting topic, but I think that'd be too low level for me to explore now. So I'll restate my original question: is there an easy way for me to sore components of the same type in contiguous storage locations? I think I answered this in my second post.
Yeah that's exactly what I meant Nohup, you beat me to it.
For instance, the "Health" components can all be pre-allocated in a vector/array, and can be tightly packed together.[/quote]
I was looking for a way to do this without knowing the different types of components, I think got this now.

This topic is closed to new replies.

Advertisement