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); } }); }