Skip to content

Conversation

@adirh3
Copy link
Contributor

@adirh3 adirh3 commented Dec 9, 2025

What does the pull request do?

Fixes a cache miss issue in SystemFontCollection that causes repeated GlyphTypeface allocations when the platform returns a typeface with a different FamilyName than requested.

What is the current behavior?

When requesting a font, the platform may return a typeface with a different FamilyName than requested. For example:

  • Request "Segoe UI Variable Text" → returns typeface with FamilyName = "Segoe UI Variable"
  • Request "Microsoft YaHei" → returns typeface with FamilyName = "Microsoft YaHei UI"

The cache stores the typeface under the actual FamilyName, but subsequent lookups use the requested name. This causes cache misses on every request, creating new GlyphTypeface instances (with underlying HarfBuzzSharp.Face native resources) repeatedly.

This causes significant memory pressure during virtualized list/panel scrolling where the same font is requested many times per second.

What is the updated/expected behavior with this PR?

Typefaces are now cached under the requested family name key, ensuring subsequent lookups hit the cache. Repeated requests return the cached instance instead of allocating new ones.

How was the solution implemented (if it's not obvious)?

The fix ensures TryAddGlyphTypeface(familyName, key, glyphTypeface) is called with the requested familyName in both code paths:

  1. After base.TryGetGlyphTypeface succeeds
  2. After _platformImpl.TryCreateGlyphTypeface succeeds

Checklist

Breaking changes

None

Obsoletions / Deprecations

None

…ers from requested

When requesting a font, the platform may return a typeface with a different
FamilyName than requested (e.g., 'Segoe UI Variable Text' returns FamilyName=
'Segoe UI Variable'). Previously, the cache stored under the actual FamilyName
but lookups used the requested name, causing cache misses and memory leaks.

This fix caches typefaces under the requested key, ensuring subsequent lookups
hit the cache correctly.

Added cross-platform unit test that verifies no new typefaces are created on
repeated requests when the returned FamilyName differs from requested.
@Gillibald
Copy link
Contributor

Have you tried #19852 ?

@adirh3
Copy link
Contributor Author

adirh3 commented Dec 9, 2025

Have you tried #19852 ?

I haven't tried, just briefly looked at the SystemFontCollection changes it doesn't seem to cache on both requested and actual font family?
I will have a deeper look tomorrow, but I guess you plan to merge this for 12.x release, and this problem seems to happen on 11.3 as well.
For example, scrolling in Fluent Search with Segoe UI Variable Text font causing RAM spike to 1gb+ very easily, with thousands of glyphs created.

@Gillibald
Copy link
Contributor

This targets 11.3 #20207

@adirh3
Copy link
Contributor Author

adirh3 commented Dec 9, 2025

This targets 11.3 #20207

This one seems to fix the issue.

I might be missing something since #19852 is a big PR, but from what I can see it still has the bug: it caches the font under what the system returns ("Segoe UI Variable") rather than what was requested ("Segoe UI Variable Text"), so the next lookup misses the cache and creates a new allocation.

I haven't tested #19852 directly, so if you can confirm it actually fixes this we can close this. Otherwise, we could keep this open if you'd like a fix in master until #19852 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants