fast image operations
segfaulting
bendn 2023-09-05
parent 0a5f7f1 · commit c5ac6f3
-rw-r--r--Cargo.toml7
-rw-r--r--benches/3_4x4.imgbuf (renamed from test_data/3_4x4.imgbuf)0
-rw-r--r--benches/3x3_at_out.imgbuf (renamed from test_results/3x3_at_out.buf)0
-rw-r--r--benches/4_4x4.imgbuf (renamed from test_data/4_4x4.imgbuf)0
-rw-r--r--benches/4x3_at_out.imgbuf (renamed from test_results/4x3_at_out.buf)0
-rw-r--r--benches/4x4_at_out.imgbuf (renamed from test_results/4x4_at_out.buf)0
-rw-r--r--benches/overlays.rs65
-rw-r--r--src/overlay.rs78
8 files changed, 72 insertions, 78 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1c85e52..ab92c06 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,13 @@ repository = "https://github.com/bend-n/fimg"
[dependencies]
png = { version = "0.17", features = ["unstable"], optional = true }
+[dev-dependencies]
+criterion = "0.5"
+
+[[bench]]
+name = "overlays"
+harness = false
+
[features]
save = ["png"]
default = ["save"]
diff --git a/test_data/3_4x4.imgbuf b/benches/3_4x4.imgbuf
index f3a8624..f3a8624 100644
--- a/test_data/3_4x4.imgbuf
+++ b/benches/3_4x4.imgbuf
diff --git a/test_results/3x3_at_out.buf b/benches/3x3_at_out.imgbuf
index 21d0141..21d0141 100644
--- a/test_results/3x3_at_out.buf
+++ b/benches/3x3_at_out.imgbuf
diff --git a/test_data/4_4x4.imgbuf b/benches/4_4x4.imgbuf
index bc6cdf8..bc6cdf8 100644
--- a/test_data/4_4x4.imgbuf
+++ b/benches/4_4x4.imgbuf
diff --git a/test_results/4x3_at_out.buf b/benches/4x3_at_out.imgbuf
index d5d8851..d5d8851 100644
--- a/test_results/4x3_at_out.buf
+++ b/benches/4x3_at_out.imgbuf
diff --git a/test_results/4x4_at_out.buf b/benches/4x4_at_out.imgbuf
index 4dedb62..4dedb62 100644
--- a/test_results/4x4_at_out.buf
+++ b/benches/4x4_at_out.imgbuf
diff --git a/benches/overlays.rs b/benches/overlays.rs
new file mode 100644
index 0000000..2236905
--- /dev/null
+++ b/benches/overlays.rs
@@ -0,0 +1,65 @@
+use criterion::{criterion_group, criterion_main, Criterion};
+use fimg::*;
+
+pub fn criterion_benchmark(bench: &mut Criterion) {
+ let mut group = bench.benchmark_group("overlays");
+ {
+ let mut a: Image<_, 3> = Image::alloc(64, 64);
+ let b = Image::<&[u8], 3>::new(
+ 4.try_into().unwrap(),
+ 4.try_into().unwrap(),
+ *&include_bytes!("3_4x4.imgbuf"),
+ );
+ group.bench_function("overlay 3x3 offset", |bench| {
+ bench.iter(|| unsafe {
+ for x in 0..16 {
+ for y in 0..16 {
+ a.as_mut().overlay_at(&b, x * 4, y * 4);
+ }
+ }
+ });
+ });
+ assert_eq!(a.as_ref().buffer, include_bytes!("3x3_at_out.imgbuf"));
+ }
+ {
+ let mut a: Image<_, 3> = Image::alloc(64, 64);
+ let b = Image::<&[u8], 4>::new(
+ 4.try_into().unwrap(),
+ 4.try_into().unwrap(),
+ *&include_bytes!("4_4x4.imgbuf"),
+ );
+ group.bench_function("overlay 4x3 offset", |bench| {
+ bench.iter(|| unsafe {
+ for x in 0..16 {
+ for y in 0..16 {
+ a.as_mut().overlay_at(&b, x * 4, y * 4);
+ }
+ }
+ });
+ });
+
+ assert_eq!(a.as_ref().buffer, include_bytes!("4x3_at_out.imgbuf"));
+ }
+ {
+ let mut a: Image<_, 4> = Image::alloc(64, 64);
+ let b = Image::<&[u8], 4>::new(
+ 4.try_into().unwrap(),
+ 4.try_into().unwrap(),
+ *&include_bytes!("4_4x4.imgbuf"),
+ );
+ group.bench_function("overlay 4x4 offset", |bench| {
+ bench.iter(|| unsafe {
+ for x in 0..16 {
+ for y in 0..16 {
+ a.as_mut().overlay_at(&b, x * 4, y * 4);
+ }
+ }
+ });
+ });
+ assert_eq!(a.as_ref().buffer, include_bytes!("4x4_at_out.imgbuf"));
+ }
+ group.finish();
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);
diff --git a/src/overlay.rs b/src/overlay.rs
index c1cac44..d1211a9 100644
--- a/src/overlay.rs
+++ b/src/overlay.rs
@@ -170,81 +170,3 @@ impl OverlayAt<Image<&[u8], 4>> for Image<&mut [u8], 4> {
self
}
}
-
-#[cfg(test)]
-mod bench {
- extern crate test;
-
- use test::Bencher;
-
- use super::*;
- use crate::{FromRef, FromRefMut};
-
- #[bench]
- fn overlay_3on3at(bench: &mut Bencher) {
- let mut v = vec![0u8; 3 * 64 * 64];
- let mut a: Image<_, 3> = Image::new(
- 64.try_into().unwrap(),
- 64.try_into().unwrap(),
- v.as_mut_slice(),
- );
- let b = Image::<&[u8], 3>::new(
- 4.try_into().unwrap(),
- 4.try_into().unwrap(),
- *&include_bytes!("../test_data/3_4x4.imgbuf"),
- );
- bench.iter(|| unsafe {
- for x in 0..16 {
- for y in 0..16 {
- a.overlay_at(&b, x * 4, y * 4);
- }
- }
- });
- assert_eq!(
- a.as_ref().buffer,
- include_bytes!("../test_results/3x3_at_out.buf")
- );
- }
-
- #[bench]
- fn overlay_4on3at(bench: &mut Bencher) {
- let mut a: Image<_, 3> = Image::alloc(64, 64);
- let b = Image::<&[u8], 4>::new(
- 4.try_into().unwrap(),
- 4.try_into().unwrap(),
- *&include_bytes!("../test_data/4_4x4.imgbuf"),
- );
- bench.iter(|| unsafe {
- for x in 0..16 {
- for y in 0..16 {
- a.as_mut().overlay_at(&b, x * 4, y * 4);
- }
- }
- });
- assert_eq!(
- a.as_ref().buffer,
- include_bytes!("../test_results/4x3_at_out.buf")
- );
- }
-
- #[bench]
- fn overlay_4on4at(bench: &mut Bencher) {
- let mut a: Image<_, 4> = Image::alloc(64, 64);
- let b = Image::<&[u8], 4>::new(
- 4.try_into().unwrap(),
- 4.try_into().unwrap(),
- *&include_bytes!("../test_data/4_4x4.imgbuf"),
- );
- bench.iter(|| unsafe {
- for x in 0..16 {
- for y in 0..16 {
- a.as_mut().overlay_at(&b, x * 4, y * 4);
- }
- }
- });
- assert_eq!(
- a.as_ref().buffer,
- include_bytes!("../test_results/4x4_at_out.buf")
- );
- }
-}