Utils: Change the priority of the cover caches

Since loadign a pixbuf from the pre-rendered cache is the most
common operation and it does not affect the behavior we can
first check that and then if the cover is midway downloading.

This avoids a mutex lock for the most common path.
This commit is contained in:
Jordan Petridis 2018-08-28 20:53:56 +03:00 committed by Jordan Petridis
parent 822deb2867
commit 273c9f7b99

View File

@ -251,21 +251,6 @@ pub(crate) fn set_image_from_path(
show_id: i32, show_id: i32,
size: u32, size: u32,
) -> Result<(), Error> { ) -> Result<(), Error> {
// Check if there's an active download about this show cover.
// If there is, a callback will be set so this function will be called again.
// If the download succeeds, there should be a quick return from the pixbuf cache_image
// If it fails another download will be scheduled.
if let Ok(guard) = COVER_DL_REGISTRY.read() {
if guard.contains(&show_id) {
let callback = clone!(image => move || {
let _ = set_image_from_path(&image, show_id, size);
glib::Continue(false)
});
gtk::timeout_add(250, callback);
return Ok(());
}
}
if let Ok(hashmap) = CACHED_PIXBUFS.read() { if let Ok(hashmap) = CACHED_PIXBUFS.read() {
// Check if the requested (cover + size) is already in the cache // Check if the requested (cover + size) is already in the cache
// and if so do an early return after that. // and if so do an early return after that.
@ -284,6 +269,21 @@ pub(crate) fn set_image_from_path(
} }
} }
// Check if there's an active download about this show cover.
// If there is, a callback will be set so this function will be called again.
// If the download succeeds, there should be a quick return from the pixbuf cache_image
// If it fails another download will be scheduled.
if let Ok(guard) = COVER_DL_REGISTRY.read() {
if guard.contains(&show_id) {
let callback = clone!(image => move || {
let _ = set_image_from_path(&image, show_id, size);
glib::Continue(false)
});
gtk::timeout_add(250, callback);
return Ok(());
}
}
let (sender, receiver) = unbounded(); let (sender, receiver) = unbounded();
THREADPOOL.spawn(move || { THREADPOOL.spawn(move || {
if let Ok(mut guard) = COVER_DL_REGISTRY.write() { if let Ok(mut guard) = COVER_DL_REGISTRY.write() {