1

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?

4

1 回答 1

2

我建议使用内存池来分配元素,例如使用Boost Pool库。

如果您实际上不需要每个元素的销毁(即元素本身具有微不足道的析构函数,尽管它们显然不能是 POD,因为它们具有虚拟成员)您可以避免完全销毁元素,并一举释放整个池。这消除了等式中的动态分配瓶颈。

有关的:


作为一个简单的措施(唾手可得的果实),可以考虑使用嵌入式快速堆库,例如libtcmalloc来自 google-perftools。

有关的:

于 2014-09-23T14:21:22.080 回答