Subject: Re: bug#57207: 29.0.50; Fontification is slow after e7b5912b23 (Improvements to long lines handling)

From: Gregory Heytings <[email-masked]>
Date: Sat, 20 Aug 2022 16:50:11 +0000

Ihor, any chance that you could try the patch (which I attach again to 
this post) and tell us if if solves your original problem, namely 
fontification that is slowed down in large Org files?

diff --git a/src/buffer.h b/src/buffer.h
index 47b4bdf749..77f9ea20af 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -149,12 +149,18 @@ #define BUF_OVERLAY_UNCHANGED_MODIFIED(buf) \
 #define BUF_BEG_UNCHANGED(buf) ((buf)->text->beg_unchanged)
 #define BUF_END_UNCHANGED(buf) ((buf)->text->end_unchanged)
 
+#define BUF_CHARS_UNCHANGED_MODIFIED(buf) \
+  ((buf)->text->chars_unchanged_modified)
+
 #define UNCHANGED_MODIFIED \
   BUF_UNCHANGED_MODIFIED (current_buffer)
 #define OVERLAY_UNCHANGED_MODIFIED \
   BUF_OVERLAY_UNCHANGED_MODIFIED (current_buffer)
 #define BEG_UNCHANGED BUF_BEG_UNCHANGED (current_buffer)
 #define END_UNCHANGED BUF_END_UNCHANGED (current_buffer)
+
+#define CHARS_UNCHANGED_MODIFIED \
+  BUF_CHARS_UNCHANGED_MODIFIED (current_buffer)
 
 /* Functions to set PT in the current buffer, or another buffer.  */
 
@@ -268,6 +274,9 @@ #define FETCH_BYTE(n) *(BYTE_POS_ADDR ((n)))
        end_unchanged contain no useful information.  */
     modiff_count overlay_unchanged_modified;
 
+    /* CHARS_MODIFF as of last redisplay that finished.  */
+    modiff_count chars_unchanged_modified;
+
     /* Properties of this buffer's text.  */
     INTERVAL intervals;
 
diff --git a/src/xdisp.c b/src/xdisp.c
index 0248e8e53f..c92231abcf 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -17323,6 +17323,7 @@ mark_window_display_accurate_1 (struct window *w, bool accurate_p)
 
       BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
       BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
+      BUF_CHARS_UNCHANGED_MODIFIED (b) = BUF_CHARS_MODIFF (b);
       BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
       BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
 
@@ -19585,7 +19586,7 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
   /* Check whether the buffer to be displayed contains long lines.  */
   if (!NILP (Vlong_line_threshold)
       && !current_buffer->long_line_optimizations_p
-      && MODIFF - UNCHANGED_MODIFIED > 8)
+      && CHARS_MODIFF - CHARS_UNCHANGED_MODIFIED > 8)
     {
       ptrdiff_t cur, next, found, max = 0, threshold;
       threshold = XFIXNUM (Vlong_line_threshold);