Skip to content

Commit e86ce74

Browse files
authored
Merge pull request #9439 from h3xds1nz/speller-suggestions-perf-improv
Reduce allocations while enumerating Speller suggestions and improve performance
2 parents 1a9d0c3 + adcd286 commit e86ce74

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,36 +177,24 @@ internal ITextPointer GetNextSpellingErrorPosition(ITextPointer position, Logica
177177
// for an error range.
178178
// This method actually runs the speller on the specified text,
179179
// re-evaluating the error from scratch.
180-
internal IList GetSuggestionsForError(SpellingError error)
180+
internal List<string> GetSuggestionsForError(SpellingError error)
181181
{
182-
ITextPointer contextStart;
183-
ITextPointer contextEnd;
184-
ITextPointer contentStart;
185-
ITextPointer contentEnd;
186-
TextMap textMap;
187-
ArrayList suggestions;
188-
189-
suggestions = new ArrayList(1);
190-
191182
//
192183
// IMPORTANT!!
193184
//
194185
// This logic here must match ScanRange, or else we might not
195186
// calculate the exact same error. Keep the two methods in sync!
196187
//
197188

198-
XmlLanguage language;
199-
CultureInfo culture = GetCurrentCultureAndLanguage(error.Start, out language);
200-
if (culture == null || !_spellerInterop.CanSpellCheck(culture))
201-
{
202-
// Return an empty list.
203-
}
204-
else
189+
List<string> suggestions = new(4);
190+
CultureInfo culture = GetCurrentCultureAndLanguage(error.Start, out XmlLanguage language);
191+
192+
if (culture is not null && _spellerInterop.CanSpellCheck(culture))
205193
{
206-
ExpandToWordBreakAndContext(error.Start, LogicalDirection.Backward, language, out contentStart, out contextStart);
207-
ExpandToWordBreakAndContext(error.End, LogicalDirection.Forward, language, out contentEnd, out contextEnd);
194+
ExpandToWordBreakAndContext(error.Start, LogicalDirection.Backward, language, out ITextPointer contentStart, out ITextPointer contextStart);
195+
ExpandToWordBreakAndContext(error.End, LogicalDirection.Forward, language, out ITextPointer contentEnd, out ITextPointer contextEnd);
208196

209-
textMap = new TextMap(contextStart, contextEnd, contentStart, contentEnd);
197+
TextMap textMap = new(contextStart, contextEnd, contentStart, contentEnd);
210198

211199
SetCulture(culture);
212200

@@ -915,7 +903,7 @@ private bool ScanErrorTextSegment(SpellerInteropBase.ISpellerSegment textSegment
915903
{
916904
if (textSegment.SubSegments.Count == 0)
917905
{
918-
ArrayList suggestions = (ArrayList)data.Data;
906+
List<string> suggestions = (List<string>)data.Data;
919907
if(textSegment.Suggestions.Count > 0)
920908
{
921909
foreach(string suggestion in textSegment.Suggestions)

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/SpellerError.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,7 @@ public void IgnoreAll()
9494
/// </remarks>
9595
public IEnumerable<string> Suggestions
9696
{
97-
get
98-
{
99-
IList suggestions = _speller.GetSuggestionsForError(this);
100-
101-
for (int i=0; i<suggestions.Count; i++)
102-
{
103-
yield return (string)suggestions[i];
104-
}
105-
}
97+
get => _speller.GetSuggestionsForError(this);
10698
}
10799

108100
#endregion Public Properties

0 commit comments

Comments
 (0)