From bd00e2b93951e9e7fa6e5abd990d2cd77d7a83bd Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Mon, 1 Jul 2024 05:19:12 +0300 Subject: [PATCH] perf: Optimize CountWordInText (#10050) * use SearchValues for special chars lookup on NET 8 * only allocate delimiter chars once * ensure no closure allocations with static lambdas --- src/Docfx.Build/Conceptual/WordCounter.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Docfx.Build/Conceptual/WordCounter.cs b/src/Docfx.Build/Conceptual/WordCounter.cs index 1a183713769..4efb8208626 100644 --- a/src/Docfx.Build/Conceptual/WordCounter.cs +++ b/src/Docfx.Build/Conceptual/WordCounter.cs @@ -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 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); @@ -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))); } }