Skip to content

Commit 9bee6d1

Browse files
committed
Optimize diff-hl--update
This adds optimizations to the buffer traversal and overlay application logic: - Cache the boolean state of (or changes ref-changes) into a local variable 'has-changes' outside the loop to prevent the Lisp interpreter from recalculating it for all active buffers. - Bind inhibit-redisplay to t during the resolution callback to prevent the display engine from rendering intermediate UI states during asynchronous overlay updates. - Conditionally execute diff-hl--update-overlays only when 'changes' or 'ref-changes' are non-nil, avoiding unnecessary function calls.
1 parent 81b7628 commit 9bee6d1

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
@@ -741,23 +741,25 @@ Return a list of line overlays used."
741741
reference
742742
(lambda (ref-changes)
743743
(when (buffer-live-p orig)
744-
(let ((ref-changes (diff-hl-adjust-changes ref-changes changes))
744+
(let ((inhibit-redisplay t)
745+
(ref-changes (diff-hl-adjust-changes ref-changes changes))
745746
(base (diff-hl--target-buffer orig)))
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 (or changes ref-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)