Subject: bug#78779: [PATCH] Avoid rebuilding a static alist inside a loop

From: Manuel Giraud <[email-masked]>
Date: Fri, 13 Jun 2025 11:29:47 +0200

Tags: patch

Hi,

This patch avoid constantly rebuilding the same alist inside the main
loop for marking diary entries.

In GNU Emacs 31.0.50 (build 2, x86_64-unknown-openbsd7.7) of 2025-06-13
 built on computer
Repository revision: bec823b107ef7d3b51b8e430ccab82c81bd63d24
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101016
System Description: OpenBSD computer 7.7 GENERIC.MP#1 amd64

Configured using:
 'configure CC=egcc CPPFLAGS=-I/usr/local/include
 LDFLAGS=-L/usr/local/lib MAKEINFO=gmakeinfo --prefix=/home/manuel/emacs
 --bindir=/home/manuel/bin --with-x-toolkit=no
 --with-toolkit-scroll-bars=no --without-cairo --without-dbus
 --without-gconf --without-gsettings --without-compress-install'

>From 34ad15643d718ed0560e899eb5639966afd6e68e Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Fri, 13 Jun 2025 11:25:15 +0200
Subject: [PATCH] Avoid rebuilding a static alist inside a loop

* lisp/calendar/diary-lib.el (diary-mark-entries-1): Build
months-alist outside of the loop.
---
 lisp/calendar/diary-lib.el | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/lisp/calendar/diary-lib.el b/lisp/calendar/diary-lib.el
index 4fdf5150cbd..8fb6fadfe4c 100644
--- a/lisp/calendar/diary-lib.el
+++ b/lisp/calendar/diary-lib.el
@@ -1280,8 +1280,15 @@ diary-mark-entries-1
        (month "[0-9]+\\|\\*")
        (day "[0-9]+\\|\\*")
        (year "[0-9]+\\|\\*"))
-    (let* ((case-fold-search t)
-           marks)
+    (let ((months-alist (if months (calendar-make-alist months)
+                          (calendar-make-alist
+                           calendar-month-name-array
+                           1 nil calendar-month-abbrev-array
+                           (mapcar (lambda (e)
+                                     (format "%s." e))
+                                   calendar-month-abbrev-array))))
+          (case-fold-search t)
+          marks)
       (dolist (date-form diary-date-forms)
         (if (eq (car date-form) 'backup) ; ignore 'backup directive
             (setq date-form (cdr date-form)))
@@ -1363,16 +1370,7 @@ diary-mark-entries-1
                 (if mm-name
                     (setq mm
                           (if (string-equal mm-name "*") 0
-                            (cdr (assoc-string
-                                  mm-name
-                                  (if months (calendar-make-alist months)
-                                    (calendar-make-alist
-                                     calendar-month-name-array
-                                     1 nil calendar-month-abbrev-array
-                                     (mapcar (lambda (e)
-                                               (format "%s." e))
-                                             calendar-month-abbrev-array)))
-                                  t)))))
+                            (cdr (assoc-string mm-name months-alist t)))))
                 (funcall markfunc mm dd yy marks)))))))))
 
 ;;;###cal-autoload
-- 
2.49.0

-- 
Manuel Giraud