From ab4b90723e4664cefef8400e678419e04728b8c2 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 8 Dec 2020 13:57:44 +0100 Subject: [PATCH] Cache toolbar icon data paths * lisp/image.el (find-image): Add an optional CACHE parameter. (find-image--cache): New variable. * lisp/tool-bar.el (tool-bar--image-expression): Use cached data to avoid looking up the image files on each refresh. --- lisp/image.el | 45 +++++++++++++++++++++++++++------------------ lisp/tool-bar.el | 3 ++- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/lisp/image.el b/lisp/image.el index 9ebb603086e..023d64fc4ee 100644 --- a/lisp/image.el +++ b/lisp/image.el @@ -679,8 +679,10 @@ BUFFER nil or omitted means use the current buffer." (setq path (cdr path))) (if found filename))) +(defvar find-image--cache (make-hash-table :test #'equal)) + ;;;###autoload -(defun find-image (specs) +(defun find-image (specs &optional cache) "Find an image, choosing one of a list of image specifications. SPECS is a list of image specifications. @@ -695,26 +697,33 @@ is supported, and FILE exists, is used to construct the image specification to be returned. Return nil if no specification is satisfied. +If CACHE is non-nil, results are cached and returned on subsequent calls. + The image is looked for in `image-load-path'. Image files should not be larger than specified by `max-image-size'." - (let (image) - (while (and specs (null image)) - (let* ((spec (car specs)) - (type (plist-get spec :type)) - (data (plist-get spec :data)) - (file (plist-get spec :file)) - found) - (when (image-type-available-p type) - (cond ((stringp file) - (if (setq found (image-search-load-path file)) - (setq image - (cons 'image (plist-put (copy-sequence spec) - :file found))))) - ((not (null data)) - (setq image (cons 'image spec))))) - (setq specs (cdr specs)))) - image)) + (or (and cache + (gethash specs find-image--cache)) + (let ((orig-specs specs) + image) + (while (and specs (null image)) + (let* ((spec (car specs)) + (type (plist-get spec :type)) + (data (plist-get spec :data)) + (file (plist-get spec :file)) + found) + (when (image-type-available-p type) + (cond ((stringp file) + (if (setq found (image-search-load-path file)) + (setq image + (cons 'image (plist-put (copy-sequence spec) + :file found))))) + ((not (null data)) + (setq image (cons 'image spec))))) + (setq specs (cdr specs)))) + (when cache + (setf (gethash orig-specs find-image--cache) image)) + image))) ;;;###autoload diff --git a/lisp/tool-bar.el b/lisp/tool-bar.el index 84562164300..37f42be3f4d 100644 --- a/lisp/tool-bar.el +++ b/lisp/tool-bar.el @@ -159,7 +159,8 @@ To define items in any other map, use `tool-bar-local-item'." ((< (display-color-cells) 256) ',(list xpm-lo-spec xpm-spec pbm-spec xbm-spec)) (t - ',(list xpm-spec pbm-spec xbm-spec)))))) + ',(list xpm-spec pbm-spec xbm-spec))) + t))) ;;;###autoload (defun tool-bar-local-item (icon def key map &rest props) -- 2.39.2