Skip to content

Commit

Permalink
fix: limit number of threads used for image processing
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
sehnryr committed Nov 10, 2024
1 parent d58bda0 commit 61e65f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ crate-type = ["staticlib", "cdylib", "rlib"]
tauri-build = { version = "2.0.2", features = [] }

[dependencies]
fast_image_resize = { version = "5.0.0", features = ["image", "rayon"] }
fast_image_resize = { version = "5.0.0", features = ["image"] }
flate2 = "1.0.34"
image = "0.25.5"
log = "0.4.22"
midoku-bindings = { git = "https://github.com/midokuapp/midoku-rs.git" }
rayon = "1.10.0"
serde_json = "1.0.132"
serde = { version = "1.0.214", features = ["derive"] }
tar = "0.4.43"
Expand Down
45 changes: 33 additions & 12 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ async fn uninstall_extension(
}

#[tauri::command]
async fn download_image(url: String, min_size: Option<u32>) -> tauri::Result<Vec<u8>> {
async fn download_image(
pool: State<'_, rayon::ThreadPool>,
url: String,
min_size: Option<u32>,
) -> tauri::Result<Vec<u8>> {
trace!(
"download_image called with url: {} and min_size: {:?}",
url,
Expand Down Expand Up @@ -181,17 +185,23 @@ async fn download_image(url: String, min_size: Option<u32>) -> tauri::Result<Vec

let mut dst_image = Image::new(width, height, src_image.pixel_type().unwrap());

let mut resizer = Resizer::new();
let resize_options =
ResizeOptions::new().resize_alg(ResizeAlg::Convolution(FilterType::Hamming));
resizer
.resize(&src_image, &mut dst_image, Some(&resize_options))
.unwrap();

let mut result_buf = BufWriter::new(Vec::new());
PngEncoder::new(&mut result_buf)
.write_image(dst_image.buffer(), width, height, src_image.color().into())
.unwrap();
let (tx, rx) = tokio::sync::oneshot::channel();
pool.install(move || {
let mut resizer = Resizer::new();
let resize_options =
ResizeOptions::new().resize_alg(ResizeAlg::Convolution(FilterType::Hamming));
resizer
.resize(&src_image, &mut dst_image, Some(&resize_options))
.unwrap();

let mut result_buf = BufWriter::new(Vec::new());
PngEncoder::new(&mut result_buf)
.write_image(dst_image.buffer(), width, height, src_image.color().into())
.unwrap();

tx.send(result_buf).unwrap();
});
let result_buf = rx.await.unwrap();

Ok(result_buf.into_inner().unwrap())
}
Expand Down Expand Up @@ -251,6 +261,14 @@ async fn get_page_list(

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let num_threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(num_threads.min(4))
.build()
.unwrap();

let mut ctx = tauri::generate_context!();
let builder = tauri::Builder::default();

Expand Down Expand Up @@ -291,6 +309,9 @@ pub fn run() {
// Load the store.
let _store = app.store(STORE_FILE)?;

// Manage the thread pool
app.manage(pool);

Ok(())
});

Expand Down

0 comments on commit 61e65f4

Please sign in to comment.