blob: a6404ccc2bf60a7a6f08d9b976617c1161541273 [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Intrusive and non-intrusive containers</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../intrusive.html" title="Chapter&#160;10.&#160;Boost.Intrusive">
<link rel="prev" href="../intrusive.html" title="Chapter&#160;10.&#160;Boost.Intrusive">
<link rel="next" href="usage.html" title="How to use Boost.Intrusive">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../intrusive.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intrusive.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="usage.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="intrusive.intrusive_vs_nontrusive"></a><a class="link" href="intrusive_vs_nontrusive.html" title="Intrusive and non-intrusive containers">Intrusive and non-intrusive
containers</a>
</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="intrusive_vs_nontrusive.html#intrusive.intrusive_vs_nontrusive.differences_intrusive_vs_nontrusive">Differences
between intrusive and non-intrusive containers</a></span></dt>
<dt><span class="section"><a href="intrusive_vs_nontrusive.html#intrusive.intrusive_vs_nontrusive.properties_of_intrusive">Properties
of Boost.Intrusive containers</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="intrusive.intrusive_vs_nontrusive.differences_intrusive_vs_nontrusive"></a><a class="link" href="intrusive_vs_nontrusive.html#intrusive.intrusive_vs_nontrusive.differences_intrusive_vs_nontrusive" title="Differences between intrusive and non-intrusive containers">Differences
between intrusive and non-intrusive containers</a>
</h3></div></div></div>
<p>
The main difference between intrusive containers and non-intrusive containers
is that in C++ non-intrusive containers store <span class="bold"><strong>copies</strong></span>
of values passed by the user. Containers use the <code class="computeroutput"><span class="identifier">Allocator</span></code>
template parameter to allocate the stored values:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">list</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">assert</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">&gt;</span> <span class="identifier">myclass_list</span><span class="special">;</span>
<span class="identifier">MyClass</span> <span class="identifier">myclass</span><span class="special">(...);</span>
<span class="identifier">myclass_list</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">myclass</span><span class="special">);</span>
<span class="comment">//The stored object is different from the original object
</span> <span class="identifier">assert</span><span class="special">(&amp;</span><span class="identifier">myclass</span> <span class="special">!=</span> <span class="special">&amp;</span><span class="identifier">myclass_list</span><span class="special">.</span><span class="identifier">front</span><span class="special">());</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
To store the newly allocated copy of <code class="computeroutput"><span class="identifier">myclass</span></code>,
the container needs additional data: <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span></code>
usually allocates nodes that contain pointers to the next and previous node
and the value itself. Something similar to:
</p>
<pre class="programlisting"><span class="comment">//A possible implementation of a std::list&lt;MyClass&gt; node
</span><span class="keyword">class</span> <span class="identifier">list_node</span>
<span class="special">{</span>
<span class="identifier">list_node</span> <span class="special">*</span><span class="identifier">next</span><span class="special">;</span>
<span class="identifier">list_node</span> <span class="special">*</span><span class="identifier">previous</span><span class="special">;</span>
<span class="identifier">MyClass</span> <span class="identifier">value</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
On the other hand, an intrusive container does not store copies of passed
objects, but it stores the objects themselves. The additional data needed
to insert the object in the container must be provided by the object itself.
For example, to insert <code class="computeroutput"><span class="identifier">MyClass</span></code>
in an intrusive container that implements a linked list, <code class="computeroutput"><span class="identifier">MyClass</span></code>
must contain the needed <span class="emphasis"><em>next</em></span> and <span class="emphasis"><em>previous</em></span>
pointers:
</p>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">MyClass</span>
<span class="special">{</span>
<span class="identifier">MyClass</span> <span class="special">*</span><span class="identifier">next</span><span class="special">;</span>
<span class="identifier">MyClass</span> <span class="special">*</span><span class="identifier">previous</span><span class="special">;</span>
<span class="comment">//Other members...
</span><span class="special">};</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<span class="identifier">acme_intrusive_list</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">&gt;</span> <span class="identifier">list</span><span class="special">;</span>
<span class="identifier">MyClass</span> <span class="identifier">myclass</span><span class="special">;</span>
<span class="identifier">list</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">myclass</span><span class="special">);</span>
<span class="comment">//"myclass" object is stored in the list
</span> <span class="identifier">assert</span><span class="special">(&amp;</span><span class="identifier">myclass</span> <span class="special">==</span> <span class="special">&amp;</span><span class="identifier">list</span><span class="special">.</span><span class="identifier">front</span><span class="special">());</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
As we can see, knowing which additional data the class should contain is
not an easy task. <span class="bold"><strong>Boost.Intrusive</strong></span> offers
several intrusive containers and an easy way to make user classes compatible
with those containers.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="intrusive.intrusive_vs_nontrusive.properties_of_intrusive"></a><a class="link" href="intrusive_vs_nontrusive.html#intrusive.intrusive_vs_nontrusive.properties_of_intrusive" title="Properties of Boost.Intrusive containers">Properties
of Boost.Intrusive containers</a>
</h3></div></div></div>
<p>
Semantically, a <span class="bold"><strong>Boost.Intrusive</strong></span> container
is similar to a STL container holding pointers to objects. That is, if you
have an intrusive list holding objects of type <code class="computeroutput"><span class="identifier">T</span></code>,
then <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">*&gt;</span></code>
would allow you to do quite the same operations (maintaining and navigating
a set of objects of type T and types derived from it).
</p>
<p>
A non-intrusive container has some limitations:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
An object can only belong to one container: If you want to share an object
between two containers, you either have to store multiple copies of those
objects or you need to use containers of pointers: <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">Object</span><span class="special">*&gt;</span></code>.
</li>
<li class="listitem">
The use of dynamic allocation to create copies of passed values can be
a performance and size bottleneck in some applications. Normally, dynamic
allocation imposes a size overhead for each allocation to store bookkeeping
information and a synchronization to protected concurrent allocation
from different threads.
</li>
<li class="listitem">
Only copies of objects are stored in non-intrusive containers. Hence
copy or move constructors and copy or move assignment operators are required.
Non-copyable and non-movable objects can't be stored in non-intrusive
containers.
</li>
<li class="listitem">
It's not possible to store a derived object in a STL-container while
retaining its original type.
</li>
</ul></div>
<p>
Intrusive containers have some important advantages:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
Operating with intrusive containers doesn't invoke any memory management
at all. The time and size overhead associated with dynamic memory can
be minimized.
</li>
<li class="listitem">
Iterating an Intrusive container needs less memory accesses than the
semantically equivalent container of pointers: iteration is faster.
</li>
<li class="listitem">
Intrusive containers offer better exception guarantees than non-intrusive
containers. In some situations intrusive containers offer a no-throw
guarantee that can't be achieved with non-intrusive containers.
</li>
<li class="listitem">
The computation of an iterator to an element from a pointer or reference
to that element is a constant time operation (computing the position
of <code class="computeroutput"><span class="identifier">T</span><span class="special">*</span></code>
in a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">*&gt;</span></code>
has linear complexity).
</li>
<li class="listitem">
Intrusive containers offer predictability when inserting and erasing
objects since no memory management is done with intrusive containers.
Memory management usually is not a predictable operation so complexity
guarantees from non-intrusive containers are looser than the guarantees
offered by intrusive containers.
</li>
</ul></div>
<p>
Intrusive containers have also downsides:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
Each type stored in an intrusive container needs additional memory holding
the maintenance information needed by the container. Hence, whenever
a certain type will be stored in an intrusive container <span class="bold"><strong>you
have to change the definition of that type</strong></span> appropriately.
Although this task is easy with <span class="bold"><strong>Boost.Intrusive</strong></span>,
touching the definition of a type is sometimes a crucial issue.
</li>
<li class="listitem">
In intrusive containers you don't store a copy of an object, <span class="bold"><strong>but rather the original object is linked with other objects
in the container</strong></span>. Objects don't need copy-constructors or
assignment operators to be stored in intrusive containers. But you have
to take care of possible side effects, whenever you change the contents
of an object (this is especially important for associative containers).
</li>
<li class="listitem">
The user <span class="bold"><strong>has to manage the lifetime of inserted
objects</strong></span> independently from the containers.
</li>
<li class="listitem">
Again you have to be <span class="bold"><strong>careful</strong></span>: in contrast
to STL containers <span class="bold"><strong>it's easy to render an iterator
invalid</strong></span> without touching the intrusive container directly,
because the object can be disposed before is erased from the container.
</li>
<li class="listitem">
<span class="bold"><strong>Boost.Intrusive</strong></span> containers are <span class="bold"><strong>non-copyable and non-assignable</strong></span>. Since intrusive
containers don't have allocation capabilities, these operations make
no sense. However, swapping can be used to implement move capabilities.
To ease the implementation of copy constructors and assignment operators
of classes storing <span class="bold"><strong>Boost.Intrusive</strong></span> containers,
<span class="bold"><strong>Boost.Intrusive</strong></span> offers special cloning
functions. See <a class="link" href="clone_from.html" title="Cloning Boost.Intrusive containers">Cloning Boost.Intrusive
containers</a> section for more information.
</li>
<li class="listitem">
Analyzing the thread safety of a program that uses containers is harder
with intrusive containers, because the container might be modified indirectly
without an explicit call to a container member.
</li>
</ul></div>
<div class="table">
<a name="id1564362"></a><p class="title"><b>Table&#160;10.1.&#160;Summary of intrusive containers advantages and disadvantages</b></p>
<div class="table-contents"><table class="table" summary="Summary of intrusive containers advantages and disadvantages">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Issue
</p>
</th>
<th>
<p>
Intrusive
</p>
</th>
<th>
<p>
Non-intrusive
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
Memory management
</p>
</td>
<td>
<p>
External
</p>
</td>
<td>
<p>
Internal through allocator
</p>
</td>
</tr>
<tr>
<td>
<p>
Insertion/Erasure time
</p>
</td>
<td>
<p>
Faster
</p>
</td>
<td>
<p>
Slower
</p>
</td>
</tr>
<tr>
<td>
<p>
Memory locality
</p>
</td>
<td>
<p>
Better
</p>
</td>
<td>
<p>
Worse
</p>
</td>
</tr>
<tr>
<td>
<p>
Can hold non-copyable and non-movable objects by value
</p>
</td>
<td>
<p>
Yes
</p>
</td>
<td>
<p>
No
</p>
</td>
</tr>
<tr>
<td>
<p>
Exception guarantees
</p>
</td>
<td>
<p>
Better
</p>
</td>
<td>
<p>
Worse
</p>
</td>
</tr>
<tr>
<td>
<p>
Computation of iterator from value
</p>
</td>
<td>
<p>
Constant
</p>
</td>
<td>
<p>
Non-constant
</p>
</td>
</tr>
<tr>
<td>
<p>
Insertion/erasure predictability
</p>
</td>
<td>
<p>
High
</p>
</td>
<td>
<p>
Low
</p>
</td>
</tr>
<tr>
<td>
<p>
Memory use
</p>
</td>
<td>
<p>
Minimal
</p>
</td>
<td>
<p>
More than minimal
</p>
</td>
</tr>
<tr>
<td>
<p>
Insert objects by value retaining polymorphic behavior
</p>
</td>
<td>
<p>
Yes
</p>
</td>
<td>
<p>
No (slicing)
</p>
</td>
</tr>
<tr>
<td>
<p>
User must modify the definition of the values to insert
</p>
</td>
<td>
<p>
Yes
</p>
</td>
<td>
<p>
No
</p>
</td>
</tr>
<tr>
<td>
<p>
Containers are copyable
</p>
</td>
<td>
<p>
No
</p>
</td>
<td>
<p>
Yes
</p>
</td>
</tr>
<tr>
<td>
<p>
Inserted object's lifetime managed by
</p>
</td>
<td>
<p>
User (more complex)
</p>
</td>
<td>
<p>
Container (less complex)
</p>
</td>
</tr>
<tr>
<td>
<p>
Container invariants can be broken without using the container
</p>
</td>
<td>
<p>
Easier
</p>
</td>
<td>
<p>
Harder (only with containers of pointers)
</p>
</td>
</tr>
<tr>
<td>
<p>
Thread-safety analysis
</p>
</td>
<td>
<p>
Harder
</p>
</td>
<td>
<p>
Easier
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
For a performance comparison between Intrusive and Non-intrusive containers
see <a class="link" href="performance.html" title="Performance">Performance</a> section.
</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2005 Olaf Krzikalla, 2006-2010 Ion Gaztanaga<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../intrusive.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intrusive.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="usage.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>