Thanduel Posted July 23, 2015 Report Share Posted July 23, 2015 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? 1 Quote Link to comment Share on other sites More sharing options...
leper Posted July 23, 2015 Report Share Posted July 23, 2015 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. 2 Quote Link to comment Share on other sites More sharing options...
Thanduel Posted July 23, 2015 Author Report Share Posted July 23, 2015 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. Quote Link to comment Share on other sites More sharing options...
Stan` Posted July 24, 2015 Report Share Posted July 24, 2015 What are the benefits of a memory pool ? Quote Link to comment Share on other sites More sharing options...
Thanduel Posted July 24, 2015 Author Report Share Posted July 24, 2015 Free cookies on Sundays.And you can achieve much faster and much more stable allocation times for dynamic memory allocation. Quote Link to comment Share on other sites More sharing options...
Stan` Posted July 24, 2015 Report Share Posted July 24, 2015 So basically, slight improvement in performance for a quite time consuming stuff ?Why don't compilers use it as default ?And Cookies : Nom Nom Nom... Quote Link to comment Share on other sites More sharing options...
Thanduel Posted July 24, 2015 Author Report Share Posted July 24, 2015 It can be an order of magnitude faster:http://www.codeproject.com/Articles/27487/Why-to-use-memory-pool-and-how-to-implement-itIf 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. Quote Link to comment Share on other sites More sharing options...
Stan` Posted July 24, 2015 Report Share Posted July 24, 2015 That sounds pretty neat Quote Link to comment Share on other sites More sharing options...
Yves Posted July 26, 2015 Report Share Posted July 26, 2015 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". Quote Link to comment Share on other sites More sharing options...
Thanduel Posted July 28, 2015 Author Report Share Posted July 28, 2015 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. Quote Link to comment Share on other sites More sharing options...
Thanduel Posted August 3, 2015 Author Report Share Posted August 3, 2015 I've created a ticket for this and begun to work on it. So far I've just finished getting to grips with RedFox's code.http://trac.wildfiregames.com/ticket/3362Hopefully I'll have something useful to report back in a few weeks. Quote Link to comment Share on other sites More sharing options...
Stan` Posted August 3, 2015 Report Share Posted August 3, 2015 I've created a ticket for this and begun to work on it. So far I've just finished getting to grips with RedFox's code.http://trac.wildfiregames.com/ticket/3362Hopefully I'll have something useful to report back in a few weeks. Good news =D Quote Link to comment Share on other sites More sharing options...
historic_bruno Posted August 5, 2015 Report Share Posted August 5, 2015 This is useful to work on. In the past I've noticed many small allocations and a rather fragmented heap, when troubleshooting memory leaks on Windows. It may not be the complete solution, but it's probably a step in the right direction. As you say, figuring out how to use it will be the hardest part. Quote Link to comment Share on other sites More sharing options...
Yves Posted August 6, 2015 Report Share Posted August 6, 2015 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. 1 Quote Link to comment Share on other sites More sharing options...
Thanduel Posted August 7, 2015 Author Report Share Posted August 7, 2015 (edited) 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 August 7, 2015 by Thanduel 1 Quote Link to comment Share on other sites More sharing options...
wraitii Posted November 2, 2015 Report Share Posted November 2, 2015 I believe we already have libraries to do memory pooling (and indeed, do in a bunch of places). We probably need to work on it, but the underlying stuff is there I think. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.