forked from HowardHinnant/HowardHinnant.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonvectorbool.html
455 lines (383 loc) · 10.8 KB
/
onvectorbool.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>On vector<bool></title>
<style>
p {text-align:justify}
li {text-align:justify}
blockquote.note
{
background-color:#E0E0E0;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
ins {background-color:#FFFFA0}
del {background-color:#FFFFA0}
</style>
</head>
<body>
<address align=right>
<a href="mailto:howard.hinnant@gmail.com">Howard E. Hinnant</a><br>
2012-08-05
</address>
<hr>
<h1 align="center">On <code>vector<bool></code></h1>
<p>
<code>vector<bool></code> has taken a lot of heat over the past decade,
and not without reason. However I believe it is way past time to draw back
some of the criticism and explore this area with a dispassionate scrutiny of
detail.
</p>
<p>
There are really two issues here:
</p>
<ol>
<li>
Is the data structure of an array of bits a good data structure?
</li>
<li>
Should the aforementioned data structure be named
<code>vector<bool></code>?
</li>
</ol>
<p>
I have strong opinions on both of these questions. And to get this out of the
way up front:
</p>
<ol>
<li>
Yes.
</li>
<li>
No.
</li>
</ol>
<p>
The <i>array of bits</i> data structure is a <b>wonderful</b> data structure. It
is often both a space <b>and</b> speed optimization over the <i>array of
bools</i> data structure if properly implemented. However it does not behave
exactly as an array of bools, and so should not pretend to be one.
</p>
<h2>First, what's wrong with <code>vector<bool></code>?</h2>
<p>
Because <code>vector<bool></code> holds bits instead of bools, it can't
return a <code>bool&</code> from its indexing operator or iterator dereference.
This can play havoc on quite innocent looking generic code. For example:
</p>
<blockquote><pre>
template <class T>
void
process(T& t)
{
<font color="#C80000">// do something with t</font>
}
template <class T, class A>
void
test(std::vector<T, A>& v)
{
for (auto& t : v)
process(t);
}
</pre></blockquote>
<p>
The above code works for all <code>T</code> <i>except</i> <code>bool</code>.
When instantiated with <code>bool</code>, you will receive a compile time
error along the lines of:
</p>
<blockquote><pre>
error: non-const lvalue reference to type 'std::__bit_reference<std::vector<bool, std::allocator<bool>>, true>' cannot bind to
a temporary of type 'reference' (aka 'std::__bit_reference<std::vector<bool, std::allocator<bool>>, true>')
for (auto& t : v)
^ ~
note: in instantiation of function template specialization 'test<bool, std::allocator<bool>>' requested here
test(v);
^
vector:2124:14: note: selected 'begin' function
with iterator type 'iterator' (aka '__bit_iterator<std::vector<bool, std::allocator<bool>>, false>')
iterator begin()
^
1 error generated.
</pre></blockquote>
<p>
This is not a great error message. But it is about the best the compiler can
do. The user is confronted with implementation details of <code>vector</code>
and in a nutshell says that the <code>vector</code> is not working with a
perfectly valid <i>ranged-based for</i> statement. The conclusion the client
comes to here is that the implementation of <code>vector</code> is broken.
And he would be at least partially correct.
</p>
<p>
But consider if instead of <code>vector<bool></code> being a
specialization instead there existed a separate class template
<code>std::bit_vector<A = std::allocator<bool>></code> and the coder
had written:
</p>
<blockquote><pre>
template <class A>
void
test(bit_vector<A>& v)
{
for (auto& t : v)
process(t);
}
</pre></blockquote>
<p>
Now one gets a similar error message:
</p>
<blockquote><pre>
error: non-const lvalue reference to type 'std::__bit_reference<std::bit_vector<std::allocator<bool>>, true>' cannot bind to
a temporary of type 'reference' (aka 'std::__bit_reference<std::bit_vector<std::allocator<bool>>, true>')
for (auto& t : v)
^ ~
note: in instantiation of function template specialization 'test<std::allocator<bool>>' requested here
test(v);
^
bit_vector:2124:14: note: selected 'begin' function
with iterator type 'iterator' (aka '__bit_iterator<std::bit_vector<std::allocator<bool>>, false>')
iterator begin()
^
1 error generated.
</pre></blockquote>
<p>
And although the error message is similar, the coder is far more likely to see
that he is using a dynamic array of bits data structure and it is understandable
that you can't form a reference to a bit.
</p>
<p>
I.e. names <b>are</b> important. And creating a specialization that has
different behavior than the primary, when the primary template would have
worked, is poor practice.
</p>
<h2>But what's right with <code>vector<bool></code>?</h2>
For the rest of this article assume that we did indeed have a
<code>std::bit_vector<A = std::allocator<bool>></code> and that
<code>vector</code> was <i>not</i> specialized on <code>bool</code>.
<code>bit_vector<></code> can be much more than simply a space
optimization over <code>vector<bool></code>, it can also be a very
significant <i>performance</i> optimization. But to achieve this higher
performance, your vendor has to adapt many of the std::algorithms to have
specialized code (optimizations) when processing sequences defined by
<code>bit_vector<>::iterator</code>s.
<h3><code>find</code></h3>
<p>
For example consider this code:
</p>
<blockquote><pre>
template <class C>
typename C::iterator
test()
{
C c(100000);
c[95000] = true;
return std::find(c.begin(), c.end(), true);
}
</pre></blockquote>
<p>
How long does <code>std::find</code> take in the above example for:
</p>
<ol type="A">
<li>A hypothetical non-specialized <code>vector<bool></code>?</li>
<li>A hypothetical <code>bit_vector<></code> using an optimized
<code>find</code>?</li>
<li>A hypothetical <code>bit_vector<></code> using the unoptimized
generic <code>find</code>?</li>
</ol>
<p>
I'm testing on an Intel Core i5 in 64 bit mode. I am normalizing all answers
such that the speed of A is 1 (smaller is faster):
</p>
<ol type="A">
<li>1.0</li>
<li>0.013</li>
<li>1.6</li>
</ol>
<p>
An array of bits can be a <b>very</b> fast data structure for a sequential
search! The optimized <code>find</code> is inspecting 64 bits at a time. And
due to the space optimization, it is much less likely to cause a cache miss.
However if the implementation fails to do this, and naively checks one bit at a
time, then this giant 75X optimization turns into a significant pessimization.
</p>
<h3><code>count</code></h3>
<p>
<code>std::count</code> can be optimized much like <code>std::find</code> to
process a word of bits at a time:
</p>
<blockquote><pre>
template <class C>
typename C::difference_type
test()
{
C c(100000);
c[95000] = true;
return std::count(c.begin(), c.end(), true);
}
</pre></blockquote>
<p>
My results are:
</p>
<ol type="A">
<li>1.0</li>
<li>0.044</li>
<li>1.02</li>
</ol>
<p>
Here the results are not quite as dramatic as for the <code>std::find</code>
case. However any time you can speed up your code by a factor of 20, one
should do so!
</p>
<h3><code>fill</code></h3>
<p>
<code>std::fill</code> is yet another example:
</p>
<blockquote><pre>
template <class C>
void
test()
{
C c(100000);
std::fill(c.begin(), c.end(), true);
}
</pre></blockquote>
<p>
My results are:
</p>
<ol type="A">
<li>1.0</li>
<li>0.40</li>
<li>38.</li>
</ol>
<p>
The optimized fill is over twice as fast as the non-specialized
<code>vector<bool></code>. But if the vendor neglects to specialize
<code>fill</code> for bit-iterators the results are disastrous! Naturally
the results are identical for the closely related <code>fill_n</code>.
</p>
<h3><code>copy</code></h3>
<p>
<code>std::copy</code> is yet another example:
</p>
<blockquote><pre>
template <class C>
void
test()
{
C c1(100000);
C c2(100000);
std::copy(c1.begin(), c1.end(), c2.begin());
}
</pre></blockquote>
<p>
My results are:
</p>
<ol type="A">
<li>1.0</li>
<li>0.36</li>
<li>34.</li>
</ol>
<p>
The optimized copy is approaches three times as fast as the non-specialized
<code>vector<bool></code>. But if the vendor neglects to specialize
<code>fill</code> for bit-iterators the results are not good. If the copy is
not aligned on word boundaries (as in the above example), then the optimized
copy slows down to the same speed as the copy for A. Results for
<code>copy_backward</code>, <code>move</code> and <code>move_backward</code> are
similar.
</p>
<h3><code>swap_ranges</code></h3>
<p>
<code>std::swap_ranges</code> is yet another example:
</p>
<blockquote><pre>
template <class C>
void
test()
{
C c1(100000);
C c2(100000);
std::swap_ranges(c1.begin(), c1.end(), c2.begin());
}
</pre></blockquote>
<p>
My results are:
</p>
<ol type="A">
<li>1.0</li>
<li>0.065</li>
<li>4.0</li>
</ol>
<p>
Yet another example of really good results with an optimized algorithm and
really poor results without this extra attention.
</p>
<h3><code>rotate</code></h3>
<p>
<code>std::rotate</code> is yet another example:
</p>
<blockquote><pre>
template <class C>
void
test()
{
C c(100000);
std::rotate(c.begin(), c.begin()+c.size()/4, c.end());
}
</pre></blockquote>
<p>
My results are:
</p>
<ol type="A">
<li>1.0</li>
<li>0.59</li>
<li>17.9</li>
</ol>
<p>
Yet another example of good results with an optimized algorithm and
very poor results without this extra attention.
</p>
<h3><code>equal</code></h3>
<p>
<code>std::equal</code> is yet another example:
</p>
<blockquote><pre>
template <class C>
bool
test()
{
C c1(100000);
C c2(100000);
return std::equal(c1.begin(), c1.end(), c2.begin());
}
</pre></blockquote>
<p>
My results are:
</p>
<ol type="A">
<li>1.0</li>
<li>0.016</li>
<li>3.33</li>
</ol>
<p>
Yet another example of excellent results with an optimized algorithm and
very poor results without this extra attention.
</p>
<h2>Summary</h2>
<p>
The dynamic array of bits is a very good data structure if attention is paid
to optimizing algorithms that can process up to a word of bits at a time. In
this case it becomes not only a space optimization but a very significant speed
optimization. If such attention to detail is not given, then the space
optimization leads to a very significant speed pessimization.
</p>
<p>
But it is a shame that the C++ committee gave this excellent data structure
the name <code>vector<bool></code> and that it gives no guidance nor
encouragement on the critical generic algorithms that need to be optimized for
this data structure. Consequently, few std::lib implementations go to this
trouble.
</p>
</body>
</html>