Skip to content

Commit

Permalink
perf: Optimize CountWordInText (#10050)
Browse files Browse the repository at this point in the history
* use SearchValues for special chars lookup on NET 8
* only allocate delimiter chars once
* ensure no closure allocations with static lambdas
  • Loading branch information
lahma committed Jul 1, 2024
1 parent a6c4b21 commit bd00e2b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Docfx.Build/Conceptual/WordCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ internal static class WordCounter
{
private static readonly string[] ExcludeNodeXPaths = ["//title"];

#if NET8_0_OR_GREATER
private static readonly System.Buffers.SearchValues<char> SpecialChars = System.Buffers.SearchValues.Create(".?!;:,()[]");
#else
private static readonly string SpecialChars = ".?!;:,()[]";
#endif

private static readonly char[] DelimiterChars = [' ', '\t', '\n'];

public static long CountWord(string html)
{
ArgumentNullException.ThrowIfNull(html);
Expand Down Expand Up @@ -50,10 +58,7 @@ private static int CountWordInText(string text)
return 0;
}

string specialChars = ".?!;:,()[]";
char[] delimiterChars = [' ', '\t', '\n'];

string[] wordList = text.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
return wordList.Count(s => !s.Trim().All(specialChars.Contains));
string[] wordList = text.Split(DelimiterChars, StringSplitOptions.RemoveEmptyEntries);
return wordList.Count(static s => !s.Trim().All(static c => SpecialChars.Contains(c)));
}
}

0 comments on commit bd00e2b

Please sign in to comment.