Skip to content

Commit 9d1a0cf

Browse files
committed
Add support to benchmark fdct functions
1 parent 1a46325 commit 9d1a0cf

File tree

6 files changed

+64
-3
lines changed

6 files changed

+64
-3
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ default = ["std"]
1616
simd = ["std"]
1717
std = []
1818

19+
# DO NOT USE THIS IN PRODUCTION. Expose several internal functions for benchmark purposes.
20+
benchmark = []
21+
1922
[dependencies]
2023

2124
[dev-dependencies]

criterion/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ license = "MIT OR Apache-2.0"
99
[workspace]
1010
members = ["."]
1111

12+
[features]
13+
simd = ["jpeg-encoder/simd"]
14+
1215
[dependencies]
13-
jpeg-encoder = {path = ".."}
16+
jpeg-encoder = {path = "..", features = ["benchmark"]}
1417

1518
[dev-dependencies]
1619
criterion = { version = "0.5", default-features = false, features = ["plotters", "cargo_bench_support"]}
@@ -21,3 +24,7 @@ opt-level = 1
2124
[[bench]]
2225
name = "encode"
2326
harness = false
27+
28+
[[bench]]
29+
name = "fdct"
30+
harness = false

criterion/benches/fdct.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
3+
use jpeg_encoder::fdct;
4+
use std::time::Duration;
5+
6+
const INPUT1: [i16; 64] = [
7+
-70, -71, -70, -68, -67, -67, -67, -67, -72, -73, -72, -70, -69, -69, -68, -69, -75, -76,
8+
-74, -73, -73, -72, -71, -70, -77, -78, -77, -75, -76, -75, -73, -71, -78, -77, -77, -76,
9+
-79, -77, -76, -75, -78, -78, -77, -77, -77, -77, -78, -77, -79, -79, -78, -78, -78, -78,
10+
-79, -78, -80, -79, -78, -78, -81, -80, -78, -76,
11+
];
12+
13+
fn criterion_benchmark(c: &mut Criterion) {
14+
15+
let mut group = c.benchmark_group("fdct");
16+
group.measurement_time(Duration::from_secs(60));
17+
group.warm_up_time(Duration::from_secs(10));
18+
19+
group.bench_function("default fdct", |b| {
20+
b.iter(|| {
21+
let mut input = INPUT1.clone();
22+
fdct(
23+
black_box(&mut input),
24+
);
25+
black_box(&input);
26+
})
27+
});
28+
29+
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
30+
group.bench_function("fdct avx2", |b| {
31+
b.iter(|| {
32+
use jpeg_encoder::fdct_avx2;
33+
34+
let mut input = INPUT1.clone();
35+
fdct_avx2(
36+
black_box(&mut input),
37+
);
38+
black_box(&input);
39+
})
40+
});
41+
42+
group.finish();
43+
}
44+
45+
criterion_group!(benches, criterion_benchmark);
46+
criterion_main!(benches);

src/avx2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod fdct;
22
mod ycbcr;
33

44
use crate::encoder::Operations;
5-
pub(crate) use fdct::fdct_avx2;
5+
pub use fdct::fdct_avx2;
66
pub(crate) use ycbcr::*;
77

88
pub(crate) struct AVX2Operations;

src/fdct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn into_el(v: i32) -> i16 {
102102

103103
#[allow(clippy::erasing_op)]
104104
#[allow(clippy::identity_op)]
105-
pub(crate) fn fdct(data: &mut [i16; 64]) {
105+
pub fn fdct(data: &mut [i16; 64]) {
106106
/* Pass 1: process rows. */
107107
/* Note results are scaled up by sqrt(8) compared to a true DCT; */
108108
/* furthermore, we scale the results by 2**PASS1_BITS. */

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ pub use image_buffer::{cmyk_to_ycck, rgb_to_ycbcr, ImageBuffer};
4848
pub use quantization::QuantizationTableType;
4949
pub use writer::{Density, JfifWrite};
5050

51+
#[cfg(feature = "benchmark")]
52+
pub use fdct::fdct;
53+
#[cfg(all(feature = "benchmark", feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
54+
pub use avx2::fdct_avx2;
55+
5156
#[cfg(test)]
5257
mod tests {
5358
use crate::image_buffer::rgb_to_ycbcr;

0 commit comments

Comments
 (0)