unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Manuel Giraud via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 68006@debbugs.gnu.org
Subject: bug#68006: 30.0.50; Image-mode speed
Date: Fri, 29 Dec 2023 12:11:44 +0100	[thread overview]
Message-ID: <87wmsxmff3.fsf@ledu-giraud.fr> (raw)
In-Reply-To: <83o7eb938y.fsf@gnu.org> (Eli Zaretskii's message of "Wed, 27 Dec 2023 15:36:13 +0200")

[-- Attachment #1: Type: text/plain, Size: 945 bytes --]

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Manuel Giraud <manuel@ledu-giraud.fr>
>> Cc: 68006@debbugs.gnu.org
>> Date: Wed, 27 Dec 2023 13:13:13 +0100
>> 
>> I was comtemplating the idea of using 'image-dired-contents-sha1' to
>> ensure that the image contents has not changed on disk.  But then I
>> asked myself if a function "give me a unique id of this file based on
>> its content" does not already exist in Emacs.  I could not found one but
>> maybe we could create one (eg. file-unique-identifier)?
>> 
>> Or do you prefer to keep this into the image library?  In which case,
>> i'll move this function to "image.el" or "image-mode.el".
>
> I think for now it should be kept in the image library, yes.  I don't
> quite see why it would be needed for other files (if it was, we would
> have had it already by now).

Hi Eli,

What about this new patch?  It works for me as intended but might need
better names and better docs.  Thanks.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-some-user-control-in-image-mode-caching.patch --]
[-- Type: text/x-patch, Size: 4183 bytes --]

From c131c03dafedbdaa9035c95386a081c452f00934 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <manuel@ledu-giraud.fr>
Date: Fri, 29 Dec 2023 11:48:21 +0100
Subject: [PATCH] Add some user control in image-mode caching

* lisp/image-mode.el (image-mode-eager-cache-flush): New custom.
(image-toggle-display-image): Add a contents hash in image spec
when flushing cache lazily.

* lisp/image/image-dired-util.el (image-dired-thumb-name):
* lisp/image.el (image-contents-sha1): Move
'image-dired-contents-sha1' to 'image-contents-sha1' in toplevel
image library.
---
 lisp/image-mode.el             | 18 ++++++++++++++++--
 lisp/image.el                  | 11 ++++++++++-
 lisp/image/image-dired-util.el |  8 +-------
 3 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index d5ca6348c92..bad6624bc96 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -93,6 +93,14 @@ image-auto-resize-on-window-resize
   :version "27.1"
   :group 'image)
 
+(defcustom image-mode-eager-cache-flush t
+  "Non-nil means flush image from cache eagerly.
+When set to nil, be aware that the image cache could grow fast
+but an image previously displayed will show faster."
+  :type 'boolean
+  :version "30.1"
+  :group 'image)
+
 (defvar-local image-transform-resize nil
   "The image resize operation.
 Non-nil to resize the image upon first display.
@@ -954,8 +962,14 @@ image-toggle-display-image
                   (plist-put (cdr image) :width
                              (plist-get (cdr image) :max-width)))))
 
-    ;; Discard any stale image data before looking it up again.
-    (image-flush image)
+    (if image-mode-eager-cache-flush
+        ;; Discard any stale image data before looking it up again.
+        (image-flush image)
+      ;; Add a content based hash into image spec to be sure that the
+      ;; cache is updated should the on disk image change.
+      (when (and filename (file-exists-p filename))
+        (setq image (append image (list :hash (image-contents-sha1 filename))))))
+
     (setq image (append image (image-transform-properties image)))
     (setq props
 	  `(display ,image
diff --git a/lisp/image.el b/lisp/image.el
index e20fbcf4c98..072549aa88f 100644
--- a/lisp/image.el
+++ b/lisp/image.el
@@ -401,7 +401,16 @@ image-type-from-file-name
 	    ;; If nothing seems to be supported, return first type that matched.
 	    (or first (setq first type))))))))
 
- ;;;###autoload
+;;;###autoload
+(defun image-contents-sha1 (filename)
+  "Compute the SHA-1 of the first 4KiB of FILENAME's contents.  The
+first 4KiB does not represent the whole file but was chosen
+because it is fast enough for interactive usage."
+  (with-temp-buffer
+    (insert-file-contents-literally filename nil 0 4096)
+    (sha1 (current-buffer))))
+
+;;;###autoload
 (defun image-supported-file-p (file)
   "Say whether Emacs has native support for displaying TYPE.
 The value is a symbol specifying the image type, or nil if type
diff --git a/lisp/image/image-dired-util.el b/lisp/image/image-dired-util.el
index e17cc6c919f..e5f526fec57 100644
--- a/lisp/image/image-dired-util.el
+++ b/lisp/image/image-dired-util.el
@@ -59,12 +59,6 @@ image-dired-dir
       (message "Thumbnail directory created: %s" image-dired-dir))
     image-dired-dir))
 
-(defun image-dired-contents-sha1 (filename)
-  "Compute the SHA-1 of the first 4KiB of FILENAME's contents."
-  (with-temp-buffer
-    (insert-file-contents-literally filename nil 0 4096)
-    (sha1 (current-buffer))))
-
 (defun image-dired-thumb-name (file)
   "Return absolute file name for thumbnail FILE.
 Depending on the value of `image-dired-thumbnail-storage' and
@@ -99,7 +93,7 @@ image-dired-thumb-name
            (concat (md5 (concat "file://" file)) ".png")
            (expand-file-name thumbdir (xdg-cache-home))))
       (let ((name (if (eq 'sha1-contents image-dired-thumb-naming)
-                      (image-dired-contents-sha1 file)
+                      (image-contents-sha1 file)
                     ;; Defaults to SHA-1 of file name
                     (sha1 file))))
         (cond ((or (eq 'image-dired image-dired-thumbnail-storage)
-- 
2.43.0


[-- Attachment #3: Type: text/plain, Size: 18 bytes --]

-- 
Manuel Giraud

  reply	other threads:[~2023-12-29 11:11 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-24 16:44 bug#68006: 30.0.50; Image-mode speed Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-24 17:01 ` Eli Zaretskii
2023-12-25 10:34   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-25 13:36     ` Eli Zaretskii
2023-12-25 18:59       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-25 19:30         ` Eli Zaretskii
2023-12-26 14:45           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-26 17:15             ` Eli Zaretskii
2023-12-26 18:07               ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-26 18:43                 ` Eli Zaretskii
2023-12-27 12:13                   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-27 13:36                     ` Eli Zaretskii
2023-12-29 11:11                       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-12-29 12:13                         ` Eli Zaretskii
2023-12-30 11:36                           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-30 12:37                           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-30 23:57                             ` Stefan Kangas
2023-12-31  7:16                               ` Eli Zaretskii
2024-01-02  0:19                                 ` Stefan Kangas
2024-01-02 12:10                                   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-02 12:49                                   ` Eli Zaretskii
2024-01-02 16:04                                     ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-02 17:02                                       ` Eli Zaretskii
2024-01-04 16:47                                         ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-04 17:43                                           ` Eli Zaretskii
2024-01-04 18:42                                             ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-04 18:55                                               ` Eli Zaretskii
2024-01-04 19:16                                                 ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-04 19:54                                                   ` Eli Zaretskii
2024-01-05 10:50                                                     ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-05 11:25                                                       ` Eli Zaretskii
2024-01-05 13:26                                                         ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-05 13:40                                                           ` Eli Zaretskii
2024-01-05 14:35                                                             ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-05 14:41                                                               ` Eli Zaretskii
2024-01-05 14:54                                                                 ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-06 13:07                                                                 ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-01 10:10                               ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wmsxmff3.fsf@ledu-giraud.fr \
    --to=bug-gnu-emacs@gnu.org \
    --cc=68006@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=manuel@ledu-giraud.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).