wpf: Performance degradation when using Spell Checker
- .NET Framework 4.8
- Windows version: 2004 (OS Build 19041.450) (yes, it includes KB4562899)
- Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
- Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc…)? No
Problem description:
Application freezes for a few seconds when typing text into a TextBox with SpellChecker enabled.
For example, when I do Ctrl-V with a following snippet from the clipboard:
Text includes apostrophes before and after code snippet (I work with markdown text). I took the snippet from your codebase, but it does not matter what is inside apostrophes.
Actual behavior:
After Ctrl-V application freezes for a few seconds and I cannot type further text. I also observe sporadic delays when text contains words in apostrophes. When I pause application, stacktrace points (multiple times) to the code added in 751153802c4e4d06484743c351266c9831ade85d
NdrClientCall2 Unknown ObjectStublessClient C++ _ObjectStubless@0 Unknown 589966ef() Unknown [Frames below may be incorrect and/or missing, native debugger attempting to walk managed call stack] Unknown 589966ef() Unknown [Managed to Native Transition] System.Windows.Documents.MsSpellCheckLib.Extensions.ToList Unknown System.Windows.Documents.MsSpellCheckLib.SpellChecker.ComprehensiveCheckImpl Unknown System.Windows.Documents.MsSpellCheckLib.SpellChecker.ComprehensiveCheckImplWithRetries.AnonymousMethod__0 Unknown System.Windows.Documents.MsSpellCheckLib.RetryHelper.TryExecuteFunction<System.__Canon> Unknown System.Windows.Documents.MsSpellCheckLib.SpellChecker.ComprehensiveCheckImplWithRetries Unknown System.Windows.Documents.WinRTSpellerInteropExtensions.ComprehensiveGetTokens Unknown System.Windows.Documents.WinRTSpellerInterop.SpellerSentence.Segments.get Unknown System.Windows.Documents.WinRTSpellerInterop.EnumTextSegments Unknown System.Windows.Documents.Speller.SearchForWordBreaks Unknown System.Windows.Documents.Speller.ExpandToWordBreakAndContext Unknown System.Windows.Documents.Speller.ScanRange Unknown System.Windows.Documents.Speller.OnIdle Unknown
I’m not sure if it makes many different COM calls or there is a single COM call inside OnIdle which takes too long. I’m not sure if it is somehow caused by my environment. Please let me know if you need extra details. Any workaround is appreciated.
Expected behavior:
Text typing does not have delays.
Minimal repro:
It 100% reproduces on a WPF sample application in Visual Studio 2017/2019.
I can attach a full example, but I think this XAML is enough because the rest is auto-generated code.
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <TextBox x:Name="textBox" TextWrapping="Wrap" Text="TextBox" AcceptsReturn="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" SpellCheck.IsEnabled="True"/> </Grid> </Window>
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 9
- Comments: 33 (5 by maintainers)
Commits related to this issue
- Use a trick in app.config to get rid of WPF spell checking performance issue Applied a trick from https://github.com/dotnet/wpf/issues/3350#issuecomment-702925249 Closes #323 — committed to denis-adamchuk/mrHelper by denis-adamchuk 4 years ago
- Applied recommended patch from dotnet/wpf#3350 (comment) to help mitigate the performance impact to spell checking and message composition for WIndows 10 1903+ on .NET Framework updates post Sept. 202... — committed to alexdillon/GroupMeClient by alexdillon 4 years ago
We spoke with Microsoft, and they gave us an undocumented switch to set in app.config:
It seems to make the problem go away for us. I’d be interested to know what it shows in @JesikaDG’s profiling.
I’ll try to get it into the next .NET 5 servicing update. It may slip to the one after that, if the deadline is too close.
Thanks for the reminder. I hadn’t realized this was blocking .NET 5.
I hate to be the bearer of bad news but it seems that the slowness of the WPF Richtextbox is back. The 2021-03 Cumulative Update for Windows 10 - KB5001567 resolved the issue (March 2021) but now the slowness is back. It doesn’t matter if the spell check is on or off. Did the latest Cumulative Update break this? The WPF application I’ve written is targeting the .NET 4.6 framework.
@garzooma This configuration trick works like a charm for me, spell checking is no longer slow. Thank you.
Fix ported to 5.0.10, 6.0.
If you don’t want to mess around with a config file, add this code after InitializeComponent:
AppContext.SetSwitch(@"Switch.System.Windows.Controls.DoNotAugmentWordBreakingUsingSpeller", true);
@thorwm-zz - if you’re seeing slowness with the switch set to True, it’s a different bug. I’m not seeing it. Please open a new issue, and include a repro.
@RevitArkitek - I’ve tried it both ways, with and without, and no improvement:
<runtime> <AppContextSwitchOverrides value="Switch.System.Windows.Controls.DoNotAugmentWordBreakingUsingSpeller=true"/> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Google.Apis.Auth" publicKeyToken="4b01fa6e34db77ab" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.48.0.0" newVersion="1.48.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
@thorwm-zz Just to confirm, are you using the DoNotAugmentWordBreakingUsingSpeller switch?
@SamBent @vatsan-madhavan @blakepell Thank you all for quick responses and for being very service minded.