Subject: bug#73231: 30.0.91; image-dired cannot be operated until all thumbnails are created (MS-Windows)

From: AKIYAMA Kouhei <[email-masked]>
Date: Fri, 13 Sep 2024 22:07:48 +0900

When using image-dired without ImageMagick or GraphicsMagick
available, operations could not be performed until all thumbnails had
been created.

* Steps to reproduce

 1. Open a command prompt (cmd.exe)
 2. wget https://alpha.gnu.org/gnu/emacs/pretest/windows/emacs-30/emacs-30.0.91.zip
 3. unzip emacs-30.0.91.zip
 4. set PATH=c:\Windows\system32
    (Make sure ImageMagick is not included)
 5. .\bin\emacs -Q --init-directory=%CD%\.emacs.d
    (Start with a brand new .emacs.d)
 6. C-x C-f <my photo directory> RET
    (<my photo directory> contains 247 jpg files)
 7. M-x image-dired RET RET
 8. After 36 seconds, the *image-dired* buffer is open with all
    thumbnails converted.

    (The aspect ratio of the image will be ignored and it will not be
    rotated correctly, but that's a separate issue.)

Let me compare it with my previous setup. I'm using MSYS2's magick.exe.

 9. C-x C-c
    (exit emacs)
 10. rmdir /S .emacs.d
     (Remove thumbnails)
 11. .\bin\emacs -Q --init-directory=%CD%\.emacs.d
 12. (setq image-dired-cmd-create-thumbnail-program
       "c:/msys64/ucrt64/bin/magick.exe"
       image-dired-cmd-create-thumbnail-options
       '("convert" "-auto-orient" "-define" "jpeg:size=%wx%h" "-size" "%wx%h"
         "%f[0]" "-resize" "%wx%h>" "-strip" "jpeg:%t"))
 13. C-x C-f <my photo directory> RET
 14. M-x image-dired RET RET
 15. After 5 seconds the *image-dired* buffer is opened. Only the first
     few thumbnails have been converted.
 16. After 19 seconds (from start) all thumbnails have been converted
     and displayed.

* Cause:

The cause is the image-dired-thumb-queue-run function and the
image-dired-create-thumb function that calls it.

The image-dired-create-thumb function executes
image-dired-thumb-queue-run with a delay using (run-at-time 0 nil
#'image-dired-thumb-queue-run). Later, image-dired-thumb-queue-run is
called all at once in succession.

In the latter half of the image-dired-thumb-queue-run function,
image-dired-create-thumb-2 is executed with a delay using
(run-with-timer 0.05 nil #'image-dired-create-thumb-2 orig-file
thumb-file). There is no limit to the number of timers, so a new timer
is added even if one has already been scheduled. Therefore, all
thumbnails are created continuously and without interruption. The
number of seconds, 0.05, is not very meaningful.

(I apologize if these behaviors were intended)

* Solution:

You can achieve a similar behavior by limiting the number of images
when using the image-dired-create-thumb-2 function.

I tried modifying the latter half of the image-dired-thumb-queue-run
function as follows and tested it again.

    ;; We are on MS-Windows with ImageMagick/GraphicsMagick, and need to
    ;; generate thumbnails by our lonesome selves.
    (while (and image-dired-queue
                (< image-dired-queue-active-jobs 1))
      (cl-incf image-dired-queue-active-jobs)
      (let* ((job (pop image-dired-queue))
             (orig-file (car job))
             (thumb-file (cadr job)))
        (run-with-timer 0.00 nil
                        (lambda (a b)
                          (cl-decf image-dired-queue-active-jobs)
                          (image-dired-create-thumb-2 a b))
                        orig-file thumb-file)))

As a result, the *image-dired* buffer appeared after 17 seconds, and
all thumbnails were created after 34 seconds.

The performance has dropped, but this is because
image-dired--probe-thumbnail-cmd is slow.  So I set it as follows to
avoid matching "convert", and avoided the slow check process.

  (setq image-dired-cmd-create-thumbnail-program "")

I tested it again, and the *image-dired* buffer appeared after 5
seconds, and all thumbnails were created after 7 seconds. This was
faster than when I used the magick command (despite running it in
multiple processes), and I got very good results.  I think it's better
not to call image-dired--probe-thumbnail-cmd too frequently.

Recently, I have been increasingly using image-dired for organizing my
photos. I look forward to seeing how image-dired continues to
evolve. Thank you.
-------
In GNU Emacs 30.0.91 (build 2, x86_64-w64-mingw32) of 2024-09-12 built
 on AVALON
Windowing system distributor 'Microsoft Corp.', version 10.0.19045
System Description: Microsoft Windows 10 Enterprise (v10.0.2009.19045.4894)

Configured using:
 'configure --with-modules --without-dbus --with-native-compilation=aot
 --without-compress-install --with-tree-sitter
 --enable-checking=yes,glyphs 'CFLAGS=-O0 -g3''

Configured features:
ACL GIF GMP GNUTLS HARFBUZZ JPEG LCMS2 LIBXML2 MODULES NATIVE_COMP
NOTIFY W32NOTIFY PDUMPER PNG RSVG SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XPM ZLIB

(NATIVE_COMP present but libgccjit not available)

Important settings:
  value of $LANG: ja_JP.CP932
  locale-coding-system: cp932

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort emacsbug mail-extr message sendmail mailcap yank-media puny
dired dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg
rfc6068 epg-config gnus-util text-property-search time-date subr-x
mm-decode mm-bodies mm-encode mail-parse rfc2231 rfc2047 rfc2045 mm-util
ietf-drums mail-prsvr mailabbrev mail-utils gmm-utils mailheader
cl-loaddefs cl-lib japan-util rmc iso-transl tooltip cconv eldoc paren
electric uniquify ediff-hook vc-hooks lisp-float-type elisp-mode mwheel
touch-screen dos-w32 ls-lisp disp-table term/w32-win w32-win w32-vars
term/common-win tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode lisp-mode prog-mode register
page tab-bar menu-bar rfn-eshadow isearch easymenu timer select
scroll-bar mouse jit-lock font-lock syntax font-core term/tty-colors
frame minibuffer nadvice seq simple cl-generic indonesian philippine
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads w32notify w32 lcms2 multi-tty move-toolbar make-network-process
native-compile emacs)

Memory information:
((conses 16 58721 16525) (symbols 48 6296 1) (strings 32 17245 3277)
 (string-bytes 1 453015) (vectors 16 10080)
 (vector-slots 8 252453 22466) (floats 8 32 648)
 (intervals 56 1127 635) (buffers 992 11))

-- 
# This report was created using machine translation.
AKIYAMA Kouhei
[email-masked]