From 3fbe651f94990cca75ddd1671b4bd8c859d99ed8 Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 20:32:59 +0100 Subject: [PATCH 1/7] perf(gui): budget the Photos texture cache by bytes The timeline kept up to 1500 decoded thumbnails regardless of their size, and marking one as used scanned the whole eviction order on every tile bind. Replace it with an LRU cache keyed on decoded pixel bytes (512 MiB, about 900 ordinary 512 px thumbnails) whose touch is logarithmic. --- crates/pdfs-gui/src/app/events.rs | 1 - crates/pdfs-gui/src/app/lru.rs | 190 ++++++++++++++++++++++++ crates/pdfs-gui/src/app/main.rs | 29 ++-- crates/pdfs-gui/src/app/pages/photos.rs | 46 ++++-- 4 files changed, 231 insertions(+), 35 deletions(-) create mode 100644 crates/pdfs-gui/src/app/lru.rs diff --git a/crates/pdfs-gui/src/app/events.rs b/crates/pdfs-gui/src/app/events.rs index 0fd1caa..98694d6 100644 --- a/crates/pdfs-gui/src/app/events.rs +++ b/crates/pdfs-gui/src/app/events.rs @@ -154,7 +154,6 @@ fn follow_changes(ui: &Rc, state: &FeedState, topics: &[Topic]) { ui.gallery.decode_queue.borrow_mut().clear(); ui.gallery.thumb_wanted.borrow_mut().clear(); ui.gallery.photo_tex.borrow_mut().clear(); - ui.gallery.photo_tex_order.borrow_mut().clear(); ui.gallery.model.remove_all(); ui.gallery.groups.remove_all(); ui.gallery.row_by_photo.borrow_mut().clear(); diff --git a/crates/pdfs-gui/src/app/lru.rs b/crates/pdfs-gui/src/app/lru.rs new file mode 100644 index 0000000..efb92a5 --- /dev/null +++ b/crates/pdfs-gui/src/app/lru.rs @@ -0,0 +1,190 @@ +//! A least-recently-used cache with a cost budget. +//! +//! Each entry carries a cost (bytes for a texture cache, `1` for a cache capped +//! by count). Inserting past the budget drops the entries used longest ago. +//! Lookups and touches are logarithmic, so a cache of a few thousand textures +//! can be touched on every tile bind without a linear scan. + +use std::borrow::Borrow; +use std::collections::{BTreeMap, HashMap}; +use std::hash::Hash; + +struct Entry { + value: V, + cost: usize, + stamp: u64, +} + +pub(crate) struct Lru { + entries: HashMap>, + /// Entries by the stamp of their last use, oldest first. + order: BTreeMap, + next: u64, + used: usize, + budget: usize, +} + +impl Lru { + pub(crate) fn new(budget: usize) -> Self { + Self { + entries: HashMap::new(), + order: BTreeMap::new(), + next: 0, + used: 0, + budget, + } + } + + /// Look an entry up without marking it used. + pub(crate) fn get(&self, key: &Q) -> Option<&V> + where + K: Borrow, + Q: Eq + Hash + ?Sized, + { + self.entries.get(key).map(|entry| &entry.value) + } + + pub(crate) fn contains_key(&self, key: &Q) -> bool + where + K: Borrow, + Q: Eq + Hash + ?Sized, + { + self.entries.contains_key(key) + } + + /// Mark an entry as just used, so it is the last to be evicted. Returns + /// whether it was there. + pub(crate) fn touch(&mut self, key: &Q) -> bool + where + K: Borrow, + Q: Eq + Hash + ?Sized, + { + let stamp = self.next; + let Some(entry) = self.entries.get_mut(key) else { + return false; + }; + if let Some(held) = self.order.remove(&entry.stamp) { + self.order.insert(stamp, held); + } + entry.stamp = stamp; + self.next += 1; + true + } + + /// Store `value` as the most recently used entry, then evict the least + /// recently used ones until the total cost fits the budget again. The new + /// entry itself is never evicted, even when it alone is over budget. + pub(crate) fn insert(&mut self, key: K, value: V, cost: usize) { + self.remove(&key); + let stamp = self.next; + self.next += 1; + self.order.insert(stamp, key.clone()); + self.entries.insert(key, Entry { value, cost, stamp }); + self.used += cost; + while self.used > self.budget && self.order.len() > 1 { + let Some((_, oldest)) = self.order.pop_first() else { + break; + }; + if let Some(entry) = self.entries.remove(&oldest) { + self.used -= entry.cost; + } + } + } + + pub(crate) fn remove(&mut self, key: &Q) -> Option + where + K: Borrow, + Q: Eq + Hash + ?Sized, + { + let entry = self.entries.remove(key)?; + self.order.remove(&entry.stamp); + self.used -= entry.cost; + Some(entry.value) + } + + pub(crate) fn clear(&mut self) { + self.entries.clear(); + self.order.clear(); + self.used = 0; + } + + #[cfg(test)] + pub(crate) fn len(&self) -> usize { + self.entries.len() + } + + #[cfg(test)] + pub(crate) fn used(&self) -> usize { + self.used + } +} + +#[cfg(test)] +mod tests { + use super::Lru; + + #[test] + fn evicts_least_recently_used_past_budget() { + let mut cache = Lru::new(3); + cache.insert("a", 1, 1); + cache.insert("b", 2, 1); + cache.insert("c", 3, 1); + assert!(cache.touch("a")); + cache.insert("d", 4, 1); + assert!(cache.contains_key("a")); + assert!(!cache.contains_key("b")); + assert!(cache.contains_key("c")); + assert!(cache.contains_key("d")); + assert_eq!(cache.len(), 3); + } + + #[test] + fn budget_counts_cost_not_entries() { + let mut cache = Lru::new(100); + cache.insert("small", (), 10); + cache.insert("medium", (), 40); + cache.insert("large", (), 60); + // 110 > 100: the oldest goes, and that is enough. + assert!(!cache.contains_key("small")); + assert_eq!(cache.used(), 100); + cache.insert("huge", (), 90); + assert_eq!(cache.used(), 90); + assert_eq!(cache.len(), 1); + } + + #[test] + fn keeps_a_single_entry_over_budget() { + let mut cache = Lru::new(10); + cache.insert("a", (), 5); + cache.insert("big", (), 50); + assert!(cache.contains_key("big")); + assert!(!cache.contains_key("a")); + assert_eq!(cache.used(), 50); + } + + #[test] + fn remove_and_clear_keep_cost_in_step() { + let mut cache = Lru::new(100); + cache.insert("a", (), 10); + cache.insert("b", (), 20); + assert_eq!(cache.remove("a"), Some(())); + assert_eq!(cache.used(), 20); + cache.clear(); + assert_eq!(cache.used(), 0); + assert_eq!(cache.len(), 0); + } + + #[test] + fn reinsert_replaces_cost_and_refreshes() { + let mut cache = Lru::new(30); + cache.insert("a", 1, 10); + cache.insert("b", 2, 10); + cache.insert("a", 3, 15); + assert_eq!(cache.used(), 25); + assert_eq!(cache.get("a"), Some(&3)); + cache.insert("c", 4, 10); + // "b" was used longest ago now. + assert!(!cache.contains_key("b")); + assert!(cache.contains_key("a")); + } +} diff --git a/crates/pdfs-gui/src/app/main.rs b/crates/pdfs-gui/src/app/main.rs index 1400359..cc3f6fe 100644 --- a/crates/pdfs-gui/src/app/main.rs +++ b/crates/pdfs-gui/src/app/main.rs @@ -6,6 +6,7 @@ pub(crate) mod dates; pub(crate) mod events; #[path = "../i18n.rs"] pub(crate) mod i18n; +pub(crate) mod lru; pub(crate) mod pages; pub(crate) mod reload; #[path = "../theme.rs"] @@ -15,6 +16,7 @@ pub(crate) mod widgets; use compat::*; use events::*; +use lru::Lru; use pages::activity::*; use pages::albums::*; use pages::browser::*; @@ -190,16 +192,11 @@ impl Ui { /// Scrolling back over a day then repaints from memory rather than decoding /// the same JPEGs off disk again. fn store_texture(&self, uid: &str, texture: gtk4::gdk::Texture) { - let mut cache = self.gallery.photo_tex.borrow_mut(); - let mut order = self.gallery.photo_tex_order.borrow_mut(); - if cache.insert(uid.to_string(), texture).is_none() { - order.push_back(uid.to_string()); - } - while order.len() > TEXTURE_CACHE_MAX { - if let Some(old) = order.pop_front() { - cache.remove(&old); - } - } + let bytes = texture_bytes(texture.width(), texture.height()); + self.gallery + .photo_tex + .borrow_mut() + .insert(uid.to_string(), texture, bytes); } /// Mark a cached thumbnail as freshly used, so eviction takes the tiles @@ -208,14 +205,7 @@ impl Ui { /// Insertion order alone evicts the top of the timeline first — exactly what /// a scroll back up then asks for again. fn touch_texture(&self, uid: &str) { - let mut order = self.gallery.photo_tex_order.borrow_mut(); - if order.back().is_some_and(|back| back == uid) { - return; - } - if let Some(at) = order.iter().position(|held| held == uid) { - order.remove(at); - order.push_back(uid.to_string()); - } + self.gallery.photo_tex.borrow_mut().touch(uid); } } @@ -631,8 +621,7 @@ fn build_window(app: &adw::Application) { library: Cell::new(true), page_waiters: RefCell::new(Vec::new()), width: Cell::new(0), - photo_tex: RefCell::new(HashMap::new()), - photo_tex_order: RefCell::new(VecDeque::new()), + photo_tex: RefCell::new(Lru::new(TEXTURE_CACHE_BYTES)), photo_nothumb: RefCell::new(HashSet::new()), thumb_wanted: RefCell::new(HashMap::new()), thumb_queue: RefCell::new(VecDeque::new()), diff --git a/crates/pdfs-gui/src/app/pages/photos.rs b/crates/pdfs-gui/src/app/pages/photos.rs index 830a203..e1bf000 100644 --- a/crates/pdfs-gui/src/app/pages/photos.rs +++ b/crates/pdfs-gui/src/app/pages/photos.rs @@ -160,11 +160,10 @@ pub(crate) struct GalleryState { /// Content width the grid is currently laid out to. Updated when the /// ListView is resized, which re-flows the visible sections. pub(crate) width: Cell, - /// Decoded thumbnails by photo uid, with the insertion order that evicts the - /// oldest past [`TEXTURE_CACHE_MAX`]. Scrolling back over a day therefore - /// repaints from memory instead of re-decoding from disk. - pub(crate) photo_tex: RefCell>, - pub(crate) photo_tex_order: RefCell>, + /// Decoded thumbnails by photo uid, the least recently used evicted once + /// their pixels pass [`TEXTURE_CACHE_BYTES`]. Scrolling back over a day + /// therefore repaints from memory instead of re-decoding from disk. + pub(crate) photo_tex: RefCell>, /// Photos the daemon reported as having no thumbnail at all, so a tile that /// can never be filled isn't requested again on every scroll past it. pub(crate) photo_nothumb: RefCell>, @@ -351,11 +350,20 @@ pub(crate) const THUMB_DEBOUNCE: Duration = Duration::from_millis(60); /// in seconds, not milliseconds. pub(crate) const THUMB_RETRY: Duration = Duration::from_secs(4); -/// Decoded thumbnails held in memory, evicted least-recently-*used* first. Each -/// is a few hundred KiB of GPU texture, and at the justified row density 600 was -/// under two screens' worth — a scroll down and back up then had to re-fetch and -/// re-decode everything it had just shown. -pub(crate) const TEXTURE_CACHE_MAX: usize = 1500; +/// Decoded thumbnail pixels held in memory, evicted least-recently-*used* +/// first. Budgeted by bytes rather than by count, because one oversized +/// thumbnail costs as much as dozens of ordinary ones. A 512 px thumbnail is +/// about 600 KiB decoded, so this holds roughly 900 of them: at the justified +/// row density 600 was under two screens' worth, and a scroll down and back up +/// then had to re-fetch and re-decode everything it had just shown. +pub(crate) const TEXTURE_CACHE_BYTES: usize = 512 * 1024 * 1024; + +/// What a decoded texture costs [`TEXTURE_CACHE_BYTES`]: four bytes a pixel. +pub(crate) fn texture_bytes(width: i32, height: i32) -> usize { + let width = usize::try_from(width).unwrap_or(0); + let height = usize::try_from(height).unwrap_or(0); + width.saturating_mul(height).saturating_mul(4).max(1) +} /// How many tiles beyond the realised rows [`prefetch_thumbs`] warms, so a tile /// is decoded before it is scrolled onto rather than after. @@ -4787,13 +4795,23 @@ pub(crate) fn gallery_unreachable(ui: &Rc) { #[cfg(test)] mod tests { use super::{ - Grouping, PHOTOS_PAGE, ROW_DEFAULT, ROW_MAX, ROW_MIN, ROW_STEP, TILE_GAP, TILE_RATIO_MAX, - TILE_RATIO_MIN, active_filters, copy_without_overwrite, empty_timeline_text, grouping_for, - memory_years, month_index, month_range, plan_grid, plan_justified, section_heading, - tile_ratio, viewport_unfilled, + Grouping, PHOTOS_PAGE, ROW_DEFAULT, ROW_MAX, ROW_MIN, ROW_STEP, TEXTURE_CACHE_BYTES, + TILE_GAP, TILE_RATIO_MAX, TILE_RATIO_MIN, active_filters, copy_without_overwrite, + empty_timeline_text, grouping_for, memory_years, month_index, month_range, plan_grid, + plan_justified, section_heading, texture_bytes, tile_ratio, viewport_unfilled, }; use pdfs_core::control::{PhotoItem, PhotoKind, PhotoMonth}; + #[test] + fn the_texture_cache_is_budgeted_by_decoded_pixels() { + assert_eq!(texture_bytes(512, 384), 512 * 384 * 4); + // A broken size still costs something, so it can't pile up for free. + assert_eq!(texture_bytes(0, 512), 1); + assert_eq!(texture_bytes(-1, -1), 1); + // Ordinary thumbnails: well over two dense screens' worth fit. + assert!(TEXTURE_CACHE_BYTES / texture_bytes(512, 384) >= 600); + } + #[test] fn the_scrubber_finds_the_month_a_photo_was_taken_in() { let months = [(2026, 3), (2025, 12), (2025, 11)].map(|(year, month)| PhotoMonth { From b854c1e819762fc054b9640465c1a50f35529014 Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 20:34:41 +0100 Subject: [PATCH 2/7] perf(gui): decode timeline thumbnails off the main thread The Photos timeline decoded up to four thumbnails per idle pass on the GTK thread, which stuttered a fast scroll. Decode each batch on gio's blocking pool instead and only paint the results on the main thread. Results from before a timeline reset are dropped, and tiles that were unbound or rebound meanwhile are no longer waiting, so they are not painted. --- crates/pdfs-gui/src/app/main.rs | 1 + crates/pdfs-gui/src/app/pages/photos.rs | 157 +++++++++++++++--------- 2 files changed, 102 insertions(+), 56 deletions(-) diff --git a/crates/pdfs-gui/src/app/main.rs b/crates/pdfs-gui/src/app/main.rs index cc3f6fe..d6e049f 100644 --- a/crates/pdfs-gui/src/app/main.rs +++ b/crates/pdfs-gui/src/app/main.rs @@ -629,6 +629,7 @@ fn build_window(app: &adw::Application) { thumb_generation: Cell::new(0), decode_queue: RefCell::new(VecDeque::new()), decode_idle: Cell::new(false), + decode_inflight: RefCell::default(), thumb_source: RefCell::new(None), relayout_source: RefCell::new(None), bound: RefCell::new(BTreeMap::new()), diff --git a/crates/pdfs-gui/src/app/pages/photos.rs b/crates/pdfs-gui/src/app/pages/photos.rs index e1bf000..4ebe0f2 100644 --- a/crates/pdfs-gui/src/app/pages/photos.rs +++ b/crates/pdfs-gui/src/app/pages/photos.rs @@ -185,12 +185,14 @@ pub(crate) struct GalleryState { pub(crate) thumb_inflight: RefCell>, pub(crate) thumb_generation: Cell, /// Thumbnails on disk waiting to be turned into textures, as `(uid, path)`. - /// Decoding happens on the GTK thread (textures are not `Send`), so it is fed - /// a few at a time from an idle callback rather than in one blocking burst - /// that would stutter the scroll. [`Self::decode_idle`] is the "callback - /// already scheduled" guard. + /// [`schedule_decode`] decodes them on worker threads a few at a time, so + /// the scroll never waits on a JPEG decoder. [`Self::decode_idle`] is the + /// "decoder already running" guard, and [`Self::decode_inflight`] the uids + /// being decoded right now, so a tile rebound meanwhile doesn't queue its + /// photo a second time. pub(crate) decode_queue: RefCell>, pub(crate) decode_idle: Cell, + pub(crate) decode_inflight: RefCell>, /// Pending debounce timers for the thumbnail queue flush and the section /// re-flow, replaced on each new trigger so only the last one fires. pub(crate) thumb_source: RefCell>, @@ -365,6 +367,9 @@ pub(crate) fn texture_bytes(width: i32, height: i32) -> usize { width.saturating_mul(height).saturating_mul(4).max(1) } +/// How many thumbnails [`schedule_decode`] decodes at once. +const DECODE_BATCH: usize = 4; + /// How many tiles beyond the realised rows [`prefetch_thumbs`] warms, so a tile /// is decoded before it is scrolled onto rather than after. pub(crate) const THUMB_PREFETCH: usize = 32; @@ -3268,6 +3273,7 @@ pub(crate) fn want_thumb(ui: &Rc, photo: &PhotoItem, picture: >k4::Picture let mut decode = ui.gallery.decode_queue.borrow_mut(); if decode.iter().any(|(uid, _)| uid == &photo.uid) + || ui.gallery.decode_inflight.borrow().contains(&photo.uid) || ui.gallery.thumb_inflight.borrow().contains(&photo.uid) { return; @@ -3459,9 +3465,16 @@ fn prefetch_thumbs(ui: &Rc, mut batch: Vec) -> Vec { batch } -/// Decode queued thumbnails into textures on an idle callback, a few per pass, so -/// a big batch fills in progressively instead of freezing the scroll for the -/// length of the whole decode. +/// Decode queued thumbnails into textures on worker threads, a few at a time, +/// and hand each finished texture to the tiles still waiting for it. +/// +/// Decoding a JPEG takes milliseconds; four of them per idle pass on the GTK +/// thread were enough to stutter a scroll. The main thread now only picks the +/// next batch (visible tiles first) and paints what comes back. A batch that +/// lands after the timeline was reset is dropped, and a tile that was rebound +/// or unbound meanwhile is no longer in [`GalleryState::thumb_wanted`], so it +/// is not painted. [`GalleryState::decode_idle`] is the "decoder running" +/// guard. pub(crate) fn schedule_decode(ui: &Rc) { if ui.gallery.decode_idle.get() || ui.gallery.decode_queue.borrow().is_empty() { return; @@ -3469,63 +3482,95 @@ pub(crate) fn schedule_decode(ui: &Rc) { ui.gallery.decode_idle.set(true); let ui = ui.clone(); - glib::idle_add_local(move || { - let batch: Vec<(String, String)> = { - let mut queue = ui.gallery.decode_queue.borrow_mut(); - let wanted = ui.gallery.thumb_wanted.borrow(); - let list = &ui.gallery.list; - let visible = |uid: &str| { - wanted.get(uid).is_some_and(|pictures| { - pictures.iter().any(|picture| { - if !picture.is_mapped() { - return false; - } - !picture.is_ancestor(list) - || picture.compute_bounds(list).is_some_and(|bounds| { - bounds.y() + bounds.height() > 0.0 - && bounds.y() < list.height() as f32 - }) - }) - }) - }; - queue.retain(|(uid, _)| wanted.contains_key(uid)); - (0..4) - .filter_map(|_| { - queue - .iter() - .position(|(uid, _)| visible(uid)) - .and_then(|index| queue.remove(index)) - .or_else(|| queue.pop_front()) - }) - .collect() - }; - for (uid, path) in batch { - if ui.gallery.photo_tex.borrow().contains_key(&uid) { - continue; + glib::spawn_future_local(async move { + loop { + let batch = next_decode_batch(&ui); + if batch.is_empty() { + break; } - let texture = match gtk4::gdk::Texture::from_filename(&path) { - Ok(texture) => texture, - Err(e) => { - tracing::debug!("cannot decode thumbnail {path}: {e}"); - ui.gallery.photo_nothumb.borrow_mut().insert(uid); + let generation = ui.gallery.thumb_generation.get(); + ui.gallery + .decode_inflight + .borrow_mut() + .extend(batch.iter().map(|(uid, _)| uid.clone())); + // All of the batch decodes at once, on the worker pool; the results + // are collected in the order they were picked. + let jobs: Vec<_> = batch + .into_iter() + .map(|(uid, path)| { + let job = gio::spawn_blocking({ + let path = path.clone(); + move || gtk4::gdk::Texture::from_filename(&path) + }); + (uid, path, job) + }) + .collect(); + for (uid, path, job) in jobs { + let decoded = job.await; + ui.gallery.decode_inflight.borrow_mut().remove(&uid); + if generation != ui.gallery.thumb_generation.get() { continue; } - }; - ui.store_texture(&uid, texture.clone()); - let waiting = ui.gallery.thumb_wanted.borrow_mut().remove(&uid); - for picture in waiting.into_iter().flatten() { - picture.set_paintable(Some(&texture)); + let texture = match decoded { + Ok(Ok(texture)) => texture, + Ok(Err(e)) => { + tracing::debug!("cannot decode thumbnail {path}: {e}"); + ui.gallery.photo_nothumb.borrow_mut().insert(uid); + continue; + } + Err(_) => { + tracing::debug!("thumbnail decoder for {path} panicked"); + ui.gallery.photo_nothumb.borrow_mut().insert(uid); + continue; + } + }; + if ui.gallery.photo_tex.borrow().contains_key(&uid) { + continue; + } + ui.store_texture(&uid, texture.clone()); + let waiting = ui.gallery.thumb_wanted.borrow_mut().remove(&uid); + for picture in waiting.into_iter().flatten() { + picture.set_paintable(Some(&texture)); + } } } - - if ui.gallery.decode_queue.borrow().is_empty() { - ui.gallery.decode_idle.set(false); - return glib::ControlFlow::Break; - } - glib::ControlFlow::Continue + ui.gallery.decode_idle.set(false); }); } +/// Take the next few thumbnails to decode off the queue: the ones on screen +/// first, then in queue order, skipping tiles nobody waits for any more and +/// photos already decoded. +fn next_decode_batch(ui: &Rc) -> Vec<(String, String)> { + let mut queue = ui.gallery.decode_queue.borrow_mut(); + let wanted = ui.gallery.thumb_wanted.borrow(); + let cached = ui.gallery.photo_tex.borrow(); + let list = &ui.gallery.list; + let visible = |uid: &str| { + wanted.get(uid).is_some_and(|pictures| { + pictures.iter().any(|picture| { + if !picture.is_mapped() { + return false; + } + !picture.is_ancestor(list) + || picture.compute_bounds(list).is_some_and(|bounds| { + bounds.y() + bounds.height() > 0.0 && bounds.y() < list.height() as f32 + }) + }) + }) + }; + queue.retain(|(uid, _)| wanted.contains_key(uid) && !cached.contains_key(uid)); + (0..DECODE_BATCH) + .filter_map(|_| { + queue + .iter() + .position(|(uid, _)| visible(uid)) + .and_then(|index| queue.remove(index)) + .or_else(|| queue.pop_front()) + }) + .collect() +} + /// Re-flow the sections on screen shortly. Debounced, because the triggers (a /// window resize, a zoom step) arrive in floods and only the final state /// matters. From 1b545ba3697ca9f312bd8b973b809f2d9a5a766e Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 20:36:12 +0100 Subject: [PATCH 3/7] perf(gui): look Drive gallery thumbnails up by key The Drive gallery's thumbnail cache was a queue searched linearly on every tile bind. Keep it in the shared LRU cache instead, still capped at 256 thumbnails, so a lookup and its touch no longer scan the queue. --- crates/pdfs-gui/src/app/lru.rs | 58 +++++++++++++++++++ .../pdfs-gui/src/app/pages/drive_galleries.rs | 42 +++++++------- 2 files changed, 78 insertions(+), 22 deletions(-) diff --git a/crates/pdfs-gui/src/app/lru.rs b/crates/pdfs-gui/src/app/lru.rs index efb92a5..1f8c7e8 100644 --- a/crates/pdfs-gui/src/app/lru.rs +++ b/crates/pdfs-gui/src/app/lru.rs @@ -71,6 +71,18 @@ impl Lru { true } + /// Look an entry up and mark it used. + pub(crate) fn fetch(&mut self, key: &Q) -> Option<&V> + where + K: Borrow, + Q: Eq + Hash + ?Sized, + { + if !self.touch(key) { + return None; + } + self.get(key) + } + /// Store `value` as the most recently used entry, then evict the least /// recently used ones until the total cost fits the budget again. The new /// entry itself is never evicted, even when it alone is over budget. @@ -102,6 +114,20 @@ impl Lru { Some(entry.value) } + /// Keep only the entries `keep` accepts. + pub(crate) fn retain(&mut self, mut keep: impl FnMut(&K, &V) -> bool) { + let order = &mut self.order; + let used = &mut self.used; + self.entries.retain(|key, entry| { + let kept = keep(key, &entry.value); + if !kept { + order.remove(&entry.stamp); + *used -= entry.cost; + } + kept + }); + } + pub(crate) fn clear(&mut self) { self.entries.clear(); self.order.clear(); @@ -187,4 +213,36 @@ mod tests { assert!(!cache.contains_key("b")); assert!(cache.contains_key("a")); } + + #[test] + fn fetch_touches_and_get_does_not() { + let mut cache = Lru::new(2); + cache.insert("a", 1, 1); + cache.insert("b", 2, 1); + assert_eq!(cache.get("a"), Some(&1)); + cache.insert("c", 3, 1); + assert!(!cache.contains_key("a")); + assert_eq!(cache.fetch("b"), Some(&2)); + cache.insert("d", 4, 1); + assert!(cache.contains_key("b")); + assert!(!cache.contains_key("c")); + assert_eq!(cache.fetch("missing"), None); + } + + #[test] + fn retain_drops_rejected_entries_and_their_cost() { + let mut cache = Lru::new(100); + cache.insert((true, "a".to_string()), (), 10); + cache.insert((false, "b".to_string()), (), 20); + cache.insert((true, "c".to_string()), (), 30); + cache.retain(|(private, _), _| !private); + assert_eq!(cache.used(), 20); + assert_eq!(cache.len(), 1); + assert!(cache.remove(&(false, "b".to_string())).is_some()); + assert_eq!(cache.used(), 0); + cache.insert((true, "d".to_string()), (), 10); + cache.clear(); + assert_eq!(cache.used(), 0); + assert_eq!(cache.len(), 0); + } } diff --git a/crates/pdfs-gui/src/app/pages/drive_galleries.rs b/crates/pdfs-gui/src/app/pages/drive_galleries.rs index 14ef016..d68f8d9 100644 --- a/crates/pdfs-gui/src/app/pages/drive_galleries.rs +++ b/crates/pdfs-gui/src/app/pages/drive_galleries.rs @@ -1,9 +1,9 @@ use crate::*; use base64::{Engine, engine::general_purpose::STANDARD}; use pdfs_core::control::{GalleryAction, GalleryAlbum, GalleryFolder, GalleryPlace}; -use std::collections::VecDeque; const PAGE: usize = 150; +/// Decoded thumbnails kept, the least recently shown dropped first. const THUMB_CACHE: usize = 256; type ThumbnailKey = (u64, bool, String); type ThumbnailTarget = (glib::WeakRef, glib::WeakRef); @@ -50,7 +50,8 @@ pub(crate) struct Browser { loading: Cell, unlocked: Cell, thumbnail_slots: tokio::sync::Semaphore, - thumbnails: RefCell>, + /// Decoded thumbnails by whether they are hidden-album ones and uid. + thumbnails: RefCell>, thumb_pending: RefCell>>, } @@ -86,7 +87,7 @@ impl Browser { } self.thumbnails .borrow_mut() - .retain(|(private, _, _)| !private); + .retain(|(private, _), _| !private); self.thumb_pending.borrow_mut().clear(); self.trail.borrow_mut().clear(); self.chooser.borrow_mut().take(); @@ -749,7 +750,7 @@ pub(crate) fn wire( loading: Cell::new(false), unlocked: Cell::new(false), thumbnail_slots: tokio::sync::Semaphore::new(8), - thumbnails: RefCell::default(), + thumbnails: RefCell::new(Lru::new(THUMB_CACHE)), thumb_pending: RefCell::default(), }); *ui.drive_galleries.borrow_mut() = Rc::downgrade(&browser); @@ -1099,20 +1100,17 @@ impl Browser { if private && !self.unlocked.get() { return; } - { - let mut cache = self.thumbnails.borrow_mut(); - if let Some(index) = cache - .iter() - .position(|(p, id, _)| *p == private && id == uid) - { - let cached = cache.remove(index).unwrap(); - picture.set_paintable(Some(&cached.2)); - if let Some(icon) = icon.upgrade() { - icon.set_visible(false); - } - cache.push_back(cached); - return; + let cached = self + .thumbnails + .borrow_mut() + .fetch(&(private, uid.to_owned())) + .cloned(); + if let Some(texture) = cached { + picture.set_paintable(Some(&texture)); + if let Some(icon) = icon.upgrade() { + icon.set_visible(false); } + return; } let key = (self.generation.get(), private, uid.to_owned()); { @@ -1170,11 +1168,11 @@ impl Browser { icon.set_visible(false); } } - let mut cache = browser.thumbnails.borrow_mut(); - if cache.len() >= THUMB_CACHE { - cache.pop_front(); - } - cache.push_back((private, key.2, texture)); + // Capped by count, like before: every entry costs one. + browser + .thumbnails + .borrow_mut() + .insert((private, key.2), texture, 1); } }); } From 960c6afe2d2c1fa87a5e65449ab59063fc9e70fc Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 20:37:22 +0100 Subject: [PATCH 4/7] perf(gui): decode Drive gallery thumbnails off the main thread Drive gallery tiles and the hidden-album preview decoded the base64 reply and the image itself on the GTK thread. Do both on gio's blocking pool, then re-check the request generation and lock state before painting, as before. --- .../pdfs-gui/src/app/pages/drive_galleries.rs | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/crates/pdfs-gui/src/app/pages/drive_galleries.rs b/crates/pdfs-gui/src/app/pages/drive_galleries.rs index d68f8d9..7b02e6b 100644 --- a/crates/pdfs-gui/src/app/pages/drive_galleries.rs +++ b/crates/pdfs-gui/src/app/pages/drive_galleries.rs @@ -603,6 +603,13 @@ impl Browser { let generation = self.generation.get(); glib::spawn_future_local(async move { let result = reply.recv().await; + // Decoded before the checks below, so they see the state after it. + let decoded = match &result { + Ok(Ok(Response::GalleryThumbnail { data: Some(data) })) if private => { + decode_thumbnail_off_thread(data.clone()).await + } + _ => None, + }; let close = |token| { let _ = spawn_request( browser.ui.dirs.control_socket(), @@ -647,8 +654,8 @@ impl Browser { picture.set_vexpand(true); content.append(&picture); } - Ok(Ok(Response::GalleryThumbnail { data: Some(data) })) if private => { - let Some(texture) = decode_thumbnail(&data) else { + Ok(Ok(Response::GalleryThumbnail { data: Some(_) })) if private => { + let Some(texture) = decoded else { browser .status .set_label(&gettext("Couldn't open the photo.")); @@ -1089,6 +1096,15 @@ fn decode_thumbnail(data: &str) -> Option { gtk4::gdk::Texture::from_bytes(&glib::Bytes::from_owned(bytes)).ok() } +/// [`decode_thumbnail`] on gio's blocking pool, so neither the base64 nor the +/// image decoder runs on the GTK thread. +async fn decode_thumbnail_off_thread(data: String) -> Option { + gio::spawn_blocking(move || decode_thumbnail(&data)) + .await + .ok() + .flatten() +} + impl Browser { fn want_thumbnail( self: &Rc, @@ -1149,6 +1165,12 @@ impl Browser { }, ); let response = request.recv().await; + let texture = match response { + Ok(Ok(Response::GalleryThumbnail { data: Some(data) })) => { + decode_thumbnail_off_thread(data).await + } + _ => None, + }; let targets = browser .thumb_pending .borrow_mut() @@ -1157,9 +1179,7 @@ impl Browser { if key.0 != browser.generation.get() || private && !browser.unlocked.get() { return; } - if let Ok(Ok(Response::GalleryThumbnail { data: Some(data) })) = response - && let Some(texture) = decode_thumbnail(&data) - { + if let Some(texture) = texture { for (picture, icon) in targets { if let Some(picture) = picture.upgrade() { picture.set_paintable(Some(&texture)); From 890adbc189efac2d0d2957c1758498e6f4482cc5 Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 20:41:59 +0100 Subject: [PATCH 5/7] perf(gui): debounce the Drive gallery re-flow on resize The Drive gallery rebuilt its whole row model on every frame the width changed, so dragging the window edge rebuilt it dozens of times a second. Lay out the first width at once as before, and wait for a resize to settle (80 ms, like the timeline) before rebuilding. --- .../pdfs-gui/src/app/pages/drive_galleries.rs | 63 ++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/crates/pdfs-gui/src/app/pages/drive_galleries.rs b/crates/pdfs-gui/src/app/pages/drive_galleries.rs index 7b02e6b..bb7c8e0 100644 --- a/crates/pdfs-gui/src/app/pages/drive_galleries.rs +++ b/crates/pdfs-gui/src/app/pages/drive_galleries.rs @@ -5,6 +5,28 @@ use pdfs_core::control::{GalleryAction, GalleryAlbum, GalleryFolder, GalleryPlac const PAGE: usize = 150; /// Decoded thumbnails kept, the least recently shown dropped first. const THUMB_CACHE: usize = 256; +/// What a new content width asks of the rows. +#[derive(Debug, PartialEq, Eq)] +enum Reflow { + /// Nothing changed, or there is no width yet. + None, + /// The first real width: lay the rows out now, nothing is on screen yet. + Now, + /// A resize: wait for it to settle, since the rows are rebuilt from + /// scratch and a drag resizes on every frame. + Debounced, +} + +fn reflow_for(previous: i32, width: i32) -> Reflow { + if width <= 0 || width == previous { + Reflow::None + } else if previous <= 0 { + Reflow::Now + } else { + Reflow::Debounced + } +} + type ThumbnailKey = (u64, bool, String); type ThumbnailTarget = (glib::WeakRef, glib::WeakRef); @@ -45,6 +67,8 @@ pub(crate) struct Browser { albums: RefCell>, items: RefCell>, width: Cell, + /// The pending re-flow after a resize, replaced by each new width. + reflow: RefCell>, generation: Cell, total: Cell, loading: Cell, @@ -752,6 +776,7 @@ pub(crate) fn wire( albums: RefCell::new(Vec::new()), items: RefCell::new(Vec::new()), width: Cell::new(0), + reflow: RefCell::new(None), generation: Cell::new(0), total: Cell::new(0), loading: Cell::new(false), @@ -1007,8 +1032,27 @@ pub(crate) fn wire( return glib::ControlFlow::Break; }; let width = scroll.width(); - if width > 0 && browser.width.replace(width) != width { - browser.rebuild(); + match reflow_for(browser.width.get(), width) { + Reflow::None => {} + Reflow::Now => { + browser.width.set(width); + browser.rebuild(); + } + Reflow::Debounced => { + browser.width.set(width); + if let Some(source) = browser.reflow.borrow_mut().take() { + source.remove(); + } + let weak = Rc::downgrade(&browser); + let source = + glib::timeout_add_local_once(super::photos::RELAYOUT_DEBOUNCE, move || { + if let Some(browser) = weak.upgrade() { + browser.reflow.borrow_mut().take(); + browser.rebuild(); + } + }); + *browser.reflow.borrow_mut() = Some(source); + } } glib::ControlFlow::Continue } @@ -1197,3 +1241,18 @@ impl Browser { }); } } + +#[cfg(test)] +mod tests { + use super::{Reflow, reflow_for}; + + #[test] + fn the_first_width_lays_out_at_once_and_a_resize_waits() { + assert_eq!(reflow_for(0, 0), Reflow::None); + assert_eq!(reflow_for(0, 800), Reflow::Now); + assert_eq!(reflow_for(800, 800), Reflow::None); + assert_eq!(reflow_for(800, 760), Reflow::Debounced); + // A collapsed widget reports no width; keep the last layout. + assert_eq!(reflow_for(800, 0), Reflow::None); + } +} From 10bc9a6a37228f3b00d32f9ba15e5941c2cf7a77 Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 20:43:44 +0100 Subject: [PATCH 6/7] perf(gui): cap the Places map's photo marker covers Every photo cover the map was shown while zooming around stayed in memory for as long as the map existed. Keep at most 1024 and drop the ones drawn longest ago, never a town's cover or one drawn this frame, and ask for a dropped cover again when its marker returns. --- crates/pdfs-gui/src/app/pages/places_map.rs | 83 ++++++++++++++++++++- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/crates/pdfs-gui/src/app/pages/places_map.rs b/crates/pdfs-gui/src/app/pages/places_map.rs index c86c955..1050cb0 100644 --- a/crates/pdfs-gui/src/app/pages/places_map.rs +++ b/crates/pdfs-gui/src/app/pages/places_map.rs @@ -67,6 +67,11 @@ const COVER_PX: i32 = 128; /// How many street-map tiles stay in memory. const TILE_CACHE: usize = 160; +/// Photo marker covers kept, 64 KiB each. A town's own cover is never +/// dropped (it only arrives with the town's card), but the photos zoomed past +/// one by one would otherwise pile up for as long as the map is open. +const COVER_CACHE: usize = 1024; + /// How long a tile that couldn't be had is left alone before it is tried /// again. const TILE_RETRY: Duration = Duration::from_secs(60); @@ -232,8 +237,11 @@ struct MapState { /// for from close up. locations: HashMap>, asked_locations: HashSet, - /// Marker photos, scaled and cropped to a square, by photo uid. - covers: HashMap, + /// Marker photos, scaled and cropped to a square, by photo uid, with the + /// draw they were last shown in. + covers: HashMap, + /// Counts draws, for dropping the covers shown longest ago. + draws: u64, asked_thumbs: HashSet, view: View, /// False until the view has been fitted to the places at a real size, and @@ -349,6 +357,7 @@ impl PlacesMap { locations: HashMap::new(), asked_locations: HashSet::new(), covers: HashMap::new(), + draws: 0, asked_thumbs: HashSet::new(), view: View { center: World { x: 0.5, y: 0.5 }, @@ -486,7 +495,18 @@ impl PlacesMap { let Some(cover) = square_cover(texture) else { return; }; - self.state.borrow_mut().covers.insert(uid.to_owned(), cover); + let mut state = self.state.borrow_mut(); + let state = &mut *state; + state.covers.insert(uid.to_owned(), (cover, state.draws)); + let towns: HashSet<&str> = state + .places + .iter() + .map(|(place, _)| place.cover.uid.as_str()) + .collect(); + for dropped in evict_covers(&mut state.covers, &towns, state.draws) { + // Asked for again once it is back on screen. + state.asked_thumbs.remove(&dropped); + } self.area.queue_draw(); } @@ -840,6 +860,7 @@ impl PlacesMap { let mut state = self.state.borrow_mut(); let state = &mut *state; let view = state.view; + state.draws += 1; paint_outline(cr, view, size, colors); let labels = match state.online.as_mut() { Some(online) => paint_tiles(cr, online, view, size, dark, area.scale_factor()), @@ -863,7 +884,10 @@ impl PlacesMap { } for bubble in &state.bubbles { let marker = &state.markers[bubble.members[0]]; - let cover = state.covers.get(&marker.cover); + let cover = state.covers.get_mut(&marker.cover).map(|(cover, drawn)| { + *drawn = state.draws; + &*cover + }); if cover.is_none() && let Target::Photo { place, index } = marker.target && state.asked_thumbs.insert(marker.cover.clone()) @@ -1121,6 +1145,35 @@ fn evict(cache: &mut HashMap, frame: u64) { } } +/// Drop the photo covers drawn longest ago, down to [`COVER_CACHE`]. Never +/// one drawn in the current draw, and never a town's cover. Returns the uids +/// dropped. +fn evict_covers( + covers: &mut HashMap, + towns: &HashSet<&str>, + draw: u64, +) -> Vec { + if covers.len() <= COVER_CACHE { + return Vec::new(); + } + let mut ages: Vec<(u64, &String)> = covers + .iter() + .filter(|(uid, (_, drawn))| *drawn < draw && !towns.contains(uid.as_str())) + .map(|(uid, (_, drawn))| (*drawn, uid)) + .collect(); + ages.sort_unstable_by_key(|(drawn, _)| *drawn); + let excess = covers.len() - COVER_CACHE; + let dropped: Vec = ages + .into_iter() + .take(excess) + .map(|(_, uid)| uid.clone()) + .collect(); + for uid in &dropped { + covers.remove(uid); + } + dropped +} + /// Set the names of the tiles on screen, the most important first, leaving /// out any that would overlap one already set. fn paint_labels( @@ -1654,6 +1707,28 @@ fn set_color(cr: &cairo::Context, color: >k4::gdk::RGBA, alpha: f32) { mod tests { use super::*; + #[test] + fn covers_past_the_cap_drop_the_stalest_photos_but_no_town() { + let mut covers: HashMap = (0..COVER_CACHE as u64 + 3) + .map(|n| (format!("p{n}"), ((), n))) + .collect(); + let towns: HashSet<&str> = ["p0"].into_iter().collect(); + let mut dropped = evict_covers(&mut covers, &towns, COVER_CACHE as u64); + dropped.sort(); + assert_eq!(dropped, ["p1", "p2", "p3"]); + assert_eq!(covers.len(), COVER_CACHE); + assert!(covers.contains_key("p0")); + } + + #[test] + fn covers_drawn_this_frame_are_kept_even_past_the_cap() { + let mut covers: HashMap = (0..COVER_CACHE + 2) + .map(|n| (format!("p{n}"), ((), 7))) + .collect(); + assert!(evict_covers(&mut covers, &HashSet::new(), 7).is_empty()); + assert_eq!(covers.len(), COVER_CACHE + 2); + } + #[test] fn projection_puts_the_equator_and_meridian_in_the_middle() { let p = project(0.0, 0.0); From ca20e5ed22d7c6f2ee27c810a310580d244f766f Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 20:47:47 +0100 Subject: [PATCH 7/7] perf(gui): keep Takeout staging file access off the main thread The Import page read and wrote its remembered archive list, checked each dropped path and stat'ed every staged archive on the GTK thread. Do all of it on gio's blocking pool. Nothing is saved until the remembered list has been read back, so it can't be overwritten by the empty list shown meanwhile, and a late save never overwrites a newer one. --- crates/pdfs-gui/src/app/main.rs | 4 + crates/pdfs-gui/src/app/pages/takeout.rs | 141 ++++++++++++++++++++--- 2 files changed, 126 insertions(+), 19 deletions(-) diff --git a/crates/pdfs-gui/src/app/main.rs b/crates/pdfs-gui/src/app/main.rs index d6e049f..74fc299 100644 --- a/crates/pdfs-gui/src/app/main.rs +++ b/crates/pdfs-gui/src/app/main.rs @@ -728,6 +728,10 @@ fn build_window(app: &adw::Application) { resume_button: takeout_widgets.resume_button.clone(), inflight: Cell::new(false), running: Cell::new(false), + restored: Cell::new(false), + save_seq: Cell::new(0), + saved_seq: Default::default(), + repaint_seq: Cell::new(0), }, update, }); diff --git a/crates/pdfs-gui/src/app/pages/takeout.rs b/crates/pdfs-gui/src/app/pages/takeout.rs index 37a4370..5e0ad6e 100644 --- a/crates/pdfs-gui/src/app/pages/takeout.rs +++ b/crates/pdfs-gui/src/app/pages/takeout.rs @@ -52,8 +52,9 @@ fn staged_path(ui: &Rc) -> PathBuf { /// Read the remembered set, dropping any archive that has since been moved or /// deleted — a stale path would only fail at launch, and silently is worse. -fn load_staged(ui: &Rc) -> Vec { - let Ok(text) = std::fs::read_to_string(staged_path(ui)) else { +/// Blocking: run it off the GTK thread. +fn load_staged(path: &Path) -> Vec { + let Ok(text) = std::fs::read_to_string(path) else { return Vec::new(); }; let paths: Vec = serde_json::from_str(&text).unwrap_or_default(); @@ -64,9 +65,39 @@ fn load_staged(ui: &Rc) -> Vec { .collect() } +/// Restore whatever was staged when the window last closed, read on a worker. +/// Anything the user staged meanwhile is kept alongside it. +fn restore_staged(ui: &Rc) { + let path = staged_path(ui); + let ui = ui.clone(); + glib::spawn_future_local(async move { + let restored = gio::spawn_blocking(move || load_staged(&path)) + .await + .unwrap_or_default(); + { + let mut staged = ui.takeout.archives.borrow_mut(); + for path in restored { + if !staged.contains(&path) { + staged.push(path); + } + } + staged.sort(); + } + ui.takeout.restored.set(true); + repaint_archives(&ui); + }); +} + /// Remember the staged set. Best-effort: failing to write it costs the user a /// re-pick next time, which is not worth interrupting them over now. +/// +/// Written on a worker. Nothing is written before the remembered set has been +/// read back, so the empty list shown meanwhile can't overwrite it, and a +/// write that lands after a newer one is skipped. fn save_staged(ui: &Rc) { + if !ui.takeout.restored.get() { + return; + } let paths: Vec = ui .takeout .archives @@ -75,12 +106,24 @@ fn save_staged(ui: &Rc) { .map(|p| p.display().to_string()) .collect(); let path = staged_path(ui); - let written = serde_json::to_string(&paths) - .map_err(|e| e.to_string()) - .and_then(|text| std::fs::write(&path, text).map_err(|e| e.to_string())); - if let Err(e) = written { - tracing::warn!("cannot remember staged Takeout archives: {e}"); - } + let seq = ui.takeout.save_seq.get() + 1; + ui.takeout.save_seq.set(seq); + let saved = ui.takeout.saved_seq.clone(); + gio::spawn_blocking(move || { + let Ok(mut last) = saved.lock() else { + return; + }; + if *last > seq { + return; + } + *last = seq; + let written = serde_json::to_string(&paths) + .map_err(|e| e.to_string()) + .and_then(|text| std::fs::write(&path, text).map_err(|e| e.to_string())); + if let Err(e) = written { + tracing::warn!("cannot remember staged Takeout archives: {e}"); + } + }); } fn empty_list_description() -> String { @@ -121,6 +164,16 @@ pub(crate) struct TakeoutState { pub(crate) inflight: Cell, /// Whether the daemon reported an import running on the last poll. pub(crate) running: Cell, + /// Whether the remembered staged set has been read back yet; until then + /// nothing is saved over it. + pub(crate) restored: Cell, + /// The last save started, and the last one written by a worker, so an + /// older save that lands late doesn't overwrite a newer one. + pub(crate) save_seq: Cell, + pub(crate) saved_seq: std::sync::Arc>, + /// Bumped by every repaint of the archive list, so the sizes of an older + /// list that arrive late are not painted over a newer one. + pub(crate) repaint_seq: Cell, } /// Widgets the Import page hands back for wiring. @@ -490,10 +543,10 @@ pub(crate) fn wire_takeout(ui: &Rc, widgets: &TakeoutWidgets) { .back_button .connect_clicked(move |_| ui_back.stack.set_visible_child_name("gallery")); - // Restore whatever was staged when the window last closed, before the first - // paint, so the page comes up as the user left it. - *ui.takeout.archives.borrow_mut() = load_staged(ui); + // Restore whatever was staged when the window last closed, so the page + // comes up as the user left it. repaint_archives(ui); + restore_staged(ui); } /// Stage `paths`, keeping only `.zip` files and skipping ones already staged. @@ -503,15 +556,36 @@ pub(crate) fn wire_takeout(ui: &Rc, widgets: &TakeoutWidgets) { /// the scan reads the archives' central directories, and an extracted tree has /// none — silently importing half of it would be worse than saying no. pub(crate) fn add_archives(ui: &Rc, paths: Vec) { + // Whether each is a regular file is asked of the filesystem, which may be + // a slow network mount: on a worker. + let ui = ui.clone(); + glib::spawn_future_local(async move { + let checked = gio::spawn_blocking(move || { + paths + .into_iter() + .map(|path| { + let file = path.is_file(); + (path, file) + }) + .collect::>() + }) + .await + .unwrap_or_default(); + stage_archives(&ui, checked); + }); +} + +/// Stage the archives [`add_archives`] checked, as `(path, is a file)`. +fn stage_archives(ui: &Rc, paths: Vec<(PathBuf, bool)>) { let mut added = 0usize; let mut rejected = 0usize; { let mut staged = ui.takeout.archives.borrow_mut(); - for path in paths { + for (path, file) in paths { let is_zip = path .extension() .is_some_and(|ext| ext.eq_ignore_ascii_case("zip")); - if !is_zip || !path.is_file() { + if !is_zip || !file { rejected += 1; continue; } @@ -542,12 +616,44 @@ pub(crate) fn add_archives(ui: &Rc, paths: Vec) { } /// Rebuild the staged-archive list and re-derive which actions are available. +/// +/// The archives' sizes are read on a worker, and the list is rebuilt once +/// they are in. pub(crate) fn repaint_archives(ui: &Rc) { + let seq = ui.takeout.repaint_seq.get().wrapping_add(1); + ui.takeout.repaint_seq.set(seq); + let staged = ui.takeout.archives.borrow().clone(); + save_staged(ui); + sync_takeout_actions(ui); + if staged.is_empty() { + paint_archives(ui, Vec::new()); + return; + } + let ui = ui.clone(); + glib::spawn_future_local(async move { + let sized = gio::spawn_blocking(move || { + staged + .into_iter() + .map(|path| { + let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0); + (path, size) + }) + .collect::>() + }) + .await + .unwrap_or_default(); + if ui.takeout.repaint_seq.get() == seq { + paint_archives(&ui, sized); + } + }); +} + +/// Fill the staged-archive list with `staged`, as `(path, size)`. +fn paint_archives(ui: &Rc, staged: Vec<(PathBuf, u64)>) { for row in ui.takeout.rows.borrow_mut().drain(..) { ui.takeout.list_group.remove(&row); } - let staged = ui.takeout.archives.borrow().clone(); let mut rows: Vec = Vec::new(); if staged.is_empty() { ui.takeout @@ -558,8 +664,8 @@ pub(crate) fn repaint_archives(ui: &Rc) { rows.push(row.upcast()); } else { let mut total = 0u64; - for path in &staged { - let size = std::fs::metadata(path).map(|m| m.len()).unwrap_or(0); + for (path, size) in &staged { + let size = *size; total += size; let row = adw::ActionRow::builder() .title( @@ -601,9 +707,6 @@ pub(crate) fn repaint_archives(ui: &Rc) { ))); } *ui.takeout.rows.borrow_mut() = rows; - - save_staged(ui); - sync_takeout_actions(ui); } /// Enable exactly the actions that make sense right now: nothing to launch