Saturday, December 10, 2011

Implement your own memory pool or just use Hoard?

Have you been bothered by the speed of memory allocation?  e.g., new/delete, or malloc/free. Performance wise, they are essentially the same thing. To speed this up, many application implement their own memory pool based allocation mechanism, and then override operator new/delete to allocate/release the memory from the memory pool. for example:

class MemPool
{
 public:
       void *allocate();
       void   release(void *p);
};

class Foo
{
 public:
      static void *operator new(size_t size)
      {
          return m_pool.allocate();
      }

      static void delete(void *p, size_t size)
      {
         m_pool.release(p);
      }
...
};
This seems to be a standard strategy many people are using. It is nothing wrong, but if you have such a memory pool in your project, you should compare its performance against Hoard's, the result may surprise you.


This happened exactly to me recently.

If I only create and delete Foo from one thread, my memory pool is much faster than Hoard;

However, if I do this in multi-threads, say, 4 threads, my pool become much slower than Hoard, and the amazing thing about Hoard is it's performance is not affected by the number of threads at all.


My pool is a very normal pool, just pre-allocates enough space, and from then on it does not allocate memory from the system anymore.  so it should be pretty faster than the plain new/delete.  However, to manage the free list, I used a user space spin lock, and this becomes the main reason of the slowness compared to Hoard.


Hoard, however, has the self-learn adaptive ability, it keeps different heap for different threads, and hence most likely requires very less lock contention among different threads.

Please check http://www.hoard.org/ to learn more detail about Hoard

1 comment: