Subject: Re: bug#56682: Fix the long lines font locking related slowdowns

From: Gregory Heytings <[email-masked]>
Date: Mon, 01 Aug 2022 09:34:18 +0000

Given your and Dmitry's feedback, I just tried to add an explicit call to 
(syntax-ppss (point-max)), without narrowing, when the buffer is opened 
(see below).

The problem is that this is, as I said, slow.  On my laptop, opening a 1 
GB file takes about 6 seconds.  The call to syntax-ppss adds 70 seconds, 
so opening a large file becomes an order of magnitude slower (13 times 
slower).  Which I think is too much for the added benefit.

diff --git a/src/buffer.c b/src/buffer.c
index a07194aef7..bff6dce1d7 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -986,6 +986,7 @@ reset_buffer (register struct buffer *b)
    b->clip_changed = 0;
    b->prevent_redisplay_optimizations_p = 1;
    b->long_line_optimizations_p = 0;
+  b->long_line_syntax_ppss_done_p = 0;
    bset_backed_up (b, Qnil);
    bset_local_minor_modes (b, Qnil);
    BUF_AUTOSAVE_MODIFF (b) = 0;
@@ -2448,6 +2449,7 @@ #define swapfield_(field, type) \
    current_buffer->prevent_redisplay_optimizations_p = 1;
    other_buffer->prevent_redisplay_optimizations_p = 1;
    swapfield (long_line_optimizations_p, bool_bf);
+  swapfield (long_line_syntax_ppss_done_p, bool_bf);
    swapfield (overlays_before, struct Lisp_Overlay *);
    swapfield (overlays_after, struct Lisp_Overlay *);
    swapfield (overlay_center, ptrdiff_t);
diff --git a/src/buffer.h b/src/buffer.h
index 47b4bdf749..3e020f1953 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -686,6 +686,8 @@ #define BVAR(buf, field) ((buf)->field ## _)
       display optimizations must be used.  */
    bool_bf long_line_optimizations_p : 1;

+  bool_bf long_line_syntax_ppss_done_p : 1;
+
    /* List of overlays that end at or before the current center,
       in order of end-position.  */
    struct Lisp_Overlay *overlays_before;
diff --git a/src/xdisp.c b/src/xdisp.c
index 8a19b3bda9..d70ab6f9c1 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -4391,6 +4391,13 @@ handle_fontified_prop (struct it *it)

        if (current_buffer->long_line_optimizations_p)
  	{
+	  if (!current_buffer->long_line_syntax_ppss_done_p)
+	    {
+	      current_buffer->long_line_syntax_ppss_done_p = 1;
+	      specbind (Qinhibit_quit, Qt);
+	      CALLN (Ffuncall, intern ("syntax-ppss"), Fpoint_max ());
+	      unbind_to (count, Qnil);
+	    }
  	  ptrdiff_t begv = it->narrowed_begv ? it->narrowed_begv : BEGV;
  	  ptrdiff_t zv = it->narrowed_zv;
  	  ptrdiff_t charpos = IT_CHARPOS (*it);