How good is boost::intrusive
library for high performance computing?
I want to use a container for a non-copyable non-assignable class. I was planning to use
normal STL with shared_ptr
. I found out that boost::intrusive
can also be used for the same purpose. So my question is, are they really that efficient?
if given an option between a STL container with shared_ptr
types and a boost::intrusive
which one would you prefer?
Generally, intrusive collections are the most efficient in terms of memory usage. If your goal is to squeeze every last cpu cycle that is the only way to go.
Consider a list of boost shared pointers. When creating a new object what happens is:
In the above, there are three memory allocations involved to create and insert a new object into a collection.
Now consider using an intrusive list. For the same task what happens is:
Here, only one memory allocation happens. Since the cpu addresses less memory when using intrusive collections, cpu caches get utilized better, resulting in fewer cache misses along with reduced memory footprint (known as locality of reference principle).
The drawback of intrusive collections is that it has to be known in advance in what type and how many collections an object can be an element of at one time. In my personal experience this extra upfront thinking leads to cleaner and simpler designs.