Improve caching of rasterized COLR glyphs in gfxFont::ColorGlyphCache
Categories
(Core :: Graphics, task)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox163 | --- | fixed |
People
(Reporter: canova, Assigned: canova)
References
(Blocks 1 open bug)
Details
Attachments
(3 external files)
I was looking at the experimental Speedometer ChatRoom-React workload, and noticed some very big speedometer wins related to how we handle our glyph cache. It looks like gfxFont::ColorGlyphCache caches rasterized COLR glyphs per gfxFont, but it is thrown away far more often than needed.
Here's an example profile from ChatRoom-React speedometer workload on Windows: https://share.firefox.dev/4AO1QZJ
gfxFont::RenderColorGlyph is 5.0% of the subtest (2053 of 40984 samples).
96% of that is COLRFonts::PaintGlyphGraph, i.e. rasterizing the glyph again rather than drawing a cached surface.
PaintGlyphGraph is 49% of PresShell::PaintInternal: https://share.firefox.dev/4CdEKgJ
Looks like there are 2 causes:
- Palette pointer changes
- The cache also clears when it sees a different
FontPalettepointer.gfxFontEntry::GetPalettecreates a newFontPalettewhenever the font entry'sPaletteCacheis reset for a differentFontPaletteValueSet(like a new document) or evicts an entry, so the pointer changes while the colors are still ideantical.
- The cache also clears when it sees a different
- Text color changes
ColorGlyphCache::SetColorsclears the whole cache when the current color differs from the last call, although only glyphs that use palette index 0xFFFF depend on it. Pages that show color glyphs in several text colors clear it constantly. Especially this is the case in ChatRoom-React workload that has colored usernames. In that workloadCOLRFonts::PaintGlyphGraphtook 69% of main-thread paint time.
I think we can do 2 fixes for these 2 issues, but they are subject to change if we think there is a better solution:
- Compare palette colors instead of the palette pointer.
- Key cache entries by
glyph ID, current colorinstead of clearing on color change, and bound the cache size since color changes no longer empty it.
I'm not 100% sure about the first one because of the possible increase in memory due to more cache being kept around. But I think this still has a huge win in emoji heavy pages like chat apps. Happy to get feedback on these!
Comment 1•5 days ago
|
||
Thanks for looking into this, it does sound like we can make this significantly better.
The second suggestion sounds fine, and should catch common cases like emoji used with differently-colored user names.
I'm a bit less sure about the first, both because of possible memory usage and also because comparing all the colors could be quite expensive. I've seen fonts that have close to 64K palette entries. I'd be curious how often the cache is getting reset for FontPalette changes, and whether there are other things we could do to mitigate this if it's a common problem.
| Assignee | ||
Comment 2•5 days ago
|
||
Thanks for looking into it quickly :jfkthame!
I'd be curious how often the cache is getting reset for FontPalette changes,
As far as I can tell, this is happening on every new document that paints with the font, which is on every iteration on speedometer (since every iteration is a new pageload in an iframe). But this doesn't seem terrible to me. I just looked at the profile that I have again and most of the time seems to be caused by number 2.. So, 1 might be an over optimization after all
I pushed some speedometer-experimental comparison runs to compare with and without the palette change, I'll let you know how much of a difference it makes when I have the numbers (unfortunately the CI seems to be a bit slow with these lately, so probably tomorrow).
comparing all the colors could be quite expensive
I was thinking that the comparison would run when the pointer changes, not per glyph. And a pointer change means the palette was just built in COLRFonts::CreateColorPalette, which already copies every color, so it is one more pass over an array we just filled. I agree that is still not great for a 64K-entry palette if two documents keep alternating.
But yeah, overall there might be a better fix to the palette part. Happy to drop that one, especially if it doesn't improve the benchmark at all.
Comment 3•5 days ago
|
||
(In reply to Nazım Can Altınova [:canova] from comment #2)
I was thinking that the comparison would run when the pointer changes, not per glyph. And a pointer change means the palette was just built in
COLRFonts::CreateColorPalette, which already copies every color, so it is one more pass over an array we just filled. I agree that is still not great for a 64K-entry palette if two documents keep alternating.
Maybe we could compute a checksum or hash of the colors when creating the palette; then we'd only need to actually compare them all if the hash matches.
But let's see whether #1 really looks like much of an issue. I suspect that in practice #2 is the big win here.
| Assignee | ||
Comment 4•4 days ago
•
|
||
Ah yeah your suspicions were right. Looked at the comparison profiles that I captured, and I can now say that the first item which is "Palette pointer changes" has no perf impact on speedometer. It looks like the real win is coming from the "Text color changes" one. I'll just drop that change, thanks!
For the record, I'm getting around ~12.4% improvement on the ChatRoom-React sp-exp subtest for the text color changes stuff, which is from going from 76.1ms to 66.7ms. It looks super promising!
| Assignee | ||
Comment 5•4 days ago
|
||
ColorGlyphCache was cleared whenever the text color changed, although only
glyphs that use the foreground palette index (0xffff) depend on it. A page
with color glyphs in several text colors kept throwing the cache away.
Key those glyphs by color and leave the rest with one entry. The paint
functions now report whether the text color was used.
Color changes no longer empty the cache, so cap it at 512 entries per
generation. A full generation is kept as the old one and hits move entries
back, so going slightly over the limit does not re-rasterize everything.
| Assignee | ||
Comment 6•4 days ago
|
||
An entry is a few kilobytes at text sizes and over a megabyte at 256px, so
512 entries could mean anything from a few megabytes to about a gigabyte
per font.
Limit each generation to 16 MB instead. A surface larger than that is not
cached.
| Assignee | ||
Comment 7•4 days ago
|
||
CAhem.ttf has no glyph that uses the text color, and no transform between a
PaintGlyph and its pattern, which is what emoji fonts do. COLRv1Paint.ttf
has both; colrv1-paint-generate.py generates it.
colrv1-fg-color and colrv1-palette-cache check that two text colors, or two
palettes, in one document do not share cached glyphs. They compare with !=
against a single-color or single-palette document because the cache can
outlive a reftest page.
The tests are in layout/reftests/font-face next to the existing COLRv1
reftests.
| Assignee | ||
Comment 8•4 days ago
|
||
:jfkthame
I just published my patches that I could measure positive speedometer impact. I have 2 commits in here (excluding the test commit). The first one is keying the color glyph cache by color instead of clearing it. And the second one adds a byte budget/limit to the cache. We didn't have a byte budget before, so technically the cache could grow a lot if the text size is large. So I'm adding a byte limit there instead. I wasn't sure how likely it is to have that large caches, so I wasn't sure about squashing it to the main commit. Please let me know about your preferences!
Comment 9•4 days ago
|
||
I think the byte limit is a good idea, given how large glyph bitmaps could become; it's basically independent of the main patch to improve currentColor handling, so makes sense to land as a separate commit, I think. (You could even spin off a separate bug for it, but I don't really mind if it gets included here; this bug can become a slightly more general "improve COLR-glyph caching" that covers both issues.)
| Assignee | ||
Comment 10•2 days ago
|
||
Thanks, sounds good to me. I'm retitling the bug to cover both of the patches.
The palette-pointer part from comment 0 was dropped (see comment 4).
The bug now covers:
- Keying the color glyph cache by text color instead of clearing it when the color changes (the Speedometer ChatRoom-React win).
- Limiting the cache by bytes instead of entries, since it previously had no size limit and large glyph sizes could make it grow a lot.
- A COLRv1 test font and reftests for both.
| Assignee | ||
Updated•1 day ago
|
Comment 11•1 day ago
|
||
Comment 12•1 day ago
|
||
| bugherder | ||
https://hg.mozilla.org/mozilla-central/rev/608febaeb773
https://hg.mozilla.org/mozilla-central/rev/bbb8fafefea0
https://hg.mozilla.org/mozilla-central/rev/81e0f17491ce
Description
•