Last Comment Bug 385263 - [pango] we call FT_Open_Face twice per font
: [pango] we call FT_Open_Face twice per font
Status: RESOLVED FIXED
: footprint, perf
Product: Core
Classification: Components
Component: Graphics (show other bugs)
: Trunk
: x86 Linux
: P2 normal with 5 votes (vote)
: mozilla1.9.1b1
Assigned To: Karl Tomlinson (:karlt)
:
Mentors:
Depends on: 453200 454718 454720 454730 454735 454743 454951 458612 462798
Blocks: 279032 397860 400265 403618 449356 455441
  Show dependency treegraph
 
Reported: 2007-06-20 17:34 PDT by David Baron :dbaron: ⌚️UTC+2 (mostly busy through August 4; review requests must explain patch)
Modified: 2008-11-02 18:23 PST (History)
14 users (show)
vladimir: wanted1.9.1+
vladimir: blocking1.9-
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments
a PangoFcFont using tree cairo (85.99 KB, patch)
2008-08-24 23:16 PDT, Karl Tomlinson (:karlt)
no flags Details | Diff | Splinter Review
a PangoFcFont using tree cairo v0.5 (93.18 KB, patch)
2008-09-04 23:05 PDT, Karl Tomlinson (:karlt)
no flags Details | Diff | Splinter Review
a PangoFcFont using tree cairo and dependencies (114.45 KB, patch)
2008-09-09 20:56 PDT, Karl Tomlinson (:karlt)
no flags Details | Diff | Splinter Review
a PangoFcFont using tree cairo v1.2 (87.53 KB, patch)
2008-09-11 20:40 PDT, Karl Tomlinson (:karlt)
no flags Details | Diff | Splinter Review
a PangoFcFont using tree cairo v1.3 (86.69 KB, patch)
2008-09-15 23:42 PDT, Karl Tomlinson (:karlt)
no flags Details | Diff | Splinter Review
a PangoFcFont using tree cairo v1.4 (86.41 KB, patch)
2008-09-16 20:41 PDT, Karl Tomlinson (:karlt)
roc: review+
Details | Diff | Splinter Review
a PangoFcFont using tree cairo v1.5 (87.31 KB, patch)
2008-09-17 17:46 PDT, Karl Tomlinson (:karlt)
no flags Details | Diff | Splinter Review
make line heights integer (5.97 KB, patch)
2008-09-26 04:36 PDT, Karl Tomlinson (:karlt)
no flags Details | Diff | Splinter Review
make line heights integer v2 (6.04 KB, patch)
2008-09-26 05:02 PDT, Karl Tomlinson (:karlt)
roc: review+
Details | Diff | Splinter Review

Description David Baron :dbaron: ⌚️UTC+2 (mostly busy through August 4; review requests must explain patch) 2007-06-20 17:34:39 PDT
We call FT_Open_Face twice per font that we load.  (We might even rasterize it twice -- I remember debugging a rounding problem once where the Xft debugging code was showing me correct rasterization but we were drawing incorrectly.)

The first call is here:

#0  FT_Open_Face (library=0xa194708, args=0xbf87af34, face_index=0, aface=0xa96b79c) at /usr/src/debug/freetype-2.3.4/src/base/ftobjs.c:1653
#1  0x00c8f9d8 in FT_New_Face (library=0xa194708, pathname=0xbf87af34 "\004", face_index=0, aface=0xa96b79c) at /usr/src/debug/freetype-2.3.4/src/base/ftobjs.c:1077
#2  0x060557ac in _XftLockFile (f=0xa96b770) at xftfreetype.c:157
#3  0x06055d21 in XftFontOpenInfo (dpy=0x9cd6c20, pattern=0xa912588, fi=0xbf87b028) at xftfreetype.c:800
#4  0x0605629a in XftFontOpenPattern (dpy=0x9cd6c20, pattern=0xa912588) at xftfreetype.c:1015
#5  0x009e10da in xft_font_get_font (font=<value optimized out>) at pangoxft-font.c:397
#6  0x024e749e in gfxPangoFont::RealizeXftFont (this=0xa876aa0, force=0) at /builds/trunk/mozilla/gfx/thebes/src/gfxPangoFonts.cpp:438
#7  0x024eca6a in gfxPangoFont::GetXftFont (this=0xa876aa0) at ../../../dist/include/thebes/gfxPangoFonts.h:79
#8  0x024e784c in gfxPangoFont::GetMetrics (this=0xa876aa0) at /builds/trunk/mozilla/gfx/thebes/src/gfxPangoFonts.cpp:533

The second call is here:

#0  FT_Open_Face (library=0xa641450, args=0xbf87a344, face_index=0, aface=0xbf87a398) at /usr/src/debug/freetype-2.3.4/src/base/ftobjs.c:1653
#1  0x00c8f9d8 in FT_New_Face (library=0xa641450, pathname=0xbf87a344 "\004", face_index=0, aface=0xbf87a398) at /usr/src/debug/freetype-2.3.4/src/base/ftobjs.c:1077
#2  0x02526e8e in _cairo_ft_unscaled_font_lock_face (unscaled=0xa99c060) at /builds/trunk/mozilla/gfx/cairo/cairo/src/cairo-ft-font.c:553
#3  0x02527638 in _cairo_ft_scaled_font_create (unscaled=0xa641450, font_face=0xbf87a344, font_matrix=0xbf87a398, ctm=0xbf87a6e8, options=0xa99c2c8, ft_options=
      {base = {antialias = CAIRO_ANTIALIAS_DEFAULT, subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT, hint_style = CAIRO_HINT_STYLE_FULL, hint_metrics = CAIRO_HINT_METRICS_DEFAULT}, load_flags = 0, extra_flags = 0}) at /builds/trunk/mozilla/gfx/cairo/cairo/src/cairo-ft-font.c:1449
#4  0x0252792e in _cairo_ft_font_face_scaled_font_create (abstract_face=0xa890cb8, font_matrix=0xbf87a650, ctm=0xbf87a6e8, options=0xa99c2c8, scaled_font=0xbf87a608)
    at /builds/trunk/mozilla/gfx/cairo/cairo/src/cairo-ft-font.c:2185
#5  0x0250732e in *INT__moz_cairo_scaled_font_create (font_face=0xa890cb8, font_matrix=0xbf87a650, ctm=0xbf87a6e8, options=0xa99c2c8)
    at /builds/trunk/mozilla/gfx/cairo/cairo/src/cairo-scaled-font.c:514
#6  0x024e63b9 in CreateScaledFont (aCR=0xa90d388, aCTM=0xbf87a6e8, aPangoFont=<value optimized out>) at /builds/trunk/mozilla/gfx/thebes/src/gfxPangoFonts.cpp:897
#7  0x024e70ca in gfxPangoFont::SetupCairoFont (this=0xa876aa0, aCR=0xa90d388) at /builds/trunk/mozilla/gfx/thebes/src/gfxPangoFonts.cpp:923

I'm not sure if this means we're allocating structures over again, or if we're getting the same ones back -- but I am seeing about 32K of allocation for one font within FT_Open_Face (not sure if it's all within one call, or separated between two).
Comment 1 Karl Tomlinson (:karlt) 2007-08-05 23:25:43 PDT
This is a consequence of using Xft for measuring/glyph-selection and cairo for rendering, each of which have their own font (face) caches.

This can be resolved for distributions that --enable-system-cairo by switching from pangoxft to pangocairo.  (But with moz-cairo, moz-cairo and pango's cairo will still have separate caches.)
Comment 2 Karl Tomlinson (:karlt) 2008-02-07 23:56:18 PST
We are now not usually using Xft, but instead both system cairo (through Pango) and tree cairo.  Distributions using --enable-system-cairo and Pango-1.18 will be able to make a big gain (half the time spent opening fonts and half the cairo font cache size) by using pango_cairo_font_get_scaled_font() in CreateScaledFont.

We could make a similarly large gain in binary builds with tree cairo by implementing our own PangoFontMap.  With our own PangoFontMap providing an implementation PangoFcFont using tree cairo, we could still use pango_itemize and pango_shape.

Such an implementation might resemble the PangoCairoFcFontMap in Pango but would be linked against tree cairo (or system cairo if selected by distributions).  The implementation of PangoFcFont might be a wrapper around a gfxFont.
The gfxFontStyle could be passed through pango_itemize to the PangoFontMap by attaching it to the PangoContext.

As well as gains in speed and memory from using half as many fonts, our own PangoFontMap would give us greater control over fontconfig pattern caches (we could have more than 64 entries for the itemizing path) and choice of fonts for characters in a language different from that specified in the document (e.g. Latin characters in a Japanese document), would enable us to implement font-size-adjust and font-weight:bolder appropriately for more than just the first-specified font, and could enable us to disable font fallback for
PUA code points.
Comment 3 Vladimir Vukicevic [:vlad] [:vladv] 2008-02-14 12:48:24 PST
This sounds like a pretty complex fix; marking blocking 1.9- given the other remaining work, but please renom if you disagree.
Comment 4 Karl Tomlinson (:karlt) 2008-08-24 23:16:45 PDT
Created attachment 335322 [details] [diff] [review]
a PangoFcFont using tree cairo

This seems to work.  Need to check performance.
This doesn't change font selection at all, but includes the font metrics changes from bug 403618.
Comment 5 Karl Tomlinson (:karlt) 2008-08-25 18:35:41 PDT
          reference w/patch change
Ts (ms)      1567    1579    <+1% (less than margin of error)
Tp (ms)       462     463     ~0%
Tp_RSS (MB)   127     115    -10%
Comment 6 Stuart Parmenter 2008-08-25 21:52:38 PDT
10% is nice.
Comment 7 Karl Tomlinson (:karlt) 2008-09-04 00:59:40 PDT
We should end up with a net gain in performance but we lose a little of the
gain (~1% of cpu cycles in the Firefox process) because we rely on cairo to
cache glyph extents whereas the PangoCairoFont added it's own cache on top of
the cairo cache.

The cairo_scaled_font glyph cache and the PangoCairoFont glyph_extents_cache
are both the same size.  The effect of having two caches would be roughly
equivalent to doubling the size of the cairo cache (but differing eviction
policies are involved).

Most of the 1% difference is cache lookup inefficiencies (bug 453200) that we
can easily recover (and gain more).  The remaining fraction of the 1% is due
to having to load glyphs ~10% more often.

I think the effect of the second cache is small enough that there are likely
more significant performance issues that we could focus on.  (e.g. bug 453200
will gain us much more than we lose.)  And if this is worth working on then,
rather than adding another level of caching, a better solution would be to
tune cairo's caches.
Comment 8 Robert O'Callahan (:roc) (Exited; email my personal email if necessary) 2008-09-04 01:18:08 PDT
We do our own glyph extents caching in gfxGlyphExtents --- I'm surprised these other caches still matter.
Comment 9 Karl Tomlinson (:karlt) 2008-09-04 05:21:54 PDT
I was using "extents" in the Pango and cairo sense where extents includes the x-advance width (logical extents in Pango).  Advance width is required at shaping, and cairo_scaled_font_glyph_extents is used for this.  (The Pango APIs allow shapers to ask for inked (tight) extents also if they want them, but I think we can expect this to be rare.)

The glyph needs to be loaded to get the advance width, so inked extents become available at the same time with Freetype and cairo APIs.  Though caching only the advance width would mean a much smaller cache.

I guess gfxGlyphExtents can be modified to store advance widths for all (or at least more) glyphs.  AppUnits are a bit of a complication.  Do we do subpixel positioning on Mac?  If not we can just store integer pixels (one set for all AppUnitsPerGfxUnit factors).  (This reminds me of gfxFont's user/device unit confusion/ignorance.)  As the tight glyph extents are doubles there is no need to recalculate those for each factor either.
Comment 10 Robert O'Callahan (:roc) (Exited; email my personal email if necessary) 2008-09-04 05:35:14 PDT
I see. I wouldn't bother changing anything here, then, just pretend I never said anything.

We do do subpixel positioning on Mac. I thought we did it with GTK too.
Comment 11 Karl Tomlinson (:karlt) 2008-09-04 05:41:42 PDT
Freetype/cairo does subpixel antialiasing (sometimes/often), but not positioning.

For most Western language texts, cairo's 256 glyph per font cache should be enough.  I assume the issue becomes measurable in talos because the same scaled font is used for multiple different scripts.  Maybe it is significant in scripts with many characters too, but then i'd expect aquiring the glyph surfaces for painting to be the bottleneck.
Comment 12 Karl Tomlinson (:karlt) 2008-09-04 23:05:14 PDT
Created attachment 336982 [details] [diff] [review]
a PangoFcFont using tree cairo v0.5

I'm happy with the performance of this one.

The reason we weren't getting the performance I expected was that there were still different cairo_scaled_fonts for measuring and drawing (even thought they were the same FT_Face and cairo_font_face), which meant that there were two independent glyph caches.  The changes in this version mean that we usually use same cairo_scaled_font for each phase (non-identity ctms being an exception), and so spend 36% less time in libfreetype (but on its own that represents only a saving of ~1% total time).
Comment 13 Karl Tomlinson (:karlt) 2008-09-07 15:28:17 PDT
Comparing builds with and without attachment 336982 [details] [diff] [review] on the tryserver,
both builds having attachment 336403 [details] [diff] [review]:

          reference w/336982 change
Ts (ms)      1331    1303     -2%
Tp (ms)       447     424     -5%
Tp_RSS (MB)   121     108    -11%

The Tp savings are roughly 2% from metric calculation changes (bug 403618),
1.5% from having half as many FT_Faces and cairo_scaled_fonts, and 1.5% from
moving work from system cairo to a moz cairo with a fix for bug 453200.  If
builds were compared without attachment 336403 [details] [diff] [review], attachment 336982 [details] [diff] [review] would
increase the effect of bug 453200, and the Tp savings would be roughly halved.

I need to tidy up this patch and add a bit more documentation.
Comment 14 Karl Tomlinson (:karlt) 2008-09-09 20:56:41 PDT
Created attachment 337810 [details] [diff] [review]
a PangoFcFont using tree cairo and dependencies

This is a complete patch including everything that is not yet in other bugs.
I'll break some pieces off to make review easier.
Comment 15 Karl Tomlinson (:karlt) 2008-09-11 20:40:51 PDT
Created attachment 338251 [details] [diff] [review]
a PangoFcFont using tree cairo v1.2

The basic overview of the change here is that

  gfxFontGroup has 
    gfxFont has
      cairo_scaled_font
      PangoFont has 
        different cairo_scaled_font

becomes

  gfxFontGroup has
    PangoFont has
      gfxFont has
        cairo_scaled_font

Metric changes for bug 403618 got a bit tangled up here too.

Many of the patches in dependencies will be needed for this to apply/build/run.
Comment 16 Karl Tomlinson (:karlt) 2008-09-15 23:42:54 PDT
Created attachment 338808 [details] [diff] [review]
a PangoFcFont using tree cairo v1.3

Update to correct the diff of gfxPangoFonts.h which was against the wrong version, and to remove some "#if 0"d code.
Comment 17 Robert O'Callahan (:roc) (Exited; email my personal email if necessary) 2008-09-16 01:38:45 PDT
Who wants to review this? I'm happy to do it, but maybe Stuart or Vlad should do it.
Comment 18 Karl Tomlinson (:karlt) 2008-09-16 20:41:57 PDT
Created attachment 339005 [details] [diff] [review]
a PangoFcFont using tree cairo v1.4

One more small change:  Always unref the fontmap on shutdown (not just in
debug versions).  We should be free from external influences that might cause
problems here (bug 454730 comment 3).
Comment 19 Robert O'Callahan (:roc) (Exited; email my personal email if necessary) 2008-09-17 15:43:30 PDT
Comment on attachment 339005 [details] [diff] [review]
a PangoFcFont using tree cairo v1.4

+    gfxFloat GetAdjustedSize() { if (!mBasePangoFont) GetBasePangoFont(); return mAdjustedSize; }

This would be clearer written over multiple lines.

+    PRBool   mHasMetrics;

PRPackedBool and put it at the end. Doesn't make a real difference but it's safer if someone copies your code or adds fields.

+    operator FT_Face() { return mFace; }

We don't usually have overloaded conversion operators. Just make it GetFT_Face.

+    PRUint32 GetCharExtents(char aChar, cairo_text_extents_t& aExtents);
+
+    void GetMetrics(gfxFont::Metrics& aMetrics, PRUint32& aSpaceGlyph);

Prefer to use pointers for out-params so it's obvious in the caller that they're out-params.

+            // cairo won't know which font to open without a file.
+            // (We don't create fonts from an FT_Face.)
+            NS_NOTREACHED("Fonts without a file are not supported");
+            static const char *noFile = "NO FILE";
+            file = noFile;
...
+            // cairo won't know what to do here either.
+            NS_NOTREACHED("No index in pattern");
+            index = 0;

Are there guarantees that say we won't get here? Or are we just doomed if we do?

+        // Note that the unique face in the name and the gfxFontStyle are not
+        // necessarily enough to provide a key that will describe a unique
+        // font.  Here, aPattern is a fully resolved pattern from
+        // FcFontRenderPrepare, which takes the requested pattern and the font
+        // pattern as input and can modify elements of the resulting pattern
+        // that affect rendering but are not included in the gfxFontStyle.
+        nsRefPtr<gfxPangoFontEntry> fe = new gfxPangoFontEntry(name);

So you're saying that we need a new fontentry for every face in order to provide that unique key?

+    // In some case, _cairo_ft_options_merge uses some options from the

"In some cases"

+        xScale = FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.x_scale));
+        yScale = FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.y_scale));

Can you comment on what the unit conversions are here?

+            aMetrics.xHeight = 0.56 * aMetrics.emHeight; // guess

Comment on where 0.56 came from
Comment 20 Karl Tomlinson (:karlt) 2008-09-17 16:30:51 PDT
(In reply to comment #19)
> +    operator FT_Face() { return mFace; }
> 
> We don't usually have overloaded conversion operators. Just make it GetFT_Face.

That ended up not being used, I think, so I'll just remove it for now.

> 
> +            // cairo won't know which font to open without a file.
> +            // (We don't create fonts from an FT_Face.)
> +            NS_NOTREACHED("Fonts without a file are not supported");
> +            static const char *noFile = "NO FILE";
> +            file = noFile;
> ...
> +            // cairo won't know what to do here either.
> +            NS_NOTREACHED("No index in pattern");
> +            index = 0;
> 
> Are there guarantees that say we won't get here? Or are we just doomed if we
> do?

gfx_pango_font_map_create_font guarantees this.

> 
> +        // Note that the unique face in the name and the gfxFontStyle are not
> +        // necessarily enough to provide a key that will describe a unique
> +        // font.  Here, aPattern is a fully resolved pattern from
> +        // FcFontRenderPrepare, which takes the requested pattern and the font
> +        // pattern as input and can modify elements of the resulting pattern
> +        // that affect rendering but are not included in the gfxFontStyle.
> +        nsRefPtr<gfxPangoFontEntry> fe = new gfxPangoFontEntry(name);
> 
> So you're saying that we need a new fontentry for every face in order to
> provide that unique key?

That wasn't what I was trying to say.  It's that the cairoFont contains
information (from the pattern) that is not in the name/fe and fontStyle
combination.

I'll modify the comment a bit and move it to the "new gfxFcFont" statement.

> +            aMetrics.xHeight = 0.56 * aMetrics.emHeight; // guess
> 
> Comment on where 0.56 came from

It kind of came from Windows/Atsui fonts but the math is not right.
I now see that CSS 2.1 says to use 0.5em, so I'll do that and comment why.

I'll make the other changes as suggested, thanks.
Comment 21 Karl Tomlinson (:karlt) 2008-09-17 17:46:43 PDT
Created attachment 339161 [details] [diff] [review]
a PangoFcFont using tree cairo v1.5

Addressed roc's review comments as discussed in comment 19 and 20.
Comment 22 Karl Tomlinson (:karlt) 2008-09-26 01:05:33 PDT
I landed this, but backed out due to svg reftest failures

http://tinderbox.mozilla.org/showlog.cgi?log=Firefox/1222410677.1222414459.5180.gz#err0

until I can work out why this patch caused the following <rect> not to fill the
content area of the enclosing <svg> element:

    <svg xmlns="http://www.w3.org/2000/svg" width="51" height="51"
         style="float:left; padding:0; border:1px solid blue; margin:0;">
      <rect width="100%" height="100%" fill="blue"/>
    </svg>
Comment 23 Karl Tomlinson (:karlt) 2008-09-26 04:36:04 PDT
Created attachment 340550 [details] [diff] [review]
make line heights integer

In the svg reftests, it looks like the border on <svg/> is being snapped to
pixels but the <rect/> that is filled is not.  (This is evident in the fact
that the test won't pass except at zoom = 1.)

The position of the rect depends on the line-height of the surrounding text,
so the test is implicitly testing for integer line-heights (which might be why
many of the tests don't pass on Mac).

While I assume integer line-heights are not necessary for CSS compliance, they
actually seem a good idea, because they ensure that a paragraph of lines will
be equally spaced (rather than just being snapped to pixels, some up and some
down.)
Comment 24 Karl Tomlinson (:karlt) 2008-09-26 04:38:24 PDT
Comment on attachment 340550 [details] [diff] [review]
make line heights integer

This logic is not quite right.
Comment 25 Karl Tomlinson (:karlt) 2008-09-26 05:02:51 PDT
Created attachment 340554 [details] [diff] [review]
make line heights integer v2
Comment 26 Robert O'Callahan (:roc) (Exited; email my personal email if necessary) 2008-09-26 05:42:01 PDT
Comment on attachment 340554 [details] [diff] [review]
make line heights integer v2

I guess this approach is OK.
Comment 27 David Baron :dbaron: ⌚️UTC+2 (mostly busy through August 4; review requests must explain patch) 2008-09-26 12:26:20 PDT
During the brief time that this landed, it helped memory use a good bit:
http://graphs.mozilla.org/graph.html#show=395139,395141,395170,911698,1431037&sel=1222343298,1222467457

Note You need to log in before you can comment on or make changes to this bug.