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 {