Skip to content

Commit e6d15fd

Browse files
committed
Optimize diff-hl--update
This adds two optimizations during the buffer traversal. First, (eq buf base) is checked before calling (buffer-base-buffer buf). This short-circuits evaluation to bypass the buffer-base-buffer call overhead. Second, the boolean state of (or changes ref-changes) outside the loop. This prevents the Lisp interpreter from recalculating the same static condition for all active buffers.
1 parent 81b7628 commit e6d15fd

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

diff-hl.el

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -742,22 +742,25 @@ Return a list of line overlays used."
742742
(lambda (ref-changes)
743743
(when (buffer-live-p orig)
744744
(let ((ref-changes (diff-hl-adjust-changes ref-changes changes))
745-
(base (diff-hl--target-buffer orig)))
745+
(base (diff-hl--target-buffer orig))
746+
(has-changes (or changes ref-changes)))
746747
(dolist (buf (buffer-list))
747-
(when (and (buffer-live-p buf)
748-
(eq (diff-hl--target-buffer buf) base)
748+
(when (and (or (eq buf base)
749+
(eq (buffer-base-buffer buf) base))
749750
(buffer-local-value 'diff-hl-mode buf))
750751
(with-current-buffer buf
752+
(diff-hl-remove-overlays)
751753
(let (reuse)
752-
(diff-hl-remove-overlays)
753-
(let ((diff-hl-highlight-function
754-
diff-hl-highlight-reference-function)
755-
(diff-hl-fringe-face-function
756-
diff-hl-fringe-reference-face-function))
757-
(setq reuse (diff-hl--update-overlays ref-changes nil)))
758-
(diff-hl--update-overlays changes reuse)
759-
(when (not (or changes ref-changes))
760-
(diff-hl--autohide-margin))))))))))))))
754+
(when ref-changes
755+
(let ((diff-hl-highlight-function
756+
diff-hl-highlight-reference-function)
757+
(diff-hl-fringe-face-function
758+
diff-hl-fringe-reference-face-function))
759+
(setq reuse (diff-hl--update-overlays ref-changes nil))))
760+
(when changes
761+
(diff-hl--update-overlays changes reuse)))
762+
(unless has-changes
763+
(diff-hl--autohide-margin)))))))))))))
761764

762765
(defun diff-hl--resolve (value-or-buffer cb)
763766
(if (listp value-or-buffer)

0 commit comments

Comments
 (0)