• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mussi

Cache friendly dynamic allocation

7 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
[quote name='Mussi' timestamp='1323815736' post='4893655']How can I achieve this without the system knowing about the different components?[/quote]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.
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
[quote name='Mussi' timestamp='1323857666' post='4893789']
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?
[/quote]
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
I think there's an issue with the use of the word "[b]alignment[/b]" 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.
1

Share this post


Link to post
Share on other sites
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:
[quote][i]Data alignment[/i] 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 [url="http://en.wikipedia.org/wiki/CPU"]CPU[/url] 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.
0

Share this post


Link to post
Share on other sites
Yeah that's exactly what I meant Nohup, you beat me to it.
[quote]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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0