Subject: bug#15876: 24.3.50; Highly degraded performance between rev 114715 and 115006

From: Eli Zaretskii <[email-masked]>
Date: Wed, 11 Dec 2013 18:28:31 +0200

> Date: Wed, 11 Dec 2013 10:52:24 +0400
> From: Dmitry Antipov <[email-masked]>
> CC: [email-masked]
> 
> IMO the real problem is that redisplay sometimes isn't clever enough about requesting
> font for the particular character (0x25b7 in our case):

Not sure if this is for redisplay to solve.  Redisplay just needs a
face for displaying a character, it knows (almost) nothing about fonts
and fontsets.  It's the font selection machinery that needs to become
smarter, I think.

> Such a request produces a lot of font-entity objects, which are not needed (and becomes
> reachable only via font cache) after the "best match" font is selected for the particular
> character. Next, if font loading is too memory-intensive and gc_cons_threshold was hit, GC
> clears font cache. Next cursor movement triggers redisplay, which in turn asks for the "best
> match" for 0x25b7 again. If font cache is never cleared, this is acceptable because all possible
> matches (represented as a vector of font-entities) are cached. But, if font cache is cleared,
> font_list_entities calls to driver->list function, which creates a lot of font-entities again,
> etc., etc.

Right, this matches my observations.  Did you succeed in finding which
function in this call sequence is the performance bottleneck?

> Until we have somewhat smarter redisplay, possible solution is to clear font cache when it
> becomes larger than some specified size rather than at each GC. Now I have the virtual machine
> with Windows 7 installed, and this fix looks reasonable for me. Could you also please try it?

It solves the immediate problem with this single character, but if I
add a few others from other Unicode blocks, the problem reappears.
This is because the threshold of 4096 seems to be too low: I've seen
the number go above 10K a couple of times.

Anyway, what about the patch below?  With it, the problem disappears
even without your "threshold" based GC.

We could be even more aggressive, and search also the face caches on
all frames (but then, if found, the face needs to be cached on the
current frame as well, before using it) -- this is left as an
exercise ;-)

The problem with this approach is that there's no guarantee we will
find a "best-match" font for a character.  However, I displayed
etc/HELLO with and without the patch, and didn't see any difference.
Maybe some font selection expert could tell if we lose anything
important by going this way.  Note that NS was already reusing the
argument FACE (but it alone) in this way.

--- src/fontset.c~	2013-11-05 06:42:21.000000000 +0200
+++ src/fontset.c	2013-12-11 11:30:03.909213300 +0200
@@ -937,7 +937,6 @@
   if (ASCII_CHAR_P (c) || face->fontset < 0)
     return face->ascii_face->id;
 
-#ifdef HAVE_NS
   if (face->font)
     {
       /* Fonts often have characters in other scripts, like symbol, even if they
@@ -946,9 +945,22 @@
          perhaps be general?  */
       Lisp_Object font_object;
       XSETFONT (font_object, face->font);
-      if (font_has_char (f, font_object, c)) return face->id;
+      if (font_has_char (f, font_object, c))
+	return face->id;
+    }
+
+  for (face_id = 0; face_id < FRAME_FACE_CACHE (f)->used; face_id++)
+    {
+      struct face *this_face = FACE_FROM_ID (f, face_id);
+
+      if (this_face != face && this_face->font)
+	{
+	  Lisp_Object font_object;
+	  XSETFONT (font_object, this_face->font);
+	  if (font_has_char (f, font_object, c))
+	    return this_face->id;
+	}
     }
-#endif
 
   eassert (fontset_id_valid_p (face->fontset));
   fontset = FONTSET_FROM_ID (face->fontset);