]> git.eshelyaron.com Git - emacs.git/commitdiff
Cache 'string-pixel-width' in 'tab-bar-format-align-right' (bug#78953)
authorJuri Linkov <juri@linkov.net>
Wed, 9 Jul 2025 06:32:18 +0000 (09:32 +0300)
committerEshel Yaron <me@eshelyaron.com>
Thu, 24 Jul 2025 07:52:07 +0000 (09:52 +0200)
* lisp/tab-bar.el (tab-bar--align-right-hash): New internal variable.
(tab-bar-format-align-right): Use memoization for 'string-pixel-width'.

(cherry picked from commit c4674bfa3ae830eedf3d1c2b27fe623e11e7fb25)

lisp/tab-bar.el

index 7ffb07e6cd5a952aa0a192df415e5c2bff2ec26c..9fc255b56847f04eb6ed74ad20978e7bca9d63f5 100644 (file)
@@ -1194,19 +1194,29 @@ when the tab is current.  Return the result as a keymap."
     `((add-tab menu-item ,tab-bar-new-button tab-bar-new-tab
                :help "New tab"))))
 
+(defvar tab-bar--align-right-hash nil
+  "Memoization table for `tab-bar-format-align-right'.")
+
 (defun tab-bar-format-align-right (&optional rest)
   "Align the rest of tab bar items to the right.
 The argument `rest' is used for special handling of this item
 by `tab-bar-format-list' that collects the rest of formatted items.
 This prevents calling other non-idempotent items like
 `tab-bar-format-global' twice."
+  (unless tab-bar--align-right-hash
+    (define-hash-table-test 'tab-bar--align-right-hash-test
+                            #'equal-including-properties
+                            #'sxhash-equal-including-properties)
+    (setq tab-bar--align-right-hash
+          (make-hash-table :test 'tab-bar--align-right-hash-test)))
   (let* ((rest (or rest (tab-bar-format-list
                          (cdr (memq 'tab-bar-format-align-right
                                     tab-bar-format)))))
          (rest (mapconcat (lambda (item) (nth 2 item)) rest ""))
          (hpos (progn
                  (add-face-text-property 0 (length rest) 'tab-bar t rest)
-                 (string-pixel-width rest)))
+                 (with-memoization (gethash rest tab-bar--align-right-hash)
+                   (string-pixel-width rest))))
          (str (propertize " " 'display
                           ;; The `right' spec doesn't work on TTY frames
                           ;; when windows are split horizontally (bug#59620)