Subject: bug#52298: 29.0.50; Frequent redisplay cycles induced by c-type-finder-timer-func timer in CC Mode

From: Alan Mackenzie <[email-masked]>
Date: Fri, 10 Dec 2021 22:52:40 +0000

Hello, Eli.

On Fri, Dec 10, 2021 at 20:51:32 +0200, Eli Zaretskii wrote:
> > Date: Fri, 10 Dec 2021 18:16:21 +0000
> > Cc: [email-masked], [email-masked]
> > From: Alan Mackenzie <[email-masked]>

> > > Both of these are called from the timer function.  Are they using
> > > with-silent-modifications?

> > I'm pretty sure they are.

> > I think that modify_text_properties is calling modiff_incr even when
> > inhibit_modification_hooks is non-nil.  I tried putting an `if' around
> > that bit of the code, without any great success.

> AFAIK, with-silent-modifications is supposed to prevent BUF_MODIFF
> from increasing.

I don't think it does in the modify_text_properties case.

> Are you sure you see that?  And what kind of 'if' did you try to put
> and where?

In modify_text_properties, around the modiff_incr bit:

diff --git a/src/textprop.c b/src/textprop.c
index d7d6a66923..d91b8624ef 100644
--- a/src/textprop.c
+++ b/src/textprop.c
@@ -85,10 +85,13 @@ modify_text_properties (Lisp_Object buffer,
Lisp_Object start, Lisp_Object end)

   prepare_to_modify_buffer_1 (b, e, NULL);

-  BUF_COMPUTE_UNCHANGED (buf, b - 1, e);
-  if (MODIFF <= SAVE_MODIFF)
-    record_first_change ();
-  modiff_incr (&MODIFF);
+  if (!inhibit_modification_hooks)
+    {
+      BUF_COMPUTE_UNCHANGED (buf, b - 1, e);
+      if (MODIFF <= SAVE_MODIFF)
+       record_first_change ();
+      modiff_incr (&MODIFF);
+    }

   bset_point_before_scroll (current_buffer, Qnil);

> > The main reason for all the redisplaying (which I got from
> > trace-redisplay displaying "redisplay_preserve_echo_area (8)") is the
> > call from detect_input_pending_run_timers in keyboard.c.  It is calling
> > redisplay_preserve_echo_area each time the timer triggers.

> This is normal, not something you need to investigate: every time a
> timer function fires, we make one more iteration through the Emacs
> idle loop, and that includes a call to redisplay_preserve_echo_area.

Ah, OK.

> Once again, the problem is not that redisplay is invoked, the problem
> is that it doesn't exit almost immediately, after detecting that
> nothing's changed.

I'm again not entirely convinced we have a problem.  When trace-redisplay
is enabled on my machine, and xdisp.c visited, Emacs uses between 20% and
25% of one CPU core for a little under 2 minutes (a 4½ year old Ryzen).
After this it is down to 0.3%.  All the time it is outputting
trace-redisplay messages, two I think for each timer iteration.

I'm a bit surprised at the moment it's taking so long to do the initial
found-type scanning, but it's not all that bad.  The
--enable-cheking=yes,glyphs will have slowed the machine down somewhat.
One refinement would be to turn off the timer when All the CC Mode
buffers have been scanned, only reenabling it when a new CC Mode buffer
gets loaded.  That might save that 0.3% core time at the end of the
found-type scan.

-- 
Alan Mackenzie (Nuremberg, Germany).