Hi all,

Now font lock operation slows down significantly in a buffer of large
doctex document, at least on machines with somewhat poor CPU power.

[How to confirm]
1. Open "latex/preview.dtx" in AUCTeX distribution, with font lock
   enabled.
2. Type C-v or PageDown repeatedly.
3. As you approach the end of the buffer, the lag from the moment of the
   typing until the window contents is actually scrolled up takes longer
   and longer. In my environment, it eventually takes a few seconds.

[How to fix]
Last year font-latex.el changed its strategy for font lock for in-line
math from syntax-based one to search-based one. Since then, it relies
heavily on texmathp.el.

In LaTeX documents, texmathp.el runs efficiently because it optimizes by
a golden rule "math expressions in LaTeX documents don't contain an
empty line". Of course empty lines frequently appear in LaTeX documents.

However, docTeX documents often lack such true empty lines. The above
example "latex/preview.dtx" is such a document. In such buffer,
`texmathp' must search from (point-min) every time, which slows down the
font lock operation.

My tentative proposal to fix this issue is attached below. With this
patch, `texmathp' regards a line containing only whitespaces except "%"
at its beginning as empty in doctex mode buffer. On my machine, this
eliminates lags described above.

This is equivalent to an assumption "math expressions in docTeX
documents don't contain neither such pseudo-empty lines nor true empty
lines". I'm not sure whether that's right or not, so comments and
suggestion are greatly appreciated.

Best regards,
Ikumi Keita

>From 11f3af94b7283b9724cf36d0c37c658f1befb8ca Mon Sep 17 00:00:00 2001
From: Ikumi Keita <[email protected]>
Date: Thu, 8 Jul 2021 01:50:22 +0900
Subject: [PATCH] Fix slowdown of font lock in doctex mode

`texmathp' limits search by looking for empty lines, which appear
frequently in LaTeX documents.  However, docTeX documents often lack
such true empty lines.  In such buffer, `texmathp' must search
from (point-min) every time, which slowed down font lock operation
siginificantly.

* texmathp.el (texmathp): Adjust regular expression so that a line
containing only whitespaces except "%" at its beginning is considered
as empty in doctex mode buffer.
---
 texmathp.el | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/texmathp.el b/texmathp.el
index ac2b75c7..42b3caf6 100644
--- a/texmathp.el
+++ b/texmathp.el
@@ -274,7 +274,10 @@ See the variable `texmathp-tex-commands' about which commands are checked."
   (interactive)
   (let* ((pos (point)) math-on sw-match
          (bound (save-excursion
-                  (if (re-search-backward "[\n\r][ \t]*[\n\r]"
+                  (if (re-search-backward
+                       (if (eq major-mode 'doctex-mode)
+                           "[\n\r]%?[ \t]*[\n\r]"
+                         "[\n\r][ \t]*[\n\r]")
                                           nil 1 texmathp-search-n-paragraphs)
                       (match-beginning 0)
                     (point-min))))
-- 
2.31.1

Reply via email to