1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-03-22 06:41:04 -07:00

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 a1a76ba06e
commit 89b40650da

View file

@ -535,18 +535,46 @@ 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 ()
(save-excursion
(goto-char (point-min))
(re-search-forward "^diff --git .+ .+$" nil t)
(forward-line 2)
(point)))
(unless (and diff--git-preamble-overlay
(overlay-buffer diff--git-preamble-overlay))
(save-excursion
(goto-char (point-min))
(let ((found (re-search-forward "^diff --git .+ .+$" nil 'move)))
(forward-line 2)
(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-put diff--git-preamble-overlay 'diff--found found))))
(if (not (overlay-get diff--git-preamble-overlay 'diff--found))
(point-min)
(overlay-end diff--git-preamble-overlay)))
(defun diff--git-footer-start ()
(save-excursion
(goto-char (point-max))
(re-search-backward "^-- $" nil t)
(point)))
(unless (and diff--git-footer-overlay
(overlay-buffer diff--git-footer-overlay))
(save-excursion
(goto-char (point-max))
(let ((found (re-search-backward "\n-- $" nil 'move)))
(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)))
(overlay-put diff--git-footer-overlay 'diff--found found))))
(if (not (overlay-get diff--git-footer-overlay 'diff--found))
(point-max)
(1+ (overlay-start diff--git-footer-overlay))))
(defun diff--indicator-matcher-helper (limit regexp)
"Fontify added/removed lines from point to LIMIT using REGEXP.