From f8dcfa980c52d3083f4c3518a72c834a70d7bda8 Mon Sep 17 00:00:00 2001 From: Ruben Schmidmeister <4602612+bash@users.noreply.github.com> Date: Thu, 9 Feb 2023 23:09:22 +0100 Subject: [PATCH 1/3] Remove scoped_threadpool from HDR decoder --- Cargo.toml | 3 +-- Cargo.toml.public-private-dependencies | 3 +-- src/codecs/hdr/decoder.rs | 22 +++++++--------------- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0d9ff1f29a..5858e9d6ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,6 @@ num-traits = "0.2.0" gif = { version = "0.12", optional = true } jpeg = { package = "jpeg-decoder", version = "0.3.0", default-features = false, optional = true } png = { version = "0.17.6", optional = true } -scoped_threadpool = { version = "0.1", optional = true } tiff = { version = "0.8.0", optional = true } ravif = { version = "0.11.0", optional = true } rgb = { version = "0.8.25", optional = true } @@ -67,7 +66,7 @@ ico = ["bmp", "png"] pnm = [] tga = [] bmp = [] -hdr = ["scoped_threadpool"] +hdr = [] dxt = [] dds = ["dxt"] farbfeld = [] diff --git a/Cargo.toml.public-private-dependencies b/Cargo.toml.public-private-dependencies index bdce0c13d1..540949de6f 100644 --- a/Cargo.toml.public-private-dependencies +++ b/Cargo.toml.public-private-dependencies @@ -37,7 +37,6 @@ num-traits = { version = "0.2.0", public = true } gif = { version = "0.11.1", optional = true } jpeg = { package = "jpeg-decoder", version = "0.2.1", default-features = false, optional = true } png = { version = "0.17.0", optional = true } -scoped_threadpool = { version = "0.1", optional = true } tiff = { version = "0.7.1", optional = true } ravif = { version = "0.8.0", optional = true } rgb = { version = "0.8.25", optional = true } @@ -63,7 +62,7 @@ pnm = [] tga = [] webp = [] bmp = [] -hdr = ["scoped_threadpool"] +hdr = [] dxt = [] dds = ["dxt"] farbfeld = [] diff --git a/src/codecs/hdr/decoder.rs b/src/codecs/hdr/decoder.rs index ac37edce7a..8c992d2fc5 100644 --- a/src/codecs/hdr/decoder.rs +++ b/src/codecs/hdr/decoder.rs @@ -1,6 +1,5 @@ use crate::Primitive; use num_traits::identities::Zero; -use scoped_threadpool::Pool; #[cfg(test)] use std::borrow::Cow; use std::convert::TryFrom; @@ -460,21 +459,14 @@ impl HdrDecoder { } let chunks_iter = output_slice.chunks_mut(self.width as usize); - let mut pool = Pool::new(8); // - - (pool.scoped(|scope| { - for chunk in chunks_iter { - let mut buf = vec![Default::default(); self.width as usize]; - read_scanline(&mut self.r, &mut buf[..])?; - let f = &f; - scope.execute(move || { - for (dst, &pix) in chunk.iter_mut().zip(buf.iter()) { - *dst = f(pix); - } - }); + + for chunk in chunks_iter { + let mut buf = vec![Default::default(); self.width as usize]; + read_scanline(&mut self.r, &mut buf[..])?; + for (dst, &pix) in chunk.iter_mut().zip(buf.iter()) { + *dst = f(pix); } - Ok(()) - }) as Result<(), ImageError>)?; + } Ok(()) } From 60c16498e092847893d66e4f3da32602a06ed706 Mon Sep 17 00:00:00 2001 From: Ruben Schmidmeister <4602612+bash@users.noreply.github.com> Date: Thu, 9 Feb 2023 23:19:40 +0100 Subject: [PATCH 2/3] Re-use Vec instead of creating a new one in each loop --- src/codecs/hdr/decoder.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/codecs/hdr/decoder.rs b/src/codecs/hdr/decoder.rs index 8c992d2fc5..04641b752d 100644 --- a/src/codecs/hdr/decoder.rs +++ b/src/codecs/hdr/decoder.rs @@ -460,12 +460,14 @@ impl HdrDecoder { let chunks_iter = output_slice.chunks_mut(self.width as usize); + let mut buf = vec![Default::default(); self.width as usize]; for chunk in chunks_iter { - let mut buf = vec![Default::default(); self.width as usize]; read_scanline(&mut self.r, &mut buf[..])?; for (dst, &pix) in chunk.iter_mut().zip(buf.iter()) { *dst = f(pix); } + buf.clear(); + buf.resize(self.width as usize, Default::default()); } Ok(()) } From 7df5cb142318525ff1c544361656157c6bb70be6 Mon Sep 17 00:00:00 2001 From: Ruben Schmidmeister <4602612+bash@users.noreply.github.com> Date: Fri, 10 Feb 2023 14:25:48 +0100 Subject: [PATCH 3/3] Remove clearing of buffer `read_scanline` overwrites the entire buffer or returns an `Err`, so the resetting the buffer is not needed. --- src/codecs/hdr/decoder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/codecs/hdr/decoder.rs b/src/codecs/hdr/decoder.rs index 04641b752d..6915f0d15f 100644 --- a/src/codecs/hdr/decoder.rs +++ b/src/codecs/hdr/decoder.rs @@ -462,12 +462,12 @@ impl HdrDecoder { let mut buf = vec![Default::default(); self.width as usize]; for chunk in chunks_iter { + // read_scanline overwrites the entire buffer or returns an Err, + // so not resetting the buffer here is ok. read_scanline(&mut self.r, &mut buf[..])?; for (dst, &pix) in chunk.iter_mut().zip(buf.iter()) { *dst = f(pix); } - buf.clear(); - buf.resize(self.width as usize, Default::default()); } Ok(()) }