Subject: Re: bug#71454: 30.0.50; Performance issues with font selection

From: Eli Zaretskii <[email-masked]>
Date: Fri, 27 Sep 2024 09:32:50 +0300

> From: Kai Ma <[email-masked]>
> Date: Fri, 27 Sep 2024 00:42:04 +0200
> 
> I got back to this problem today and have some initial ideas.
> 
> I did some profiling and the profiler clearly shows that most CPU time was in macfont_list and CTFontDescriptorCreateMatchingFontDescriptors. (screenshot attached below) So yes, it’s a Mac-only problem.
> 
> macfont_list will call CTFontDescriptorCreateMatchingFontDescriptors for n times, where n is the number of installed fonts. It seems CTFontDescriptorCreateMatchingFontDescriptors is very expensive, and we should minimize the use of it.
> 
> I did a quick proof-of-concept patch (attached) that removes the outer loop (utilizing CTFontDescriptorCreateMatchingFontDescriptors itself to search for fonts). And now I no longer experience long delays. It’s not complete as I haven’t adapted & incorporated mac_font_create_preferred_family_for_attributes yet.
> 
> I don’t see noticeable font differences after the change on the files I’m working on. I would like to gather some feedback from the more experienced on whether this is a promising solution or not. If it is, I will refine it into a formal patch.

Thanks.  I'm adding a few people in the hope that they could comment
on the patch, or maybe try it and provide feedback.

> > I have currently (length (font-family-list)) = 582 font families
> > installed. And whenever I input some ununsual characters, Emacs will
> > freeze for seconds until I am able to do anything else.  Worse, the
> > freeze delay for each character will add up.  And whenver the face
> > changes (including hl-line-mode), or I switched to another buffer for
> > some time, there will be a delay again.

This part, about _any_ unusual characters causing a significant delay,
seems to imply that the default fontset is not set up correctly on
your system (or maybe in general on macOS), or perhaps that the way
the fontsets are used on macOS is very different from other systems.
For starters, are the fonts that Emacs selects for the characters you
used for testing different from the fonts defined by fontset-default
for those characters?  If so, what happens if you modify
fontset-default (using set-fontset-font) to specify for these
characters the same fonts as what Emacs selects with the default value
of fontset-default? does font selection become much faster (it
should)?

If specifying the precise font in the fontset doesn't speed up font
selection, then something is very wrong with how fontsets are used on
macOS.  Emacs comes with the default value of fontset-default that is
already configured for many characters, and the ones you show should
be included.  So I'm puzzled why you experience such long delays when
some of those characters are used.

It could also be the matter of selecting the default face's font.  For
best results, it should be a font that covers many popular scripts, at
the very least Latin, Cyrillic, and Greek.  Judging by the "unusual"
characters you show, it sounds like Latin characters are not well
covered by your default face's font?  If so, perhaps choosing a font
with better coverage will make the situation better for you?