Debian Codegraph @

third_party/rust/tinyvec/benches/smallvec.rs

raw
1 //! Benchmarks that compare TinyVec to SmallVec
2 //!
3 //! All the following commentary is based on the latest nightly at the time:
4 //! rustc 1.55.0 (c8dfcfe04 2021-09-06).
5 //!
6 //! Some of these benchmarks are just a few instructions, so we put our own for
7 //! loop inside the criterion::Bencher::iter call. This seems to improve the
8 //! stability of measurements, and it has the wonderful side effect of making
9 //! the emitted assembly easier to follow. Some of these benchmarks are totally
10 //! inlined so that there are no calls at all in the hot path, so finding
11 //! this for loop is an easy way to find your way around the emitted assembly.
12 //!
13 //! The clear method is cheaper to call for arrays of elements without a Drop
14 //! impl, so wherever possible we reuse a single object in the benchmark loop,
15 //! with a clear + black_box on each iteration in an attempt to not make that
16 //! visible to the optimizer.
17 //!
18 //! We always call black_box(&v), instead of v = black_box(v) because the latter
19 //! does a move of the inline array, which is linear in the size of the array
20 //! and thus varies based on the array type being benchmarked, and this move can
21 //! be more expensive than the function we're trying to benchmark.
22 //!
23 //! We also black_box the input to each method call. This has a significant
24 //! effect on the assembly emitted, for example if we do not black_box the range
25 //! we iterate over in the ::push benchmarks, the loop is unrolled. It's not
26 //! entirely clear if it's better to black_box the iterator that yields the
27 //! items being pushed, or to black_box at a deeper level: v.push(black_box(i))
28 //! for example. Anecdotally, it seems like the latter approach produces
29 //! unreasonably bad assembly.
30
31 use criterion::{black_box, criterion_group, criterion_main, Criterion};
32 use smallvec::SmallVec;
33 use std::iter::FromIterator;
34 use tinyvec::TinyVec;
35
36 const ITERS: usize = 10_000;
37
38 macro_rules! tinyvec_benches {
39 ($c:expr, $type:ty ; $len:expr) => {{
40 let mut g = $c.benchmark_group(concat!(
41 "TinyVec_",
42 stringify!($type),
43 "_",
44 stringify!($len)
45 ));
46
47 g.bench_function(
48 concat!(
49 "TinyVec<[",
50 stringify!($type),
51 "; ",
52 stringify!($len),
53 "]>::default"
54 ),
55 |b| {
56 b.iter(|| {
57 for _ in 0..ITERS {
58 let v: TinyVec<[$type; $len]> = TinyVec::default();
59 black_box(&v);
60 }
61 });
62 },
63 );
64
65 g.bench_function(
66 concat!(
67 "TinyVec<[",
68 stringify!($type),
69 "; ",
70 stringify!($len),
71 "]>::clone"
72 ),
73 |b| {
74 b.iter(|| {
75 let outer: TinyVec<[$type; $len]> =
76 black_box(TinyVec::from_iter(0..=($len as usize - 1) as _));
77 for _ in 0..ITERS {
78 let v = outer.clone();
79 black_box(&v);
80 }
81 });
82 },
83 );
84
85 g.bench_function(
86 concat!(
87 "TinyVec<[",
88 stringify!($type),
89 "; ",
90 stringify!($len),
91 "]>::clear"
92 ),
93 |b| {
94 b.iter(|| {
95 let mut v: TinyVec<[$type; $len]> = TinyVec::default();
96 for _ in 0..ITERS {
97 v.clear();
98 black_box(&v);
99 }
100 });
101 },
102 );
103
104 g.bench_function(
105 concat!(
106 "TinyVec<[",
107 stringify!($type),
108 "; ",
109 stringify!($len),
110 "]>::push"
111 ),
112 |b| {
113 b.iter(|| {
114 let mut v: TinyVec<[$type; $len]> = TinyVec::default();
115 for _ in 0..ITERS {
116 v.clear();
117 black_box(&v);
118 for i in black_box(0..=($len as usize - 1) as _) {
119 v.push(i);
120 }
121 black_box(&v);
122 }
123 });
124 },
125 );
126
127 g.bench_function(
128 concat!(
129 "TinyVec<[",
130 stringify!($type),
131 "; ",
132 stringify!($len),
133 "]>::from_iter"
134 ),
135 |b| {
136 b.iter(|| {
137 for _ in 0..ITERS {
138 let v: TinyVec<[$type; $len]> =
139 TinyVec::from_iter(black_box(0..=($len as usize - 1) as _));
140 black_box(&v);
141 }
142 });
143 },
144 );
145
146 g.bench_function(
147 concat!(
148 "TinyVec<[",
149 stringify!($type),
150 "; ",
151 stringify!($len),
152 "]>::from_slice"
153 ),
154 |b| {
155 b.iter(|| {
156 let data: &[$type] = &[0, 1, 2, 3, 4, 5, 6, 7];
157 for _ in 0..ITERS {
158 let v: TinyVec<[$type; $len]> = TinyVec::from(black_box(data));
159 black_box(&v);
160 }
161 });
162 },
163 );
164
165 g.bench_function(
166 concat!(
167 "TinyVec<[",
168 stringify!($type),
169 "; ",
170 stringify!($len),
171 "]>::extend"
172 ),
173 |b| {
174 b.iter(|| {
175 let mut v: TinyVec<[$type; $len]> = black_box(TinyVec::default());
176 for _ in 0..ITERS {
177 v.clear();
178 black_box(&v);
179 v.extend(black_box(0..=($len as usize - 1) as _));
180 black_box(&v);
181 }
182 });
183 },
184 );
185
186 g.bench_function(
187 concat!(
188 "TinyVec<[",
189 stringify!($type),
190 "; ",
191 stringify!($len),
192 "]>::extend_from_slice"
193 ),
194 |b| {
195 b.iter(|| {
196 let data: &[$type] = black_box(&[0, 1, 2, 3, 4, 5, 6, 7]);
197 let mut v: TinyVec<[$type; $len]> = black_box(TinyVec::default());
198 for _ in 0..ITERS {
199 v.clear();
200 black_box(&v);
201 v.extend_from_slice(data);
202 black_box(&v);
203 }
204 });
205 },
206 );
207
208 g.bench_function(
209 concat!(
210 "TinyVec<[",
211 stringify!($type),
212 "; ",
213 stringify!($len),
214 "]>::insert"
215 ),
216 |b| {
217 b.iter(|| {
218 let mut v: TinyVec<[$type; $len]> = TinyVec::default();
219 for _ in 0..ITERS {
220 v.clear();
221 black_box(&v);
222 for i in black_box(0..=($len as usize - 1) as _) {
223 v.insert(i as usize, i);
224 }
225 black_box(&v);
226 }
227 });
228 },
229 );
230
231 g.bench_function(
232 concat!(
233 "TinyVec<[",
234 stringify!($type),
235 "; ",
236 stringify!($len),
237 "]>::remove"
238 ),
239 |b| {
240 b.iter(|| {
241 let outer: TinyVec<[$type; $len]> =
242 black_box(TinyVec::from_iter(0..=($len as usize - 1) as _));
243 for _ in 0..ITERS {
244 let mut v = outer.clone();
245 for i in black_box((0..=($len as usize - 1) as _).rev()) {
246 v.remove(i);
247 }
248 black_box(&v);
249 }
250 });
251 },
252 );
253 }};
254 }
255
256 fn tinyvec_benches(c: &mut Criterion) {
257 tinyvec_benches!(c, u8; 8);
258 tinyvec_benches!(c, u8; 16);
259 tinyvec_benches!(c, u8; 32);
260 tinyvec_benches!(c, u8; 64);
261 tinyvec_benches!(c, u8; 128);
262 tinyvec_benches!(c, u8; 256);
263 tinyvec_benches!(c, u64; 2);
264 tinyvec_benches!(c, u64; 4);
265 tinyvec_benches!(c, u64; 8);
266 tinyvec_benches!(c, u64; 16);
267 tinyvec_benches!(c, u64; 32);
268 }
269
270 macro_rules! smallvec_benches {
271 ($c:expr, $type:ty ; $len:expr) => {{
272 let mut g = $c.benchmark_group(concat!(
273 "SmallVec_",
274 stringify!($type),
275 "_",
276 stringify!($len)
277 ));
278
279 g.bench_function(
280 concat!(
281 "SmallVec<[",
282 stringify!($type),
283 "; ",
284 stringify!($len),
285 "]>::default"
286 ),
287 |b| {
288 b.iter(|| {
289 for _ in 0..ITERS {
290 let v: SmallVec<[$type; $len]> = SmallVec::default();
291 black_box(&v);
292 }
293 });
294 },
295 );
296
297 g.bench_function(
298 concat!(
299 "SmallVec<[",
300 stringify!($type),
301 "; ",
302 stringify!($len),
303 "]>::clone"
304 ),
305 |b| {
306 b.iter(|| {
307 let outer: SmallVec<[$type; $len]> =
308 black_box(SmallVec::from_iter(0..=($len as usize - 1) as _));
309 for _ in 0..ITERS {
310 let v = outer.clone();
311 black_box(&v);
312 }
313 });
314 },
315 );
316
317 g.bench_function(
318 concat!(
319 "SmallVec<[",
320 stringify!($type),
321 "; ",
322 stringify!($len),
323 "]>::clear"
324 ),
325 |b| {
326 b.iter(|| {
327 let mut v: SmallVec<[$type; $len]> = SmallVec::default();
328 for _ in 0..ITERS {
329 v.clear();
330 black_box(&v);
331 }
332 });
333 },
334 );
335
336 g.bench_function(
337 concat!(
338 "SmallVec<[",
339 stringify!($type),
340 "; ",
341 stringify!($len),
342 "]>::push"
343 ),
344 |b| {
345 b.iter(|| {
346 let mut v: SmallVec<[$type; $len]> = SmallVec::default();
347 for _ in 0..ITERS {
348 v.clear();
349 black_box(&v);
350 for i in black_box(0..=($len as usize - 1) as _) {
351 v.push(i);
352 }
353 black_box(&v);
354 }
355 });
356 },
357 );
358
359 g.bench_function(
360 concat!(
361 "SmallVec<[",
362 stringify!($type),
363 "; ",
364 stringify!($len),
365 "]>::from_iter"
366 ),
367 |b| {
368 b.iter(|| {
369 for _ in 0..ITERS {
370 let v: SmallVec<[$type; $len]> =
371 SmallVec::from_iter(black_box(0..=($len as usize - 1) as _));
372 black_box(&v);
373 }
374 });
375 },
376 );
377
378 g.bench_function(
379 concat!(
380 "SmallVec<[",
381 stringify!($type),
382 "; ",
383 stringify!($len),
384 "]>::from_slice"
385 ),
386 |b| {
387 b.iter(|| {
388 let data: &[$type] = &[0, 1, 2, 3, 4, 5, 6, 7];
389 for _ in 0..ITERS {
390 let v: SmallVec<[$type; $len]> = SmallVec::from(black_box(data));
391 black_box(&v);
392 }
393 });
394 },
395 );
396
397 g.bench_function(
398 concat!(
399 "SmallVec<[",
400 stringify!($type),
401 "; ",
402 stringify!($len),
403 "]>::extend"
404 ),
405 |b| {
406 b.iter(|| {
407 let mut v: SmallVec<[$type; $len]> = black_box(SmallVec::default());
408 for _ in 0..ITERS {
409 v.clear();
410 black_box(&v);
411 v.extend(black_box(0..=($len as usize - 1) as _));
412 black_box(&v);
413 }
414 });
415 },
416 );
417
418 g.bench_function(
419 concat!(
420 "SmallVec<[",
421 stringify!($type),
422 "; ",
423 stringify!($len),
424 "]>::extend_from_slice"
425 ),
426 |b| {
427 b.iter(|| {
428 let data: &[$type] = black_box(&[0, 1, 2, 3, 4, 5, 6, 7]);
429 let mut v: SmallVec<[$type; $len]> = black_box(SmallVec::default());
430 for _ in 0..ITERS {
431 v.clear();
432 black_box(&v);
433 v.extend_from_slice(data);
434 black_box(&v);
435 }
436 });
437 },
438 );
439
440 g.bench_function(
441 concat!(
442 "SmallVec<[",
443 stringify!($type),
444 "; ",
445 stringify!($len),
446 "]>::insert"
447 ),
448 |b| {
449 b.iter(|| {
450 let mut v: SmallVec<[$type; $len]> = SmallVec::default();
451 for _ in 0..ITERS {
452 v.clear();
453 black_box(&v);
454 for i in black_box(0..=($len as usize - 1) as _) {
455 v.insert(i as usize, i);
456 }
457 black_box(&v);
458 }
459 });
460 },
461 );
462
463 g.bench_function(
464 concat!(
465 "SmallVec<[",
466 stringify!($type),
467 "; ",
468 stringify!($len),
469 "]>::remove"
470 ),
471 |b| {
472 b.iter(|| {
473 let outer: SmallVec<[$type; $len]> =
474 black_box(SmallVec::from_iter(0..=($len as usize - 1) as _));
475 for _ in 0..ITERS {
476 let mut v = outer.clone();
477 for i in black_box((0..=($len as usize - 1) as _).rev()) {
478 v.remove(i);
479 }
480 black_box(&v);
481 }
482 });
483 },
484 );
485 }};
486 }
487
488 fn smallvec_benches(c: &mut Criterion) {
489 smallvec_benches!(c, u8; 8);
490 smallvec_benches!(c, u8; 16);
491 smallvec_benches!(c, u8; 32);
492 smallvec_benches!(c, u8; 64);
493 smallvec_benches!(c, u8; 128);
494 smallvec_benches!(c, u8; 256);
495 smallvec_benches!(c, u64; 2);
496 smallvec_benches!(c, u64; 4);
497 smallvec_benches!(c, u64; 8);
498 smallvec_benches!(c, u64; 16);
499 smallvec_benches!(c, u64; 32);
500 }
501
502 criterion_group!(benches, tinyvec_benches, smallvec_benches);
503 criterion_main!(benches);