From d8725b3d7c0d45ea95c7a80743dd06ff6ef35dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Thu, 27 Jul 2023 11:17:51 +0800 Subject: [PATCH] feat: avoid duplicating the same task of precaching the size (#11) --- src/core/tasks/precache.rs | 14 ++++++++++++-- src/core/tasks/scheduler.rs | 10 ++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/core/tasks/precache.rs b/src/core/tasks/precache.rs index f8641010..8f9ccd07 100644 --- a/src/core/tasks/precache.rs +++ b/src/core/tasks/precache.rs @@ -1,7 +1,8 @@ -use std::{collections::BTreeMap, path::{Path, PathBuf}, sync::Arc}; +use std::{collections::{BTreeMap, BTreeSet}, path::{Path, PathBuf}, sync::Arc}; use anyhow::{Error, Result}; use image::{imageops::FilterType, ImageFormat}; +use parking_lot::Mutex; use tokio::{fs, sync::mpsc}; use super::TaskOp; @@ -12,6 +13,8 @@ pub struct Precache { tx: async_channel::Sender, sch: mpsc::UnboundedSender, + + pub(super) size_handing: Mutex>, } #[derive(Debug)] @@ -48,7 +51,7 @@ pub(super) struct PrecacheOpVideo { impl Precache { pub(super) fn new(sch: mpsc::UnboundedSender) -> Self { let (tx, rx) = async_channel::unbounded(); - Self { tx, rx, sch } + Self { tx, rx, sch, size_handing: Default::default() } } #[inline] @@ -115,9 +118,16 @@ impl Precache { if let Ok(mut file) = File::from(&task.target).await { file.length = length; task.throttle.done((task.target, file), |buf| { + let mut handing = self.size_handing.lock(); + for (path, _) in &buf { + handing.remove(path); + } + let parent = buf[0].0.parent().unwrap().to_path_buf(); emit!(Files(FilesOp::Sort(parent, BTreeMap::from_iter(buf)))); }); + } else { + self.size_handing.lock().remove(&task.target); }; self.sch.send(TaskOp::Adv(task.id, 1, 0))?; diff --git a/src/core/tasks/scheduler.rs b/src/core/tasks/scheduler.rs index 14eac7b4..a07aea2d 100644 --- a/src/core/tasks/scheduler.rs +++ b/src/core/tasks/scheduler.rs @@ -383,11 +383,17 @@ impl Scheduler { pub(super) fn precache_size(&self, targets: Vec) { let throttle = Arc::new(Throttle::new(targets.len(), Duration::from_millis(300))); + let mut handing = self.precache.size_handing.lock(); + let mut running = self.running.write(); for target in targets { - let name = format!("Calculate the size of {:?}", target); - let id = self.running.write().add(name); + if !handing.contains(&target) { + handing.insert(target.clone()); + } else { + continue; + } + let id = running.add(format!("Calculate the size of {:?}", target)); let _ = self.todo.send_blocking({ let precache = self.precache.clone(); let throttle = throttle.clone();