mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-21 10:23:48 +01:00
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.
This commit is contained in:
1 parent
b854c1e819
commit
1b545ba369
2 files changed
+78
-22
No files matched your search
@@ -71,6 +71,18 @@ impl<K: Eq + Hash + Clone, V> Lru<K, V> {
|
||||
true
|
||||
}
|
||||
|
||||
/// Look an entry up and mark it used.
|
||||
pub(crate) fn fetch<Q>(&mut self, key: &Q) -> Option<&V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
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<K: Eq + Hash + Clone, V> Lru<K, V> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<gtk4::Picture>, glib::WeakRef<gtk4::Image>);
|
||||
@@ -50,7 +50,8 @@ pub(crate) struct Browser {
|
||||
loading: Cell<bool>,
|
||||
unlocked: Cell<bool>,
|
||||
thumbnail_slots: tokio::sync::Semaphore,
|
||||
thumbnails: RefCell<VecDeque<(bool, String, gtk4::gdk::Texture)>>,
|
||||
/// Decoded thumbnails by whether they are hidden-album ones and uid.
|
||||
thumbnails: RefCell<Lru<(bool, String), gtk4::gdk::Texture>>,
|
||||
thumb_pending: RefCell<HashMap<ThumbnailKey, Vec<ThumbnailTarget>>>,
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user