| 1 |
/*! Benchmarks for `BitSlice::copy_from_slice`. |
| 2 |
|
| 3 |
The `copy_from_slice` implementation attempts to detect slice conditions that |
| 4 |
allow element-wise `memcpy` behavior, rather than the conservative bit-by-bit |
| 5 |
iteration, as element load/stores are faster than reading and writing each bit |
| 6 |
in an element individually. |
| 7 |
!*/ |
| 8 |
|
| 9 |
#![feature(maybe_uninit_uninit_array, maybe_uninit_slice)] |
| 10 |
|
| 11 |
use std::mem::MaybeUninit; |
| 12 |
|
| 13 |
use bitvec::{ |
| 14 |
mem::{ |
| 15 |
bits_of, |
| 16 |
elts, |
| 17 |
}, |
| 18 |
prelude::*, |
| 19 |
}; |
| 20 |
use criterion::{ |
| 21 |
criterion_group, |
| 22 |
criterion_main, |
| 23 |
BenchmarkId, |
| 24 |
Criterion, |
| 25 |
SamplingMode, |
| 26 |
Throughput, |
| 27 |
}; |
| 28 |
use tap::Tap; |
| 29 |
|
| 30 |
// One kibibyte |
| 31 |
const KIBIBYTE: usize = 1024; |
| 32 |
// Some number of kibibytes |
| 33 |
#[allow(clippy::identity_op)] |
| 34 |
const FACTOR: usize = 1 * KIBIBYTE; |
| 35 |
|
| 36 |
// Scalars applied to FACTOR to get a range of action |
| 37 |
const SCALARS: &[usize] = &[1, 2, 4, 8, 16, 24, 32, 40, 52, 64]; |
| 38 |
// The maximum number of bits in a memory region. |
| 39 |
const MAX_BITS: usize = 64 * FACTOR * 8; |
| 40 |
|
| 41 |
fn make_slots<T, const LEN: usize>() |
| 42 |
-> ([MaybeUninit<T>; LEN], [MaybeUninit<T>; LEN]) |
| 43 |
where T: BitStore { |
| 44 |
( |
| 45 |
MaybeUninit::<T>::uninit_array::<LEN>(), |
| 46 |
MaybeUninit::<T>::uninit_array::<LEN>(), |
| 47 |
) |
| 48 |
} |
| 49 |
|
| 50 |
fn view_slots<'a, 'b, T>( |
| 51 |
src: &'a [MaybeUninit<T>], |
| 52 |
dst: &'b mut [MaybeUninit<T>], |
| 53 |
) -> (&'a [T], &'b mut [T]) |
| 54 |
where |
| 55 |
T: BitStore, |
| 56 |
{ |
| 57 |
unsafe { |
| 58 |
( |
| 59 |
MaybeUninit::slice_assume_init_ref(src), |
| 60 |
MaybeUninit::slice_assume_init_mut(dst), |
| 61 |
) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
pub fn benchmarks(crit: &mut Criterion) { |
| 66 |
fn steps() |
| 67 |
-> impl Iterator<Item = (impl Fn(&'static str) -> BenchmarkId, usize, Throughput)> |
| 68 |
{ |
| 69 |
SCALARS.iter().map(|&n| { |
| 70 |
( |
| 71 |
move |name| BenchmarkId::new(name, n), |
| 72 |
n * FACTOR * bits_of::<u8>(), |
| 73 |
Throughput::Bytes((n * FACTOR) as u64), |
| 74 |
) |
| 75 |
}) |
| 76 |
} |
| 77 |
|
| 78 |
let (src_words, mut dst_words) = |
| 79 |
make_slots::<usize, { elts::<usize>(MAX_BITS) }>(); |
| 80 |
let (src_bytes, mut dst_bytes) = |
| 81 |
make_slots::<u8, { elts::<u8>(MAX_BITS) }>(); |
| 82 |
|
| 83 |
let (src_words, dst_words) = view_slots(&src_words, &mut dst_words); |
| 84 |
let (src_bytes, dst_bytes) = view_slots(&src_bytes, &mut dst_bytes); |
| 85 |
|
| 86 |
macro_rules! mkgrp { |
| 87 |
($crit:ident, $name:literal) => { |
| 88 |
(&mut *$crit).benchmark_group($name).tap_mut(|grp| { |
| 89 |
grp.sampling_mode(SamplingMode::Flat).sample_size(2000); |
| 90 |
}) |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
let mut group = mkgrp!(crit, "Element-wise"); |
| 95 |
for (id, bits, thrpt) in steps() { |
| 96 |
group.throughput(thrpt); |
| 97 |
let words = bits / bits_of::<usize>(); |
| 98 |
let bytes = bits / bits_of::<u8>(); |
| 99 |
|
| 100 |
let (src_words, dst_words) = |
| 101 |
(&src_words[.. words], &mut dst_words[.. words]); |
| 102 |
let (src_bytes, dst_bytes) = |
| 103 |
(&src_bytes[.. bytes], &mut dst_bytes[.. bytes]); |
| 104 |
|
| 105 |
// Use the builtin memcpy to run the slices in bulk. This ought to be a |
| 106 |
// lower bound on execution time. |
| 107 |
|
| 108 |
group.bench_function(id("words_plain"), |b| { |
| 109 |
let (src, dst) = (src_words, &mut *dst_words); |
| 110 |
b.iter(|| dst.copy_from_slice(src)) |
| 111 |
}); |
| 112 |
group.bench_function(id("bytes_plain"), |b| { |
| 113 |
let (src, dst) = (src_bytes, &mut *dst_bytes); |
| 114 |
b.iter(|| dst.copy_from_slice(src)) |
| 115 |
}); |
| 116 |
|
| 117 |
group.bench_function(id("words_manual"), |b| { |
| 118 |
let (src, dst) = (src_words, &mut *dst_words); |
| 119 |
b.iter(|| { |
| 120 |
for (from, to) in src.iter().zip(dst.iter_mut()) { |
| 121 |
*to = *from; |
| 122 |
} |
| 123 |
}) |
| 124 |
}); |
| 125 |
group.bench_function(id("bytes_manual"), |b| { |
| 126 |
let (src, dst) = (src_bytes, &mut *dst_bytes); |
| 127 |
b.iter(|| { |
| 128 |
for (from, to) in src.iter().zip(dst.iter_mut()) { |
| 129 |
*to = *from; |
| 130 |
} |
| 131 |
}) |
| 132 |
}); |
| 133 |
} |
| 134 |
group.finish(); |
| 135 |
|
| 136 |
let mut group = mkgrp!(crit, "Bit-wise accelerated"); |
| 137 |
for (id, bits, thrpt) in steps() { |
| 138 |
group.throughput(thrpt); |
| 139 |
let words = bits / bits_of::<usize>(); |
| 140 |
let bytes = bits / bits_of::<u8>(); |
| 141 |
|
| 142 |
let (src_words, dst_words) = |
| 143 |
(&src_words[.. words], &mut dst_words[.. words]); |
| 144 |
let (src_bytes, dst_bytes) = |
| 145 |
(&src_bytes[.. bytes], &mut dst_bytes[.. bytes]); |
| 146 |
|
| 147 |
// Ideal bitwise memcpy: no edges, same typarams, fully aligned. |
| 148 |
|
| 149 |
group.bench_function(id("bits_words_plain"), |b| { |
| 150 |
let (src, dst) = ( |
| 151 |
src_words.view_bits::<Lsb0>(), |
| 152 |
dst_words.view_bits_mut::<Lsb0>(), |
| 153 |
); |
| 154 |
b.iter(|| dst.copy_from_bitslice(src)); |
| 155 |
}); |
| 156 |
group.bench_function(id("bits_bytes_plain"), |b| { |
| 157 |
let (src, dst) = ( |
| 158 |
src_bytes.view_bits::<Lsb0>(), |
| 159 |
dst_bytes.view_bits_mut::<Lsb0>(), |
| 160 |
); |
| 161 |
b.iter(|| dst.copy_from_bitslice(src)); |
| 162 |
}); |
| 163 |
|
| 164 |
// Same typarams, fully aligned, with fuzzed edges. |
| 165 |
|
| 166 |
group.bench_function(id("bits_words_edges"), |b| { |
| 167 |
let src = src_words.view_bits::<Lsb0>(); |
| 168 |
let len = src.len(); |
| 169 |
let (src, dst) = ( |
| 170 |
&src[10 .. len - 10], |
| 171 |
&mut dst_words.view_bits_mut::<Lsb0>()[10 .. len - 10], |
| 172 |
); |
| 173 |
b.iter(|| dst.copy_from_bitslice(src)); |
| 174 |
}); |
| 175 |
group.bench_function(id("bits_bytes_edges"), |b| { |
| 176 |
let src = src_bytes.view_bits::<Lsb0>(); |
| 177 |
let len = src.len(); |
| 178 |
let (src, dst) = ( |
| 179 |
&src[10 .. len - 10], |
| 180 |
&mut dst_bytes.view_bits_mut::<Lsb0>()[10 .. len - 10], |
| 181 |
); |
| 182 |
b.iter(|| dst.copy_from_bitslice(src)); |
| 183 |
}); |
| 184 |
|
| 185 |
// Same typarams, misaligned. |
| 186 |
|
| 187 |
group.bench_function(id("bits_words_misalign"), |b| { |
| 188 |
let src = &src_words.view_bits::<Lsb0>()[10 ..]; |
| 189 |
let dst = &mut dst_words.view_bits_mut::<Lsb0>()[.. src.len()]; |
| 190 |
b.iter(|| dst.copy_from_bitslice(src)); |
| 191 |
}); |
| 192 |
group.bench_function(id("bits_bytes_misalign"), |b| { |
| 193 |
let src = &src_bytes.view_bits::<Lsb0>()[10 ..]; |
| 194 |
let dst = &mut dst_bytes.view_bits_mut::<Lsb0>()[.. src.len()]; |
| 195 |
b.iter(|| dst.copy_from_bitslice(src)); |
| 196 |
}); |
| 197 |
} |
| 198 |
group.finish(); |
| 199 |
|
| 200 |
let mut group = mkgrp!(crit, "Bit-wise crawl"); |
| 201 |
for (id, bits, thrpt) in steps() { |
| 202 |
group.throughput(thrpt); |
| 203 |
let words = bits / bits_of::<usize>(); |
| 204 |
let bytes = bits / bits_of::<u8>(); |
| 205 |
|
| 206 |
let (src_words, dst_words) = |
| 207 |
(&src_words[.. words], &mut dst_words[.. words]); |
| 208 |
let (src_bytes, dst_bytes) = |
| 209 |
(&src_bytes[.. bytes], &mut dst_bytes[.. bytes]); |
| 210 |
|
| 211 |
// Mismatched type parameters |
| 212 |
|
| 213 |
group.bench_function(id("bits_words_mismatched"), |b| { |
| 214 |
let (src, dst) = ( |
| 215 |
src_words.view_bits::<Msb0>(), |
| 216 |
dst_words.view_bits_mut::<Lsb0>(), |
| 217 |
); |
| 218 |
b.iter(|| dst.clone_from_bitslice(src)); |
| 219 |
}); |
| 220 |
group.bench_function(id("bits_bytes_mismatched"), |b| { |
| 221 |
let (src, dst) = ( |
| 222 |
src_bytes.view_bits::<Msb0>(), |
| 223 |
dst_bytes.view_bits_mut::<Lsb0>(), |
| 224 |
); |
| 225 |
b.iter(|| dst.clone_from_bitslice(src)); |
| 226 |
}); |
| 227 |
|
| 228 |
// Crawl each bit individually. This ought to be an upper bound on |
| 229 |
// execution time. |
| 230 |
|
| 231 |
group.bench_function(id("bitwise_words"), |b| { |
| 232 |
let (src, dst) = ( |
| 233 |
src_words.view_bits::<Lsb0>(), |
| 234 |
dst_words.view_bits_mut::<Lsb0>(), |
| 235 |
); |
| 236 |
b.iter(|| unsafe { |
| 237 |
for (from, to) in |
| 238 |
src.as_bitptr_range().zip(dst.as_mut_bitptr_range()) |
| 239 |
{ |
| 240 |
to.write(from.read()); |
| 241 |
} |
| 242 |
}) |
| 243 |
}); |
| 244 |
group.bench_function(id("bitwise_bytes"), |b| { |
| 245 |
let (src, dst) = ( |
| 246 |
src_bytes.view_bits::<Lsb0>(), |
| 247 |
dst_bytes.view_bits_mut::<Lsb0>(), |
| 248 |
); |
| 249 |
b.iter(|| unsafe { |
| 250 |
for (from, to) in |
| 251 |
src.as_bitptr_range().zip(dst.as_mut_bitptr_range()) |
| 252 |
{ |
| 253 |
to.write(from.read()); |
| 254 |
} |
| 255 |
}) |
| 256 |
}); |
| 257 |
|
| 258 |
group.bench_function(id("bitwise_words_mismatch"), |b| { |
| 259 |
let (src, dst) = ( |
| 260 |
src_words.view_bits::<Lsb0>(), |
| 261 |
dst_words.view_bits_mut::<Msb0>(), |
| 262 |
); |
| 263 |
b.iter(|| unsafe { |
| 264 |
for (from, to) in |
| 265 |
src.as_bitptr_range().zip(dst.as_mut_bitptr_range()) |
| 266 |
{ |
| 267 |
to.write(from.read()); |
| 268 |
} |
| 269 |
}) |
| 270 |
}); |
| 271 |
group.bench_function(id("bitwise_bytes_mismatch"), |b| { |
| 272 |
let (src, dst) = ( |
| 273 |
src_bytes.view_bits::<Msb0>(), |
| 274 |
dst_bytes.view_bits_mut::<Lsb0>(), |
| 275 |
); |
| 276 |
b.iter(|| unsafe { |
| 277 |
for (from, to) in |
| 278 |
src.as_bitptr_range().zip(dst.as_mut_bitptr_range()) |
| 279 |
{ |
| 280 |
to.write(from.read()); |
| 281 |
} |
| 282 |
}) |
| 283 |
}); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
criterion_group!(benches, benchmarks); |
| 288 |
criterion_main!(benches); |