From ae1eae22172dbb773348d4196f70c581bcc55585 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 21 Feb 2024 12:59:26 +1100 Subject: [PATCH 1/2] optimize EnglishArticle.AppendArticlePrefix --- src/Benchmarks/Benchmarks.csproj | 1 + src/Benchmarks/EnglishArticleBenchmarks.cs | 7 +++++++ src/Benchmarks/Program.cs | 3 ++- src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs | 1 + src/Humanizer/ArticlePrefixSort.cs | 9 +++++---- 5 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 src/Benchmarks/EnglishArticleBenchmarks.cs diff --git a/src/Benchmarks/Benchmarks.csproj b/src/Benchmarks/Benchmarks.csproj index dfc4697f4..bcf7843ad 100644 --- a/src/Benchmarks/Benchmarks.csproj +++ b/src/Benchmarks/Benchmarks.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Benchmarks/EnglishArticleBenchmarks.cs b/src/Benchmarks/EnglishArticleBenchmarks.cs new file mode 100644 index 000000000..219161cfd --- /dev/null +++ b/src/Benchmarks/EnglishArticleBenchmarks.cs @@ -0,0 +1,7 @@ +[MemoryDiagnoser(false)] +public class EnglishArticleBenchmarks +{ + [Benchmark] + public string[] AppendArticlePrefix() => + EnglishArticle.AppendArticlePrefix([ "Ant", "The Theater", "The apple", "Fox", "Bear"]); +} \ No newline at end of file diff --git a/src/Benchmarks/Program.cs b/src/Benchmarks/Program.cs index 8765747d0..d2ced67d3 100644 --- a/src/Benchmarks/Program.cs +++ b/src/Benchmarks/Program.cs @@ -1 +1,2 @@ -BenchmarkRunner.Run(); \ No newline at end of file +BenchmarkRunner.Run( + typeof(Program).Assembly); \ No newline at end of file diff --git a/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs b/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs index a37fc8e3a..ffb973e4a 100644 --- a/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs +++ b/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs @@ -6,6 +6,7 @@ public class ArticlePrefixSortTests [InlineData(new[] { "Ant", "The Theater", "The apple", "Fox", "Bear" }, new[] { "Ant", "The apple", "Bear", "Fox", "The Theater" })] [InlineData(new[] { "An Ant", "The Theater", "the apple", "a Fox", "Bear" }, new[] { "An Ant", "the apple", "Bear", "a Fox", "The Theater" })] [InlineData(new[] { "Ant", "A Theater", "an apple", "Fox", "Bear" }, new[] { "Ant", "an apple", "Bear", "Fox", "A Theater" })] + [InlineData(new[] { " Ant ", " A Theater ", " an apple ", " Fox", "Bear " }, new[] { "A Theater", "an apple", "Ant", "Bear", "Fox" })] public void SortStringArrayIgnoringArticlePrefixes(string[] input, string[] expectedOutput) => Assert.Equal(expectedOutput, EnglishArticle.PrependArticleSuffix(EnglishArticle.AppendArticlePrefix(input))); diff --git a/src/Humanizer/ArticlePrefixSort.cs b/src/Humanizer/ArticlePrefixSort.cs index 5077e1e20..b59a62463 100644 --- a/src/Humanizer/ArticlePrefixSort.cs +++ b/src/Humanizer/ArticlePrefixSort.cs @@ -22,17 +22,18 @@ public static string[] AppendArticlePrefix(string[] items) for (var i = 0; i < items.Length; i++) { - var item = items[i]; + var item = items[i].AsSpan(); if (_regex.IsMatch(item)) { - var article = item.Substring(0, item.IndexOf(" ", StringComparison.CurrentCulture)); - var removed = item.Remove(0, item.IndexOf(" ", StringComparison.CurrentCulture)); + var indexOf = item.IndexOf(' '); + var removed = item[indexOf..].TrimStart(); + var article = item[..indexOf].TrimEnd(); var appended = $"{removed} {article}"; transformed[i] = appended.Trim(); } else { - transformed[i] = item.Trim(); + transformed[i] = item.Trim().ToString(); } } Array.Sort(transformed); From 034233da3b55b7fa0c716b211ad9f30a07fe6837 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 21 Feb 2024 13:16:42 +1100 Subject: [PATCH 2/2] Update ArticlePrefixSort.cs --- src/Humanizer/ArticlePrefixSort.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Humanizer/ArticlePrefixSort.cs b/src/Humanizer/ArticlePrefixSort.cs index b59a62463..e92fd8e33 100644 --- a/src/Humanizer/ArticlePrefixSort.cs +++ b/src/Humanizer/ArticlePrefixSort.cs @@ -28,8 +28,7 @@ public static string[] AppendArticlePrefix(string[] items) var indexOf = item.IndexOf(' '); var removed = item[indexOf..].TrimStart(); var article = item[..indexOf].TrimEnd(); - var appended = $"{removed} {article}"; - transformed[i] = appended.Trim(); + transformed[i] = $"{removed} {article}"; } else {