I came up with the attached patch.  It increases EMMS cover lookups (in
the browser and in Helm-EMMS) by an order of magnitude.

-- 
Pierre Neidhardt
https://ambrevar.xyz/
From 6013464eb7295fcd749b3146f758fc9295fbe11a Mon Sep 17 00:00:00 2001
From: Pierre Neidhardt <[email protected]>
Date: Thu, 21 Feb 2019 17:46:58 +0100
Subject: [PATCH] * lisp/emms-browser.el: Add
 emms-browser-cache-thumbnail-async for faster thumbnail lookups.

---
 doc/emms.texinfo     |  8 ++++++++
 lisp/emms-browser.el | 24 ++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/doc/emms.texinfo b/doc/emms.texinfo
index dc6664d..0777e14 100644
--- a/doc/emms.texinfo
+++ b/doc/emms.texinfo
@@ -1677,6 +1677,14 @@ upon change in the source folder.
 Customize @var{emms-browser-covers-file-extensions} to include or exclude
 specific extensions.
 
+'emms-browser-cache-thumbnail' might be everytime a cover is queried, so
+to help with performance you can also set @var{emms-browser-covers} to
+'emms-browser-cache-thumbnail-async'.  The latter is like the former
+except that it caches queries: every subsequent query will be much
+faster.  The drawback is that it won't see changes made to covers after
+the first query.  To force-refresh the thumbnail cache, you can run
+'emms-browser-clear-cache-hash'.
+
 Also, Emacs by default will jump around a lot when scrolling a buffer
 with images. In order to prevent that, you can set
 @var{scroll-up-aggressively} and @var{scroll-down-aggressively} to the
diff --git a/lisp/emms-browser.el b/lisp/emms-browser.el
index b930d8b..f805b3b 100644
--- a/lisp/emms-browser.el
+++ b/lisp/emms-browser.el
@@ -2226,5 +2226,29 @@ will always use the same cover per folder.
                    (setq cache-dest-file nil))))
           cache-dest-file)))))
 
+(defvar emms-browser--cache-hash nil
+  "Cache for `emms-browser-cache-thumbnail-async'.")
+
+(defun emms-browser-cache-thumbnail-async (dir size)
+  "Like `emms-browser-cache-thumbnail' but caches queries for faster lookups.
+The drawback is that if changes are made to the covers in DIR
+after `emms-browser-cache-thumbnail-async' queried them, it won't
+be taken into account.  Call `emms-browser-clear-cache-hash' to
+refresh the cache."
+  (unless emms-browser--cache-hash
+    (setq emms-browser--cache-hash (make-hash-table :test 'equal)))
+  (let* ((key (cons dir size))
+         (val (gethash key emms-browser--cache-hash)))
+    (or val
+        (puthash key (emms-browser-cache-thumbnail dir size)
+                 emms-browser--cache-hash))))
+
+(defun emms-browser-clear-cache-hash ()
+  "Resets `emms-browser-cache-thumbnail-async' cache.
+This is useful if there were changes on disk after
+`emms-browser-cache-thumbnail-async' first cached them."
+  (interactive)
+  (clrhash emms-browser--cache-hash))
+
 (provide 'emms-browser)
 ;;; emms-browser.el ends here
-- 
2.20.1

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Emms-help mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/emms-help

Reply via email to