Jump to content

Memory Pools


Recommended Posts

Hi 0ad Devs!

About two years ago there was an attempt at converting 0ad's dynamic memory usage to a memory pool system. Eg ticket #2020 which is closed and marked wontfix.

Did anything ever come of this, I know RedFox's mega patch had to be broken up, but as far as I can see little of his code made it into the main 0ad repo. Were there specific technical reasons why this approach was rejected?

  • Like 1
Link to comment
Share on other sites

Did anything ever come of this,

No.

I know RedFox's mega patch had to be broken up, but as far as I can see little of his code made it into the main 0ad repo.

There was not a lot of useful code (not breaking everything, removing error checking, ...) that was worth salvaging. See the (unfinished, though from looking through the remainder there isn't a lot of useful code in there) split up attempt for some details http://git.wildfiregames.com/gitweb/?p=0ad.git;a=shortlog;h=refs/heads/projects/philip/megapatch-split.

Were there specific technical reasons why this approach was rejected?

#2020 and all his other tickets were succeeded by the "megapatch" (if you need a zip file of a patch that is a bad sign, if that zip doesn't contain a series of patches with reasoning (commit messages) for each of them you're doing it horribly wrong). If you take a look at the patch at #2020 and the corresponding commit in the split up attempt you'll notice that there are some tests in one that aren't in the other. Not that this patch or the megapatch was actually supported in any way...

So there is no issue with adding a custom allocator utilizing memory pooling, but since from a quick glance that patch is missing some error handling and wasn't tested (it is neither used in the patch at #2020 or in the megapatch) so it could be broken. If someone is willing to work on this I'd suggest taking a look at the patch and then starting from scratch.

  • Like 2
Link to comment
Share on other sites

Thanks for your response Leper. I had hoped that more work had happened behind the scenes. I think it would be a worthwhile endeavour to use a memory pooling system. It is just a mamoth task to integrate into the codebase and tune it to be the right size for the game. I'm having to work on a pure C memory pool implementation for work, perhaps I can find some overlap and contribute this. We will see.

Link to comment
Share on other sites

It can be an order of magnitude faster:

http://www.codeproject.com/Articles/27487/Why-to-use-memory-pool-and-how-to-implement-it

If it were just a slight improvement it wouldn't be worth it.

C++ compilers use a general purpose allocation system that is good enough for most applications. They also provide a mechanism for people to use special purpose allocators. So for example when you instantiate an STL data structure you can pass in a custom allocator for it to use.

A memory pool system pre-allocates memory and divides it up into set sized portions that can be very quickly handed out. It allows you to circumvent the long search times for malloc to allocate memory off the heap.

Link to comment
Share on other sites

The first example in the codeprojects article is quite strange. Doesn't it just change from heap to stack allocated memory? It does avoid searching for free memory on the heap because it can just add sizeof(CTestClass) to the stack pointer, but I wouldn't call this a memory pool, not even a "simple memory pool".

Link to comment
Share on other sites

Despite the fact that in the example he uses stack allocation it still serves as a good illustration of the potential performance difference between normal heap allocation and a memory pool. This is because the main function of a memory pool is to return a pre-allocated memory location of a specific size. In the case of the example this performance difference is exaggerated because there is only one pool of memory to return so no logic is required to determine which pool to return but the outcome is still relevant. This is because the source of the memory for the pool is irrelevant ie stack or heap. The point is the machinery required to get memory from a pre allocated source is much faster.

In the case of 0ad you would pre-allocate a huge chunk of memory on the heap and divide it into set sizes in the initialisation. During run time the main machinery would be to pass out the next free block of a size that satisfies the memory request. This adds some logic but that logic would never be slower than a search through the heap.

Link to comment
Share on other sites

I recently found this paper (around 100 pages) from Ulrich Drepper named "What every programmer should know about memory". It was very interesting and I red most of it in three days. It's not specifically about memory pools, but covers many performance aspects of memory and I learned a lot from it.

Online-Version: http://lwn.net/Articles/250967/

PDF Download: http://lwn.net/Articles/259710/

I'm sure that improving memory efficiency can help us a lot to improve performance. However, I would first try to pick some spots where you can prove that memory is the bottleneck and then try to resolve the problems there. Just adding a whole memory pooling system for everything sounds like a lot of work with possibly little effect on performance. To cover everything, a solution has to be generic and the real problems might need more specific solutions.

  • Like 1
Link to comment
Share on other sites

Just to start there was a large effort originally to show that memory was a significant issue. This can be seen in the original posts connected with this work and all the profiling posted in that thread. Whilst I do not think that the memory improvement would solve the performance problems related to algorithmic issues I do think it will be more than just a fractional improvement. However, to verify this my plan is to target specific areas that are critical to performance first like the unit ranging code or the pathfinder. I agree that just changing the entire codebase to use a memory pooling system without first verifying that it is helpful would be stupid a lot of work for little benefit.

Thanks for the link to paper.

Edited by Thanduel
  • Like 1
Link to comment
Share on other sites

  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...