Skip to main content
gamedev.net
🔒 Locked

SmallObjectAllocator

Started by TonyFish Apr 28, 2004 at 3:45 PM 6 replies 850+ views
Original Post
TonyFish
TonyFish
Hi, I''ve just finished reading about Andrei Alexandrescu''s Small Object Allocator and was wondering if there would be any objections to making this the base object class of all classes within a game engine? Thanks Tony
<Fish>{
SiCrane
SiCrane
Yes. It''s a small object allocator; if its a big object then you shouldn''t derive it from the SmallObject class.
TonyFish
TonyFish
yes but by overloading new, it uses the default allocator for objects over a predefined size.
<Fish>{
TonyFish
TonyFish
Given this, any objections?
<Fish>{
Cygon
Cygon
Yes, you would want to use multiple allocators for different kinds of objects of similar size in order to keep memory fragmentation (and thus, cpu cache fails) at a minimum.

If the size of objects managed by the small object allocator differs greatly, you''ll also waste a lot of memory because each object takes as much memory as the largest object the allocator could possibly host.

It will also be faster (and more convenient) if you work with small helper classes like vectors and matrices on the stack instead of allocating them in the heap (with or without the small object allocator).

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
TonyFish
TonyFish
quote:
It will also be faster (and more convenient) if you work with small helper classes like vectors and matrices on the stack instead of allocating them in the heap (with or without the small object allocator).


So one would have to write a manager to take care of the various allocators? Could someone explain the stack vs. heap thing to me cos my knowledge is a tad hazzy!

[edited by - TonyFish on April 29, 2004 9:10:22 AM]
<Fish>{
mattnewport
mattnewport
I don't think you should be worrying about using a custom allocator if you don't know the difference between the stack and the heap... Try this for starters.

It's been said many times before but it bears repeating because noone ever seems to take it in... Premature Optimization is the Root of All Evil Just write your program with clean code and sensible algorithms and get it working. If it's fast enough, you're done. If it's not profile and optimize the bottlenecks. Memory allocation really shouldn't be an issue in a PC game unless you're doing something stupid. If you're doing something stupid (like allocating and freeing 1000s of objects every frame), fix it by not allocating 1000s of objects every frame rather than trying to use some super-clever custom allocator that you don't really understand. Only if you have some very specific case where you've really thought about it and can't see a better solution should you use a custom allocator - a particle system perhaps. Using a custom allocator for everything is very unlikely to be necessary and just adds unnecessary complexity.

I have great respect for Andrei Alexandrescu btw - I'm not suggesting his code isn't useful, just that you shouldn't use it unless you have a very clear need for it. Don't add unneccessary complexity until you absolutely have to.


[edited by - mattnewport on April 30, 2004 2:34:56 AM]
TonyFish
TonyFish
I do know the difference it''s just that I was under the impression that the SOA performed about the same as stack allocations, or at least he says it does! I obviously need to do some testing.
<Fish>{

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.