-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Optimize GlyphTypeface: Avoid create the cmap Dictionary when get Count only #11139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes the performance of getting the Count property in the FontFaceLayoutInfo class by avoiding the expensive creation of the CMap Dictionary when only the count is needed.
- Introduces conditional logic to check if the CMap is already created before accessing its Count
- Directly retrieves glyph count from FontFace when CMap is not yet initialized
- Maintains existing behavior while improving performance for count-only operations
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontFaceLayoutInfo.cs
Show resolved
Hide resolved
src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontFaceLayoutInfo.cs
Show resolved
Hide resolved
To be fair if you just want a count you can use GlyphTypeface.GlyphCount. How did you come with this, is it in some hot path? |
I might be misremembering, but doesn't CMap get created the first time you do a single text run / line anyways? Not mentioning that the generation method is slow af and should be rewritten but as miloush says, you have the prop on So I'm indeed curious about the scenario where you don't end up creating it and can get away with this. |
We just use the CharacterToGlyphMap as the Dictionary and pass it to other business code. The hot path will count the CharacterToGlyphMap Dictionary for all fonts. |
@h3xds1nz As you can see, ont of the IDictionary hot path is wpf/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontFaceLayoutInfo.cs Lines 634 to 657 in f7ebd69
So that, it is meaningful to do this. |
Description
As the code shows:
wpf/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontFaceLayoutInfo.cs
Lines 585 to 609 in f7ebd69
We can know that the cost of creating CMap is expensive. When we get the
Count
only, we do not need to create the CMap Dictionary.We can get the count from FontFace directly:
wpf/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFace.h
Line 126 in f7ebd69
Customer Impact
Performance
Regression
Testing
CI only.
Risk
Low. I do not change the behavior. The old behavior is get the count after the CMap Dictionary created, and the new behavior is get the count from FontFace directly.
Microsoft Reviewers: Open in CodeFlow