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

From: Dmitry Gutov <[email-masked]>
Date: Fri, 5 Aug 2022 17:41:51 +0300

On 05.08.2022 17:20, Eli Zaretskii wrote:
>> Date: Fri, 5 Aug 2022 15:59:57 +0300
>> Cc: [email-masked], [email-masked]
>> From: Dmitry Gutov <[email-masked]>
>>
>> On 01.08.2022 14:58, Eli Zaretskii wrote:
>>
>>> As I wrote elsewhere, I'm okay with extending 'widen' so that it could
>>> "unlock" the locked narrowing, which could then be used in major modes
>>> that convince us their performance is adequate (or clearly announce in
>>> their docs that they don't care about files with long lines ;-).
>>
>> And to address the idea of "unlocking" the narrowing: I think I have
>> demonstrated that the remaining slowdown can be caused purely by the
>> length of the buffer and how long 'parse-partial-sexp' takes to parse
>> it.
> 
> No, you haven't demonstrated that.

Apply the patch for xdisp.c that I have sent previously (it will be at 
the end of this email too) and recompile Emacs.

Now try two different scenarios. 1,2a and 1,2b.

1. Visit dictionary.json. It will ask you whether to open such big file 
literally, but after you answer 'y', it will display the beginning of 
the file quickly.
2a) Evaluate (benchmark 1 '(save-excursion (parse-partial-sexp 1 
(point-max)))), note the reported delay.

Kill and re-visit the file.

1. (same as before)
2b) Press M->, note the delay you see.

The delays in scenarios 1,2a and 1,2b should be ~the same. They are so 
in my testing.

Or try this scenario: 1,2a,2b. Step 2b should work instantly here.

The patch:

diff --git a/src/xdisp.c b/src/xdisp.c
index 099efed2db..02d7f6c562 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -4391,19 +4391,19 @@ handle_fontified_prop (struct it *it)

        eassert (it->end_charpos == ZV);

-      if (current_buffer->long_line_optimizations_p)
-	{
-	  ptrdiff_t begv = it->narrowed_begv;
-	  ptrdiff_t zv = it->narrowed_zv;
-	  ptrdiff_t charpos = IT_CHARPOS (*it);
-	  if (charpos < begv || charpos > zv)
-	    {
-	      begv = get_narrowed_begv (it->w, charpos);
-	      zv = get_narrowed_zv (it->w, charpos);
-	    }
-	  narrow_to_region_internal (make_fixnum (begv), make_fixnum (zv), true);
-	  specbind (Qrestrictions_locked, Qt);
-	}
+      /* if (current_buffer->long_line_optimizations_p) */
+      /* 	{ */
+      /* 	  ptrdiff_t begv = it->narrowed_begv; */
+      /* 	  ptrdiff_t zv = it->narrowed_zv; */
+      /* 	  ptrdiff_t charpos = IT_CHARPOS (*it); */
+      /* 	  if (charpos < begv || charpos > zv) */
+      /* 	    { */
+      /* 	      begv = get_narrowed_begv (it->w, charpos); */
+      /* 	      zv = get_narrowed_zv (it->w, charpos); */
+      /* 	    } */
+      /* 	  narrow_to_region_internal (make_fixnum (begv), make_fixnum 
(zv), true); */
+      /* 	  specbind (Qrestrictions_locked, Qt); */
+      /* 	} */

        /* Don't allow Lisp that runs from 'fontification-functions'
  	 clear our face and image caches behind our back.  */