Memory Pools support in Phobos

bearophile bearophileHUGS at lycos.com
Fri Nov 12 04:49:47 PST 2010


F. Almeida:

> Regardless of the direction chosen for delete and clear(), Phobos is still lacking in functions that ease the management of the C heap.

I think Andrei agrees with you, he discussed this a bit in past.

I agree, adding few manual memory management functions/structs to Phobos will help a lot lower level coding. The good thing is that I think only one or few hundreds of lines of code may be enough to solve most of this problem.

The data structures I'd like in this manual memory management Phobos module:

1) In my dlibs1 I have a "simple" struct that acts like a pool:

struct Foo {} // some struct
alias MemoryPool!Foo FooPool;

And then you may ask FooPool: a new Foo, clear them all, deallocate them all (if you want you may also add a freelist of Foo so you may deallocate only one of them). Surely there are ways to design something more general than this, but in lot of cases this was enough for me, and I have seen programs become twice faster just using this MemoryPool.

2) A second data structure that may be useful is a memory arena, where you allocate different sized objects.

3) A third data structure I'd like in Phobos is a hierarchical allocator, it's very easy to use, easy to implement, but it is powerful:
http://swapped.cc/halloc/

4) This old code by Walter, the mem.c/mem.h memory management utility is partially obsolete, but some of its ideas may be useful for people that use the C heap manually from D:
http://c.snippets.org/code/temp/snip-c.zip

5) This is a kind of hack, but helps a little: a function that carves a whole 2D dynamic array of dynamic arrays matrix out of a single C-heap allocated memory object (it has to respect alignments of its basic item, so sometimes it adds a bit of padding). This helps a little reduce memory usage and sometimes increases performance because of higher cache coherence.

6) An unsafe growable dynamic array allocated backwards on the stack :-) This works, but you need to use it carefully, not defining new variables in the middle.

Bye,
bearophile


More information about the Digitalmars-d mailing list