From a14e6219574c897bcf027956fce6a1b89a0a1532 Mon Sep 17 00:00:00 2001 From: Stephen D <webmaster@scd31.com> Date: Sun, 11 Feb 2024 08:23:22 -0400 Subject: [PATCH] fix slowdown bug with thousands of images --- src/img.rs | 9 ++++++--- src/main.rs | 1 - 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/img.rs b/src/img.rs index 6afd2fd..8cba8fc 100644 --- a/src/img.rs +++ b/src/img.rs @@ -1,5 +1,5 @@ use image::{imageops::FilterType, io::Reader as ImageReader, ImageOutputFormat}; -use std::{cmp::max, fs, io::Cursor, path::PathBuf, sync::mpsc::Receiver}; +use std::{cmp::max, collections::HashSet, fs, io::Cursor, path::PathBuf, sync::mpsc::Receiver}; const IMG_MAX_SIZE: u32 = 2048; const JPEG_QUALITY: u8 = 100; // let ssdv do the compression @@ -80,6 +80,7 @@ impl ImgInfo { pub struct ImgManager { paths: Vec<PathBuf>, imgs: Vec<ImgInfo>, + seen_imgs: HashSet<PathBuf>, // used to receive image requests, which will be sent at full resolution rx: Receiver<u8>, @@ -99,6 +100,7 @@ impl ImgManager { Self { paths, imgs: vec![], + seen_imgs: HashSet::new(), rx, iter: 0, @@ -230,19 +232,20 @@ impl ImgManager { // only files .filter(|(p, _)| p.is_file()) // only new files - .filter(|(p, _)| !self.imgs.iter().any(|i| i.path == p.as_path())) + .filter(|(p, _)| !self.seen_imgs.contains(p)) .collect(); for (path, metadata) in new_imgs { let size_bytes = metadata.len(); self.imgs.push(ImgInfo { - path, + path: path.clone(), path_idx: self.path_idx, size_bytes, txed: false, found_on_iter: self.iter, }); + self.seen_imgs.insert(path); } Ok(()) diff --git a/src/main.rs b/src/main.rs index a67e152..c6d42d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,6 @@ fn main() -> anyhow::Result<()> { let config = Config::load()?; let controller = Controller::new(config); - controller.run_forever(); Ok(()) -- GitLab