Skip to content

Commit 0c9cdb3

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 0c9cdb3

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

diff-hl.el

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -742,22 +742,24 @@ 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 (eq (diff-hl--target-buffer buf) base)
749749
(buffer-local-value 'diff-hl-mode buf))
750750
(with-current-buffer buf
751+
(diff-hl-remove-overlays)
751752
(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))))))))))))))
753+
(when ref-changes
754+
(let ((diff-hl-highlight-function
755+
diff-hl-highlight-reference-function)
756+
(diff-hl-fringe-face-function
757+
diff-hl-fringe-reference-face-function))
758+
(setq reuse (diff-hl--update-overlays ref-changes nil))))
759+
(when changes
760+
(diff-hl--update-overlays changes reuse)))
761+
(unless has-changes
762+
(diff-hl--autohide-margin)))))))))))))
761763

762764
(defun diff-hl--resolve (value-or-buffer cb)
763765
(if (listp value-or-buffer)

0 commit comments

Comments
 (0)