Skip to content
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

Eliminate memory copy when reading font data #6254

Merged
merged 1 commit into from
Jul 13, 2022

Conversation

bgrainger
Copy link
Contributor

Fixes #6236

Description

Using a <Resource> font in WPF currently causes a lot of allocation, GC pinning, and memory copying. This is entirely avoidable by returning a pointer to the already-loaded font data to DWrite.

Customer Impact

Without this fix, customers are forced to hook DWrite APIs and supply their own font-loading implementation; e.g., https://faithlife.codes/blog/2019/06/improving-wpf-text-display-performance/.

Regression

Not a regression.

Testing

Manual testing using a test app: https://github.com/bgrainger/EmbeddedFontPerformance

Risk

Risk is low. This fix has been prototyped in a commercial WPF application that's been shipping for 5 years (see blog post above). Since the font source is an UnmanagedMemoryStream, we can rely on its data being present in memory at least as long as we hold a reference to the stream. (And since, practically speaking, the font data is backed by the assembly that contains it being mmap'ed into the process' address space, it's going to be safe to dereference it for the lifetime of the application.)

@bgrainger bgrainger requested a review from a team as a code owner March 14, 2022 16:32
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Mar 14, 2022
@ghost ghost requested review from dipeshmsft, singhashish-wpf and SamBent March 14, 2022 16:32
@ghost ghost added the Community Contribution A label for all community Contributions label Mar 14, 2022
@bgrainger
Copy link
Contributor Author

Performance data obtained by running the test app, maximising on a 1600x1200 screen, then scrolling the textbox all the way to the bottom.

Before

image

After

image

Comparison

image

@singhashish-wpf
Copy link
Member

This PR is being considered for next community test pass.

@singhashish-wpf singhashish-wpf merged commit 75465ad into dotnet:main Jul 13, 2022
@bgrainger bgrainger deleted the fix-font-performance branch July 13, 2022 19:28
@ghost ghost locked as resolved and limited conversation to collaborators Aug 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Poor performance for <Resource> fonts
3 participants