Repository navigation
Description
- .NET Version: 6.0
- Windows version: Windows 10 21H2 (19044.1526)
- Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
Problem description:
When using a font that is embedded as a <Resource> (see fonts as resource items), the performance of rendering text can be extremely poor.
This was first noted in a production .NET 4.8 WPF Application; a detailed writeup of the problem and a solution that was found is at https://faithlife.codes/blog/2019/06/improving-wpf-text-display-performance/.
Actual behavior:
dotTrace shows that very large amounts of time are spent in MS.Internal.Text.TextInterface.FontFileStream.ReadFileFragment. Additionally, large amounts of memory are allocated and the program spends a lot of time in GC.
Expected behavior:
Rendering a <Resource> font is extremely efficient because the resource is already mmap'ed into memory and returning its data could be as simple as a pointer addition.
Minimal repro:
An example repro is at https://github.com/bgrainger/EmbeddedFontPerformance. Clone the repo, build the code, and scroll the text box. You'll notice that it can take several seconds to draw the next screen of text (depending on how large the window is).
The following text complements that repo and describes how it reproduces the problem:
This reproduces most easily with the Noto Sans CJK font (perhaps because of the large number of glyphs that have to be read?).
Embed the font:
<ItemGroup>
<Resource Include="NotoSansCJKtc-Regular.otf" />
</ItemGroup>Then create a text box that references the font and contains a large amount of Chinese text:
<Grid>
<ScrollViewer>
<TextBlock FontFamily="./#Noto Sans CJK TC" TextWrapping="Wrap">
<!-- place Chinese text here -->
</TextBlock>
</ScrollViewer>
</Grid>Run the app and scroll the ScrollViewer.

Activity
bgrainger commentedon Mar 9, 2022
The following fix (to
FontFileStream.cpp) eliminates the problem on my system.(It's not written as well as it could be; just a very simple POC fix.)
If this seems like the appropriate direction to take to fix this bug, I'd be happy to open a PR.
lindexi commentedon Mar 10, 2022
@bgrainger See #5305 , and @ThomasGoulet73 is working on this.
bgrainger commentedon Mar 10, 2022
I saw PR #6171 but it didn't (yet?) touch this file, so I didn't know if it would obsolete my proposed fix.
If all the C++/CLI is getting replaced with C#, then yes, fixing the C++/CLI may be pointless, unless:
lindexi commentedon Mar 10, 2022
@bgrainger The bad news is I think we hard to replace it with C# version until 8.0
bgrainger commentedon Mar 10, 2022
If a simple C++/CLI fix can make it into 7.0, I'd like to make that happen; will wait for WPF team triage.
ThomasGoulet73 commentedon Mar 10, 2022
My PR #6171 is the start of the migration of DirectWriteForwarder to C#. The migration is a direct port with minimal changes to the C++ code when porting. That means that the changes suggested in @bgrainger's patch could be either applied before or after the migration to C#.
That being said, the changes looks like a good idea but there is a risk with those changes, due to the fact that it would not copy in a new array.
ReadFileFragmentis invoked fromPresentationNativewhich is not open source so it's hard to evaluate the risk for external contributors.bgrainger commentedon Mar 10, 2022
As long as the array isn't deallocated before
ReleaseFileFragmentis called, there is no risk. AFAIK, theUnmanagedMemoryStreamreturned for resources is a pointer to the resource within the DLL that's loaded into the process' address space, so this can't happen.As per the blog post in the OP, a "hack" that implements this solution has been shipping (with no problems) in Logos for years, so I feel confident in this solution.
kant2002 commentedon Mar 13, 2022
@bgrainger send PR to C++ part. I think that way we can hedge if DWrite rewrite in C# get stalled indefinitely. Also if you can measure improvements somehow that helps advocate for the fix
Eliminate memory copy when reading font data. Fixes dotnet#6236
Eliminate memory copy when reading font data. Fixes dotnet#6236
Eliminate memory copy when reading font data. Fixes #6236 (#6254)
Eliminate memory copy when reading font data. Fixes dotnet#6236 (dotn…