-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #413 from anforowicz/unfilter-benchmarks
Scaffolding for direct benchmarking of `crate::filter::unfilter`.
- Loading branch information
Showing
5 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! Usage example: | ||
//! | ||
//! ``` | ||
//! $ alias bench="rustup run nightly cargo bench" | ||
//! $ bench --bench=unfilter --features=benchmarks -- --save-baseline my_baseline | ||
//! ... tweak something, say the Sub filter ... | ||
//! $ bench --bench=unfilter --features=benchmarks -- filter=Sub --baseline my_baseline | ||
//! ``` | ||
use criterion::{criterion_group, criterion_main, Criterion, Throughput}; | ||
use png::benchable_apis::unfilter; | ||
use png::FilterType; | ||
use rand::Rng; | ||
|
||
fn unfilter_all(c: &mut Criterion) { | ||
let bpps = [1, 2, 3, 4, 6, 8]; | ||
let filters = [ | ||
FilterType::Sub, | ||
FilterType::Up, | ||
FilterType::Avg, | ||
FilterType::Paeth, | ||
]; | ||
for &filter in filters.iter() { | ||
for &bpp in bpps.iter() { | ||
bench_unfilter(c, filter, bpp); | ||
} | ||
} | ||
} | ||
|
||
criterion_group!(benches, unfilter_all); | ||
criterion_main!(benches); | ||
|
||
fn bench_unfilter(c: &mut Criterion, filter: FilterType, bpp: u8) { | ||
let mut group = c.benchmark_group("unfilter"); | ||
|
||
fn get_random_bytes<R: Rng>(rng: &mut R, n: usize) -> Vec<u8> { | ||
use rand::Fill; | ||
let mut result = vec![0u8; n]; | ||
result.as_mut_slice().try_fill(rng).unwrap(); | ||
result | ||
} | ||
let mut rng = rand::thread_rng(); | ||
let row_size = 4096 * (bpp as usize); | ||
let two_rows = get_random_bytes(&mut rng, row_size * 2); | ||
|
||
group.throughput(Throughput::Bytes(row_size as u64)); | ||
group.bench_with_input( | ||
format!("filter={filter:?}/bpp={bpp}"), | ||
&two_rows, | ||
|b, two_rows| { | ||
let (prev_row, curr_row) = two_rows.split_at(row_size); | ||
let mut curr_row = curr_row.to_vec(); | ||
b.iter(|| unfilter(filter, bpp, prev_row, curr_row.as_mut_slice())); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//! Development-time-only helper module for exporting private APIs so that they can be benchmarked. | ||
//! This module is gated behind the "benchmarks" feature. | ||
use crate::common::BytesPerPixel; | ||
use crate::filter::FilterType; | ||
|
||
/// Re-exporting `unfilter` to make it easier to benchmark, despite some items being only | ||
/// `pub(crate)`: `fn unfilter`, `enum BytesPerPixel`. | ||
pub fn unfilter(filter: FilterType, tbpp: u8, previous: &[u8], current: &mut [u8]) { | ||
let tbpp = BytesPerPixel::from_usize(tbpp as usize); | ||
crate::filter::unfilter(filter, tbpp, previous, current) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters