diff-mode.el: Don't recompute the position of Git preamble/footer

On some profiles (after disabling syntax and refined
fontification), this was taking >90% of the time to fontify a buffer.

* lisp/vc/diff-mode.el (diff--git-preamble-overlay)
(diff--git-footer-overlay): New variables.
(diff--git-preamble-end, diff--git-footer-start): Use them to cache
the result.
This commit is contained in:
Stefan Monnier 2026-03-19 14:50:21 -04:00
parent 32e8989157
commit 43304ea735

View file

@ -535,18 +535,38 @@ use the face `diff-removed' for removed lines, and the face
(group (any "<-"))
(group (zero-or-more nonl) "\n")))
(defvar-local diff--git-preamble-overlay nil)
(defvar-local diff--git-footer-overlay nil)
(defun diff--git-preamble-end ()
(unless (and diff--git-preamble-overlay
(overlay-buffer diff--git-preamble-overlay))
(save-excursion
(goto-char (point-min))
(re-search-forward "^diff --git .+ .+$" nil t)
(forward-line 2)
(point)))
(if diff--git-preamble-overlay
(move-overlay diff--git-preamble-overlay (point-min) (point))
(let ((ol (make-overlay (point-min) (point))))
(overlay-put ol 'modification-hooks (lambda (&rest _) (delete-overlay ol)))
(overlay-put ol 'evaporate t)
(setq diff--git-preamble-overlay ol)))))
(overlay-end diff--git-preamble-overlay))
(defun diff--git-footer-start ()
(unless (and diff--git-footer-overlay
(overlay-buffer diff--git-footer-overlay))
(save-excursion
(goto-char (point-max))
(re-search-backward "^-- $" nil t)
(point)))
(re-search-backward "\n-- $" nil t)
(if diff--git-footer-overlay
(move-overlay diff--git-footer-overlay
(match-beginning 0) (point-max))
(let ((ol (make-overlay (match-beginning 0) (point-max) nil t t)))
(overlay-put ol 'modification-hooks (lambda (&rest _) (delete-overlay ol)))
(overlay-put ol 'evaporate t)
(setq diff--git-footer-overlay ol)))))
(1+ (overlay-start diff--git-footer-overlay)))
(defun diff--indicator-matcher-helper (limit regexp)
"Fontify added/removed lines from point to LIMIT using REGEXP.