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

From: Gregory Heytings <[email-masked]>
Date: Sat, 30 Jul 2022 18:13:18 +0000

>> So the only remaining question is whether it is necessary to recompute 
>> narrowed_begv and narrowed_zv in init_iterator:
>
> I tend to think we should, but let me think about this some more.
>

Okay, I'll wait for your feedback.

Wouldn't it make sense to also limit the portion of the buffer to which 
pre-/post-command-hook have access (see below)?

With that patch, I was able to open and edit a file with a single 50 GB 
(!) line, in js-mode.  Does that still not qualify as "arbitrarily large"?

I compared that with a 50 GB JSON file with 80 character wide lines. 
With that file it was necessary to disable font-lock-mode, which took too 
much time.  Apart from that, I did not see any significant performance 
differences while editing the file, compared to the single line one.

diff --git a/src/keyboard.c b/src/keyboard.c
index 2863058d63..ce529222a3 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -1461,7 +1461,22 @@ command_loop_1 (void)
        }
        Vthis_command = cmd;
        Vreal_this_command = cmd;
-      safe_run_hooks (Qpre_command_hook);
+
+      if (current_buffer->long_line_optimizations_p)
+	{
+	  specpdl_ref count = SPECPDL_INDEX ();
+	  struct window *w = XWINDOW (selected_window);
+	  ptrdiff_t begv = get_narrowed_begv (w, PT);
+	  ptrdiff_t zv = get_narrowed_zv (w, PT);
+	  if (!begv) begv = BEGV;
+	  Fnarrow_to_region (make_fixnum (begv), make_fixnum (zv), Qt);
+	  safe_run_hooks (Qpre_command_hook);
+	  unbind_to (count, Qnil);
+	}
+      else
+	{
+	  safe_run_hooks (Qpre_command_hook);
+	}

        already_adjusted = 0;

@@ -1513,7 +1528,21 @@ command_loop_1 (void)
            }
        kset_last_prefix_arg (current_kboard, Vcurrent_prefix_arg);

-      safe_run_hooks (Qpost_command_hook);
+      if (current_buffer->long_line_optimizations_p)
+	{
+	  specpdl_ref count = SPECPDL_INDEX ();
+	  struct window *w = XWINDOW (selected_window);
+	  ptrdiff_t begv = get_narrowed_begv (w, PT);
+	  ptrdiff_t zv = get_narrowed_zv (w, PT);
+	  if (!begv) begv = BEGV;
+	  Fnarrow_to_region (make_fixnum (begv), make_fixnum (zv), Qt);
+	  safe_run_hooks (Qpost_command_hook);
+	  unbind_to (count, Qnil);
+	}
+      else
+	{
+	  safe_run_hooks (Qpost_command_hook);
+	}

        /* If displaying a message, resize the echo area window to fit
  	 that message's size exactly.  Do this only if the echo area