Skip to content

Commit

Permalink
LIBRARY also cache no thumbnail
Browse files Browse the repository at this point in the history
Every read_art() involves scanning a whole folder on disk so that
shouldn't happen every frame
  • Loading branch information
Beinsezii committed Aug 19, 2024
1 parent 66ff298 commit d7ede21
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub struct Library {
statusline: RwLock<String>,
theme: RwLock<Theme>,
art: RwLock<Option<Arc<RawImage>>>,
thumbnails: RwLock<HashMap<(usize, usize, PathBuf), Arc<RawImage>>>,
thumbnails: RwLock<HashMap<(usize, usize, PathBuf), Option<Arc<RawImage>>>>,
}

impl Library {
Expand Down Expand Up @@ -532,14 +532,17 @@ impl Library {
pub fn thumbnail(&self, w: usize, h: usize) -> Option<Arc<RawImage>> {
let Some(track) = self.track_get() else { return None };
if let Ok(Some(thumbnail)) = self.thumbnails.timed_read().as_deref().map(|hm| hm.get(&(w, h, track.path().to_owned()))) {
return Some(thumbnail.clone());
return thumbnail.clone();
}

self.read_art();
let Ok(mut thumbnail_writer) = self.thumbnails.timed_write() else {
return None;
};

self.read_art();
let art_reader = &self.art.timed_read();
let Ok(Some(art)) = art_reader.as_deref() else { return None };
let Ok(mut thumbnail_writer) = self.thumbnails.timed_write() else {
let Ok(Some(art)) = art_reader.as_deref() else {
thumbnail_writer.insert((w, h, track.path().to_owned()), None);
return None;
};

Expand Down Expand Up @@ -592,9 +595,9 @@ impl Library {
art.len()
);

let new_thumb = Arc::new(thumbnail);
let new_thumb = Some(Arc::new(thumbnail));
thumbnail_writer.insert((w, h, track.path().to_owned()), new_thumb.clone());
Some(new_thumb)
new_thumb
}

// ## Other Settings ## }}}
Expand Down

0 comments on commit d7ede21

Please sign in to comment.