Depending on the machine configuration, make your lines longer. 1024*8
is enough to freeze my MacBook Pro 2019 visibly but I need more
characters to slow down an XPS-13 running Ubuntu.
In the real world the issue comes up when I'm enjoying my
compilation-shell-minor-mode buffer but accidentally cat or print some
data with very long lines. Then Emacs freezes and I experience one of my
very rare moments of unhappiness with the editor.
Using `M-x profiler-start, profiler-stop, profiler-report` the culprit
is `compilation-parse-errors`. The function loops over many (about 50)
patterns of potential error output and scans the buffer for them
The patch edits the function to use a limited version of
`re-search-forward`. Instead of finding the pattern everywhere, it only
looks for the pattern in the first 1024 chars of every line. It seems
plausible that real-world compiler warnings would sit close to the
beginning of lines.
With the patch the experience (in combination with global-so-long-mode)
becomes tolerable; while not lightning fast, Emacs is a lot more
responsive in this situation and I as a user retain control to fix the
problem, such as comint-clear-buffer etc.
diff --git a/lisp/progmodes/compile.el b/lisp/progmodes/compile.el
index 7a02c3a896..45c69f330c 100644
@@ -1535,7 +1535,7 @@ to `compilation-error-regexp-alist' if RULES is nil."
(error "HYPERLINK should be an integer: %s" (nth 5 item)))
- (while (re-search-forward pat end t)
+ (while (compilation--re-search-forward-limited pat end 1024)
(when (setq props (compilation-error-properties
file line end-line col end-col
(or type 2) fmt rule))
@@ -1597,6 +1597,28 @@ to `compilation-error-regexp-alist' if RULES is nil."
(match-beginning mn) (match-end mn)
'font-lock-face (cadr props)))))))))
+(defun compilation--re-search-forward-limited (regexp bound n)
+ "Like 're-search-forward limited to the first N chars per line.
+This avoids Emacs performance degradation on scanning excessively
+long lines of text. It is reasonable when scanning for compiler
+warnings to expect to find them early on each line. REGEXP and
+BOUND are as in 're-search-forward."
+ (let ((inhibit-field-text-motion t)
+ (found nil)
+ (orig-point (point)))
+ (while (and (null found)
+ (< (point) bound)
+ (not (eobp)))
+ (setq found (re-search-forward regexp
+ (max (point)
+ (min bound (+ (point-at-bol) n)))
+ (when (null found)
+ (forward-line 1)))
+ (when (null found)
+ (goto-char orig-point))
(defvar-local compilation--parsed -1)
In GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.21,
cairo version 1.16.0)
Windowing system distributor 'The X.Org Foundation', version 11.0.12009000
System Description: Ubuntu 20.04.2 LTS