Skip to content

Commit

Permalink
Parallel processing for preview generation
Browse files Browse the repository at this point in the history
  • Loading branch information
blissd committed Apr 4, 2024
1 parent a5316d3 commit 75bee04
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion core/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 15 additions & 17 deletions src/app/background/generate_previews.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 75bee04

Please sign in to comment.