| 1 |
//! Usage example: |
| 2 |
//! |
| 3 |
//! ``` |
| 4 |
//! $ alias bench="rustup run nightly cargo bench" |
| 5 |
//! $ bench --bench=expand_paletted --features=benchmarks -- --save-baseline my_baseline |
| 6 |
//! ... tweak something ... |
| 7 |
//! $ bench --bench=expand_paletted --features=benchmarks -- --baseline my_baseline |
| 8 |
//! ``` |
| 9 |
|
| 10 |
use criterion::{criterion_group, criterion_main, Criterion, Throughput}; |
| 11 |
use png::benchable_apis::{create_info_from_plte_trns_bitdepth, create_transform_fn, TransformFn}; |
| 12 |
use png::{Info, Transformations}; |
| 13 |
use rand::Rng; |
| 14 |
use std::fmt::{self, Display}; |
| 15 |
|
| 16 |
#[derive(Clone, Copy)] |
| 17 |
enum TrnsPresence { |
| 18 |
Present, |
| 19 |
Absent, |
| 20 |
} |
| 21 |
|
| 22 |
impl Display for TrnsPresence { |
| 23 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 24 |
match self { |
| 25 |
TrnsPresence::Present => write!(f, "trns=yes"), |
| 26 |
TrnsPresence::Absent => write!(f, "trns=no"), |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
fn expand_paletted_all(c: &mut Criterion) { |
| 32 |
let trns_options = [TrnsPresence::Absent, TrnsPresence::Present]; |
| 33 |
let bit_depths = [4, 8]; |
| 34 |
|
| 35 |
let input_size = { |
| 36 |
let typical_l1_cache_size = 32 * 1024; |
| 37 |
let mut factor = 1; // input |
| 38 |
factor += 4; // RGBA output |
| 39 |
factor += 1; // other data |
| 40 |
typical_l1_cache_size / factor |
| 41 |
}; |
| 42 |
|
| 43 |
for trns in trns_options.iter().copied() { |
| 44 |
for bit_depth in bit_depths.iter().copied() { |
| 45 |
bench_expand_palette(c, trns, bit_depth, input_size); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
bench_create_fn(c, 256, 256); // Full PLTE and trNS |
| 50 |
bench_create_fn(c, 224, 32); // Partial PLTE and trNS |
| 51 |
bench_create_fn(c, 16, 1); // Guess: typical for small images? |
| 52 |
} |
| 53 |
|
| 54 |
criterion_group!(benches, expand_paletted_all); |
| 55 |
criterion_main!(benches); |
| 56 |
|
| 57 |
fn get_random_bytes<R: Rng>(rng: &mut R, n: usize) -> Vec<u8> { |
| 58 |
use rand::Fill; |
| 59 |
let mut result = vec![0u8; n]; |
| 60 |
result.as_mut_slice().try_fill(rng).unwrap(); |
| 61 |
result |
| 62 |
} |
| 63 |
|
| 64 |
struct Input { |
| 65 |
palette: Vec<u8>, |
| 66 |
trns: Option<Vec<u8>>, |
| 67 |
src: Vec<u8>, |
| 68 |
src_bit_depth: u8, |
| 69 |
} |
| 70 |
|
| 71 |
impl Input { |
| 72 |
fn new(trns: TrnsPresence, src_bit_depth: u8, input_size_in_bytes: usize) -> Self { |
| 73 |
let mut rng = rand::thread_rng(); |
| 74 |
|
| 75 |
// We provide RGB entries for 192 out of 256 possible indices and Alpha/Transparency |
| 76 |
// entries for 32 out of 256 possible indices. Rationale for these numbers: |
| 77 |
// * Oftentimes only a handful of colors at the edges of an icon need transparency |
| 78 |
// * In general, code needs to handle out-of-bounds indices, so it seems desirable |
| 79 |
// to explicitly test this. |
| 80 |
let palette = get_random_bytes(&mut rng, 192.min(input_size_in_bytes) * 3); |
| 81 |
let trns = match trns { |
| 82 |
TrnsPresence::Absent => None, |
| 83 |
TrnsPresence::Present => Some(get_random_bytes(&mut rng, 32.min(input_size_in_bytes))), |
| 84 |
}; |
| 85 |
let src = get_random_bytes(&mut rng, input_size_in_bytes); |
| 86 |
|
| 87 |
Self { |
| 88 |
palette, |
| 89 |
trns, |
| 90 |
src, |
| 91 |
src_bit_depth, |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
fn output_size_in_bytes(&self) -> usize { |
| 96 |
let output_bytes_per_input_sample = match self.trns { |
| 97 |
None => 3, |
| 98 |
Some(_) => 4, |
| 99 |
}; |
| 100 |
let samples_count_per_byte = (8 / self.src_bit_depth) as usize; |
| 101 |
let samples_count = self.src.len() * samples_count_per_byte; |
| 102 |
samples_count * output_bytes_per_input_sample |
| 103 |
} |
| 104 |
|
| 105 |
fn to_info(&self) -> Info { |
| 106 |
create_info_from_plte_trns_bitdepth(&self.palette, self.trns.as_deref(), self.src_bit_depth) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[inline(always)] |
| 111 |
fn create_expand_palette_fn(info: &Info) -> TransformFn { |
| 112 |
create_transform_fn(info, Transformations::EXPAND).unwrap() |
| 113 |
} |
| 114 |
|
| 115 |
fn bench_create_fn(c: &mut Criterion, plte_size: usize, trns_size: usize) { |
| 116 |
let mut group = c.benchmark_group("expand_paletted(ctor)"); |
| 117 |
group.sample_size(1000); |
| 118 |
|
| 119 |
let mut rng = rand::thread_rng(); |
| 120 |
let plte = get_random_bytes(&mut rng, 3 * plte_size as usize); |
| 121 |
let trns = get_random_bytes(&mut rng, trns_size as usize); |
| 122 |
let info = create_info_from_plte_trns_bitdepth(&plte, Some(&trns), 8); |
| 123 |
group.bench_with_input( |
| 124 |
format!("plte={plte_size}/trns={trns_size:?}"), |
| 125 |
&info, |
| 126 |
|b, info| { |
| 127 |
b.iter(|| create_expand_palette_fn(info)); |
| 128 |
}, |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
fn bench_expand_palette( |
| 133 |
c: &mut Criterion, |
| 134 |
trns: TrnsPresence, |
| 135 |
src_bit_depth: u8, |
| 136 |
input_size_in_bytes: usize, |
| 137 |
) { |
| 138 |
let mut group = c.benchmark_group("expand_paletted(exec)"); |
| 139 |
|
| 140 |
let input = Input::new(trns, src_bit_depth, input_size_in_bytes); |
| 141 |
let transform_fn = create_expand_palette_fn(&input.to_info()); |
| 142 |
group.throughput(Throughput::Bytes(input.output_size_in_bytes() as u64)); |
| 143 |
group.sample_size(500); |
| 144 |
group.bench_with_input( |
| 145 |
format!("{trns}/src_bits={src_bit_depth}/src_size={input_size_in_bytes}"), |
| 146 |
&input, |
| 147 |
|b, input| { |
| 148 |
let mut output = vec![0; input.output_size_in_bytes()]; |
| 149 |
let info = input.to_info(); |
| 150 |
b.iter(|| { |
| 151 |
transform_fn(input.src.as_slice(), output.as_mut_slice(), &info); |
| 152 |
}); |
| 153 |
}, |
| 154 |
); |
| 155 |
} |