123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- //! 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);
|