From 89b40650da20b70e32872e0bb08bac48ea251781 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Thu, 19 Mar 2026 14:50:21 -0400 Subject: [PATCH] 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. --- lisp/vc/diff-mode.el | 46 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 86095780f67..8a432a7f618 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -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.