Search code examples
c++boostpolymorphismshared-ptrboost-ptr-container

Increasing Speed of Destruction


I have an application that creates thousands of small objects (upwards of 500,000). There is an object factory that allocates these objects on the heap.

The problem that I'm running into is that when the object that holds these smaller object goes out of scope (Driver), 65% of the processing time is spent destroying these small objects.

map, entries, and fields hold pointers to an abstract base class, and each base class has many child classes.

The application architecture follows this format:

class Driver {

    boost::ptr_map<std::string, Class1-Base> map;
}

class Class1-Base {

    boost::ptr_vector<Class2-Base> entries;
}

class Class2-Base {
    boost::ptr_vector<Class3-Base> fields;
}

class Class3-Base {
    unsigned long value;
}

I have tried several different methods to increase the application's performance.

I first used a data structures with normal pointers and then explicitly deleted the objects in the class's destructor.

I then tried using data structures with boost::shared_ptr<> but I determined that reference counting caused a significant overhead and didn't provide any great benefit.

The solution I've come to now is to use boost::ptr_container so it takes ownership of the heap objects and will automatically destroy them when the container goes out of scope. With this solution, significant time is still spent destroying objects.

Is there anything I can do to prevent all this time destroying objects?


Solution

  • I'd suggest using a memory pool to allocate the elements from, e.g. using Boost Pool library.

    If you don't actually require destruction per element (i.e. the elements themselves have trivial destructors, although they, obviously cannot be POD since they have virtual members) you can avoid destructing the elements altogether, and release the entire pool in one fell swoop. This removes the dynamic allocation bottleneck from the equation.

    Related:


    As a simple measure (the low-hanging fruit) consider using a drop-in fast heap library, such as libtcmalloc from google-perftools.

    Related: