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
|
//! Benchmarks that compare TinyVec to SmallVec
//!
//! All the following commentary is based on the latest nightly at the time:
//! rustc 1.55.0 (c8dfcfe04 2021-09-06).
//!
//! Some of these benchmarks are just a few instructions, so we put our own for loop inside
//! the criterion::Bencher::iter call. This seems to improve the stability of measurements, and it
//! has the wonderful side effect of making the emitted assembly easier to follow. Some of these
//! benchmarks are totally inlined so that there are no calls at all in the hot path, so finding
//! this for loop is an easy way to find your way around the emitted assembly.
//!
//! The clear method is cheaper to call for arrays of elements without a Drop impl, so wherever
//! possible we reuse a single object in the benchmark loop, with a clear + black_box on each
//! iteration in an attempt to not make that visible to the optimizer.
//!
//! We always call black_box(&v), instead of v = black_box(v) because the latter does a move of the
//! inline array, which is linear in the size of the array and thus varies based on the array type
//! being benchmarked, and this move can be more expensive than the function we're trying to
//! benchmark.
//!
//! We also black_box the input to each method call. This has a significant effect on the assembly
//! emitted, for example if we do not black_box the range we iterate over in the ::push benchmarks,
//! the loop is unrolled. It's not entirely clear if it's better to black_box the iterator that
//! yields the items being pushed, or to black_box at a deeper level: v.push(black_box(i)) for
//! example. Anecdotally, it seems like the latter approach produces unreasonably bad assembly.
//!
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use smallvec::SmallVec;
use std::iter::FromIterator;
use tinyvec::TinyVec;
const ITERS: usize = 10_000;
macro_rules! tinyvec_benches {
($c:expr, $type:ty ; $len:expr) => {{
let mut g = $c.benchmark_group(concat!(
"TinyVec_",
stringify!($type),
"_",
stringify!($len)
));
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::default"
),
|b| {
b.iter(|| {
for _ in 0..ITERS {
let v: TinyVec<[$type; $len]> = TinyVec::default();
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::clone"
),
|b| {
b.iter(|| {
let outer: TinyVec<[$type; $len]> =
black_box(TinyVec::from_iter(0..=($len as usize - 1) as _));
for _ in 0..ITERS {
let v = outer.clone();
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::clear"
),
|b| {
b.iter(|| {
let mut v: TinyVec<[$type; $len]> = TinyVec::default();
for _ in 0..ITERS {
v.clear();
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::push"
),
|b| {
b.iter(|| {
let mut v: TinyVec<[$type; $len]> = TinyVec::default();
for _ in 0..ITERS {
v.clear();
black_box(&v);
for i in black_box(0..=($len as usize - 1) as _) {
v.push(i);
}
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::from_iter"
),
|b| {
b.iter(|| {
for _ in 0..ITERS {
let v: TinyVec<[$type; $len]> =
TinyVec::from_iter(black_box(0..=($len as usize - 1) as _));
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::from_slice"
),
|b| {
b.iter(|| {
let data: &[$type] = &[0, 1, 2, 3, 4, 5, 6, 7];
for _ in 0..ITERS {
let v: TinyVec<[$type; $len]> = TinyVec::from(black_box(data));
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::extend"
),
|b| {
b.iter(|| {
let mut v: TinyVec<[$type; $len]> = black_box(TinyVec::default());
for _ in 0..ITERS {
v.clear();
black_box(&v);
v.extend(black_box(0..=($len as usize - 1) as _));
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::extend_from_slice"
),
|b| {
b.iter(|| {
let data: &[$type] = black_box(&[0, 1, 2, 3, 4, 5, 6, 7]);
let mut v: TinyVec<[$type; $len]> = black_box(TinyVec::default());
for _ in 0..ITERS {
v.clear();
black_box(&v);
v.extend_from_slice(data);
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::insert"
),
|b| {
b.iter(|| {
let mut v: TinyVec<[$type; $len]> = TinyVec::default();
for _ in 0..ITERS {
v.clear();
black_box(&v);
for i in black_box(0..=($len as usize - 1) as _) {
v.insert(i as usize, i);
}
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"TinyVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::remove"
),
|b| {
b.iter(|| {
let outer: TinyVec<[$type; $len]> =
black_box(TinyVec::from_iter(0..=($len as usize - 1) as _));
for _ in 0..ITERS {
let mut v = outer.clone();
for i in black_box((0..=($len as usize - 1) as _).rev()) {
v.remove(i);
}
black_box(&v);
}
});
},
);
}};
}
fn tinyvec_benches(c: &mut Criterion) {
tinyvec_benches!(c, u8; 8);
tinyvec_benches!(c, u8; 16);
tinyvec_benches!(c, u8; 32);
tinyvec_benches!(c, u8; 64);
tinyvec_benches!(c, u8; 128);
tinyvec_benches!(c, u8; 256);
tinyvec_benches!(c, u64; 2);
tinyvec_benches!(c, u64; 4);
tinyvec_benches!(c, u64; 8);
tinyvec_benches!(c, u64; 16);
tinyvec_benches!(c, u64; 32);
}
macro_rules! smallvec_benches {
($c:expr, $type:ty ; $len:expr) => {{
let mut g = $c.benchmark_group(concat!(
"SmallVec_",
stringify!($type),
"_",
stringify!($len)
));
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::default"
),
|b| {
b.iter(|| {
for _ in 0..ITERS {
let v: SmallVec<[$type; $len]> = SmallVec::default();
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::clone"
),
|b| {
b.iter(|| {
let outer: SmallVec<[$type; $len]> =
black_box(SmallVec::from_iter(0..=($len as usize - 1) as _));
for _ in 0..ITERS {
let v = outer.clone();
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::clear"
),
|b| {
b.iter(|| {
let mut v: SmallVec<[$type; $len]> = SmallVec::default();
for _ in 0..ITERS {
v.clear();
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::push"
),
|b| {
b.iter(|| {
let mut v: SmallVec<[$type; $len]> = SmallVec::default();
for _ in 0..ITERS {
v.clear();
black_box(&v);
for i in black_box(0..=($len as usize - 1) as _) {
v.push(i);
}
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::from_iter"
),
|b| {
b.iter(|| {
for _ in 0..ITERS {
let v: SmallVec<[$type; $len]> =
SmallVec::from_iter(black_box(0..=($len as usize - 1) as _));
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::from_slice"
),
|b| {
b.iter(|| {
let data: &[$type] = &[0, 1, 2, 3, 4, 5, 6, 7];
for _ in 0..ITERS {
let v: SmallVec<[$type; $len]> = SmallVec::from(black_box(data));
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::extend"
),
|b| {
b.iter(|| {
let mut v: SmallVec<[$type; $len]> = black_box(SmallVec::default());
for _ in 0..ITERS {
v.clear();
black_box(&v);
v.extend(black_box(0..=($len as usize - 1) as _));
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::extend_from_slice"
),
|b| {
b.iter(|| {
let data: &[$type] = black_box(&[0, 1, 2, 3, 4, 5, 6, 7]);
let mut v: SmallVec<[$type; $len]> = black_box(SmallVec::default());
for _ in 0..ITERS {
v.clear();
black_box(&v);
v.extend_from_slice(data);
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::insert"
),
|b| {
b.iter(|| {
let mut v: SmallVec<[$type; $len]> = SmallVec::default();
for _ in 0..ITERS {
v.clear();
black_box(&v);
for i in black_box(0..=($len as usize - 1) as _) {
v.insert(i as usize, i);
}
black_box(&v);
}
});
},
);
g.bench_function(
concat!(
"SmallVec<[",
stringify!($type),
"; ",
stringify!($len),
"]>::remove"
),
|b| {
b.iter(|| {
let outer: SmallVec<[$type; $len]> =
black_box(SmallVec::from_iter(0..=($len as usize - 1) as _));
for _ in 0..ITERS {
let mut v = outer.clone();
for i in black_box((0..=($len as usize - 1) as _).rev()) {
v.remove(i);
}
black_box(&v);
}
});
},
);
}};
}
fn smallvec_benches(c: &mut Criterion) {
smallvec_benches!(c, u8; 8);
smallvec_benches!(c, u8; 16);
smallvec_benches!(c, u8; 32);
smallvec_benches!(c, u8; 64);
smallvec_benches!(c, u8; 128);
smallvec_benches!(c, u8; 256);
smallvec_benches!(c, u64; 2);
smallvec_benches!(c, u64; 4);
smallvec_benches!(c, u64; 8);
smallvec_benches!(c, u64; 16);
smallvec_benches!(c, u64; 32);
}
criterion_group!(benches, tinyvec_benches, smallvec_benches);
criterion_main!(benches);
|