Subject: bug#77961: 31.0.50; Rendering HTML email is very slow since commit #eab14d68b2e72b9a6b8b0cc67c9667c2bfbed4f5

From: Gerd Möllmann <[email-masked]>
Date: Tue, 22 Apr 2025 10:02:58 +0200

Gerd Möllmann <[email-masked]> writes:

> Ihor Radchenko <[email-masked]> writes:
>
>> Iñigo Serna <[email-masked]> writes:
>>
>>> I attach a profiler report of opening the same HTML email with 
>>> commit
>>> #eab14d68b2e72b9a6b8b0cc67c9667c2bfbed4f5 reverted (ie, no memset, 
>>>  no
>>> for-loop either).
>>
>> what could provide more datapoints is compiling with -g3 and then
>> recording perf profile.
>
> That and maybe more samples with Emacs' profiler, i.e. run it say 100
> times in a loop.

Could you please also check with this diff?

diff --git a/src/dispnew.c b/src/dispnew.c
index 24bf975ef9f..40df078aea6 100644
--- a/src/dispnew.c
+++ b/src/dispnew.c
@@ -504,13 +504,16 @@ adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y
 
 	  while (row < end)
 	    {
-	      row->glyphs[LEFT_MARGIN_AREA]
-		= xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
-			     dim.width, sizeof (struct glyph));
-	      /* We actually need to clear only the 'frame' member, but
-                 it's easier to clear everything.  */
-	      memset (row->glyphs[LEFT_MARGIN_AREA], 0,
-		      dim.width * sizeof (struct glyph));
+	      if (dim.width > matrix->matrix_w || new_rows)
+		{
+		  row->glyphs[LEFT_MARGIN_AREA]
+		    = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
+				 dim.width, sizeof (struct glyph));
+		  /* We actually need to clear only the 'frame' member, but
+		     it's easier to clear everything.  */
+		  memset (row->glyphs[LEFT_MARGIN_AREA], 0,
+			  dim.width * sizeof (struct glyph));
+		}
 
 	      if ((row == matrix->rows + dim.height - 1
 		   /* The mode line, if displayed, never has marginal

This kind of assumes that set-window-configuration is called in a sort
of tight loop, and that window matrix sizes actually don't change.