summaryrefslogtreecommitdiff
path: root/doc/html/intrusive/intrusive_vs_nontrusive.html
blob: 6f84bb21b2e89556cc45908154599737081807b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<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.76.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../intrusive.html" title="Chapter&#160;13.&#160;Boost.Intrusive">
<link rel="prev" href="../intrusive.html" title="Chapter&#160;13.&#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="intrusive.intrusive_vs_nontrusive.properties_of_intrusive.t0"></a><p class="title"><b>Table&#160;13.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<br>Copyright &#169; 2006-2012 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>