From 75bee04b5a25350127ed1711ece720ed9aed0038 Mon Sep 17 00:00:00 2001 From: David Bliss Date: Thu, 4 Apr 2024 06:57:16 +0100 Subject: [PATCH] Parallel processing for preview generation --- Cargo.lock | 1 + Cargo.toml | 1 + core/src/preview.rs | 2 +- src/app/background/generate_previews.rs | 32 ++++++++++++------------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f86c62f..2e7a8f07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1345,6 +1345,7 @@ dependencies = [ "humansize", "itertools", "photos_core", + "rayon", "relm4", "tracing", "tracing-subscriber", diff --git a/Cargo.toml b/Cargo.toml index 54a30c3d..318129ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ tracing-subscriber = "0.3" relm4 = { version = "0.8.1", features = ["libadwaita", "gnome_45"] } itertools = "0.12.1" humansize = "2.1.3" +rayon = "1.10.0" [dependencies.photos_core] diff --git a/core/src/preview.rs b/core/src/preview.rs index dc624c47..2abe7fa1 100644 --- a/core/src/preview.rs +++ b/core/src/preview.rs @@ -10,7 +10,7 @@ use image::io::Reader as ImageReader; use image::DynamicImage; use std::path; -const EDGE: u32 = 400; +const EDGE: u32 = 200; #[derive(Debug, Clone)] pub struct Previewer { diff --git a/src/app/background/generate_previews.rs b/src/app/background/generate_previews.rs index 96b73fc3..182120fc 100644 --- a/src/app/background/generate_previews.rs +++ b/src/app/background/generate_previews.rs @@ -8,6 +8,7 @@ use std::sync::{Arc, Mutex}; use photos_core::Result; use photos_core::repo::PictureId; use std::path::PathBuf; +use rayon::prelude::*; #[derive(Debug)] pub enum GeneratePreviewsInput { @@ -38,23 +39,20 @@ impl GeneratePreviews { // Process newer photos first. pics.reverse(); - for mut pic in pics { - let result = self.previewer.set_preview(&mut pic); - if let Err(e) = result { - println!("Failed set_preview: {:?}", e); - continue; - } - - let result = self.repo.lock().unwrap().add_preview(&pic); - if let Err(e) = result { - println!("Failed add_preview: {:?}", e); - continue; - } - - if let Err(e) = sender.output(GeneratePreviewsOutput::PreviewUpdated(pic.picture_id, pic.square_preview_path)) { - println!("Failed sending PreviewUpdated: {:?}", e); - } - } + pics.clone().par_iter_mut() + .filter(|pic| !pic.square_preview_path.as_ref().is_some_and(|p| p.exists())) + .map(|mut pic| { + self.previewer.set_preview(&mut pic); + pic + }) + .for_each(|pic| { + let result = self.repo.lock().unwrap().add_preview(&pic); + if let Err(e) = result { + println!("Failed add_preview: {:?}", e); + } else if let Err(e) = sender.output(GeneratePreviewsOutput::PreviewUpdated(pic.picture_id, pic.square_preview_path.clone())) { + println!("Failed sending PreviewUpdated: {:?}", e); + } + }); println!("Generated {} previews in {} seconds.", pics_count, start.elapsed().as_secs());