Subject: bug#42267: Fontification takes a long time when an equation contains a double prime

From: Ikumi Keita <[email-masked]>
Date: Fri, 10 Jul 2020 01:37:25 +0900

Hi Najib,

>>>>> Najib Idrissi <[email-masked]> writes:
> Sure, see the attached file. As I said, it's a file filled with lipsum
> with a single equation at the bottom.

>> AUCTeX's fontification for math went through a bigger overhaul.  Please
>> share the file and a recipe (preferably starting with `emacs -Q') how to
>> trigger your obeservation, that would help a lot.

> 1. Open the file and go to the equation at the bottom
> 2. Notice that editing speed inside the equation is normal (e.g. hold
>    the space bar inside the equation and notice that it goes at a
>    normal speed)
> 3. Add a double prime somewhere in the equation, e.g. after the 2 so
>    that it becomes 1+1=2''
> 4. Notice that now editing speed is degraded, for example hold the
>    space bar at the end of the equation and notice that there is now a
>    stutter. If your CPU is too fast and you don't notice the stutter,
>    you can just copy more paragraphs of lipsum, I guess.

> In my real life example (that I cannot share because the article in
> question is not public yet) the slowdown is so bad that a single
> keystroke inside an affected equation can take several seconds.

Thank you for providing concrete example and procedure to reproduce. I
experience the slowdown you described.

The attached patch fixes, or at least reduces, the slowdown on my side.
Could you check whether it works for you or not?

Best Regards,
Ikumi Keita

diff --git a/font-latex.el b/font-latex.el
index d7d26fab..7c88dff2 100644
--- a/font-latex.el
+++ b/font-latex.el
@@ -2076,6 +2076,9 @@ set to french, and >>german<< (and 8-bit) are used if set to german."
 			     (setq nest-count (1+ nest-count))
 			   (when (/= nest-count 0)
 			     (setq nest-count (1- nest-count)))))))
+	      (unless (and (> (point) point-of-surrender)
+			   (not (bobp)))
+		(throw 'extend nil))
 	      (when (< (point) font-lock-beg)
                 (setq font-lock-beg (point))
 		(throw 'extend t)))))))))