Wednesday, December 10, 2008

Noticeably faster font selection with fontconfig for Firefox 3.1 Beta 2

Firefox 3.1 Beta 2 includes several important performance improvements, but one improvement that is specific to platforms using GTK+ and fontconfig (such as Linux) can be noticed while zooming pages with a large number of fonts.

The difference is very clear if you run Firefox versions 3.0 and 3.1 Beta 2 concurrently and open http://wikipedia.org/ in each. If the number of fonts on your system is similar to the number on my system, you can type Ctrl-+ in version 3.0, then have time to switch focus to type Ctrl-+ in version 3.1 Beta 2, and 3.1 Beta 2 will be displaying the page larger before version 3.0.

The effect is very noticeable on Wikipedia due to the large number of fonts for the different languages, but the effect is also visible on pages that display text in a number of different sizes and styles.

There are several improvements contributing here, but one of the recent improvements comes from a change in the way fontconfig is used to perform font selection.

Typically applications using fontconfig construct an FcPattern *pattern containing a set of requested properties for the font. Next, user preferences and defaults are applied to the pattern using FcConfigSubstitute, and then either FcFontSetMatch or FcFontSetSort is used to find the font on the system that most closely matches the requested properties.

When all default font families have been included in the pattern, there are around 80 family names (as well as other properties), each of which is compared against names for every font on the system. For a system with 500 fonts (which is not unusual) that is about 40000 (case-insensitive) string comparisons.

Fontconfig uses a concept of strong and weak "bindings" for family names, which is important in the selection of default fonts for each language. There are currently no direct APIs to get the binding type of a family name from the pattern, so applications must use fontconfig match or sort functions if they are to pick up default fonts as intended.

However, you can imagine the speed improvement obtained by filtering the set of family names in the pattern to those that exist on the system, and filtering the set of all fonts to those that are in a requested or default family, before passing these sets to the fontconfig's match or sort functions.

1 comment:

Zac said...

Great stuff :-)