From df930725173b7f19d8ccd2f4b9e6c701fddfd360 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Wed, 15 Jan 2025 23:54:14 +0700 Subject: [PATCH 1/5] Add benchmarking for whitespace detection in strings This commit introduces a new method `IsWhiteSpace` to benchmark the detection of whitespace in spans. --- .../libraries/System.Memory/ReadOnlySpan.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs index dae8b7a7775..e4b0a740389 100644 --- a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs +++ b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs @@ -95,6 +95,26 @@ public static IEnumerable TrimArguments() yield return "abcdefg"; } + [Benchmark] + [ArgumentsSource(nameof(IsWhiteSpaceArguments))] + [MemoryRandomization] + public bool IsWhiteSpace(string input) => input.AsSpan().IsWhiteSpace(); + + public static IEnumerable IsWhiteSpaceArguments() + { + yield return ""; + yield return "0abcdefg"; + yield return " 1abcdefg"; + yield return " 2abcdefg"; + yield return " 4abcdefg"; + yield return " 5abcdefg"; + yield return " 6abcdefg"; + yield return " 7abcdefg"; + yield return " 8abcdefg"; + yield return " 16abcdefg"; + yield return " 32abcdefg"; + } + private static string GenerateInputString(char source, int count, char replaceChar, int replacePos) { char[] str = new char[count]; From 1fe32811664c74fbfbab0b242778ea61b2e8fda8 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Thu, 16 Jan 2025 04:07:43 +0700 Subject: [PATCH 2/5] Update IsWhiteSpaceArguments --- src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs index e4b0a740389..6e993776f04 100644 --- a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs +++ b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs @@ -111,6 +111,10 @@ public static IEnumerable IsWhiteSpaceArguments() yield return " 6abcdefg"; yield return " 7abcdefg"; yield return " 8abcdefg"; + yield return " 9abcdefg"; + yield return " 10abcdefg"; + yield return " 11abcdefg"; + yield return " 12abcdefg"; yield return " 16abcdefg"; yield return " 32abcdefg"; } From b10d16b33e86941d414fde68bffb0006fe9a2860 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Fri, 17 Jan 2025 09:22:44 +0700 Subject: [PATCH 3/5] Remove unnecessary string yields in ReadOnlySpan.cs This commit removes several lines that yield specific string values in the `ReadOnlySpan.cs` file. The removed strings include " 5abcdefg", " 10abcdefg", " 11abcdefg", and " 12abcdefg". These changes aim to simplify the output and adjust the test cases generated by this method. --- src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs index 6e993776f04..73fac7e67b3 100644 --- a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs +++ b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs @@ -107,14 +107,10 @@ public static IEnumerable IsWhiteSpaceArguments() yield return " 1abcdefg"; yield return " 2abcdefg"; yield return " 4abcdefg"; - yield return " 5abcdefg"; yield return " 6abcdefg"; yield return " 7abcdefg"; yield return " 8abcdefg"; yield return " 9abcdefg"; - yield return " 10abcdefg"; - yield return " 11abcdefg"; - yield return " 12abcdefg"; yield return " 16abcdefg"; yield return " 32abcdefg"; } From 516f59cfc80c10f4a7189548c6ed67a064200874 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Thu, 23 Jan 2025 06:39:26 +0700 Subject: [PATCH 4/5] Updated the `IsWhiteSpace `method to test for whitespace characters other than space. --- .../libraries/System.Memory/ReadOnlySpan.cs | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs index 73fac7e67b3..ea3feb756f3 100644 --- a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs +++ b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Linq; using BenchmarkDotNet.Attributes; using MicroBenchmarks; using Consumer = BenchmarkDotNet.Engines.Consumer; @@ -98,21 +99,23 @@ public static IEnumerable TrimArguments() [Benchmark] [ArgumentsSource(nameof(IsWhiteSpaceArguments))] [MemoryRandomization] - public bool IsWhiteSpace(string input) => input.AsSpan().IsWhiteSpace(); + public bool IsWhiteSpace(int _, string input) => input.AsSpan().IsWhiteSpace(); - public static IEnumerable IsWhiteSpaceArguments() + public static IEnumerable IsWhiteSpaceArguments() { - yield return ""; - yield return "0abcdefg"; - yield return " 1abcdefg"; - yield return " 2abcdefg"; - yield return " 4abcdefg"; - yield return " 6abcdefg"; - yield return " 7abcdefg"; - yield return " 8abcdefg"; - yield return " 9abcdefg"; - yield return " 16abcdefg"; - yield return " 32abcdefg"; + const string WhiteSpaceChars = "\t\n\v\f\r\u0085\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000"; + + yield return new object[] { 001, ""}; + yield return new object[] { 002, "0abcdefg" }; + yield return new object[] { 010, new string(' ', 01) + "1abcdefg" }; + yield return new object[] { 020, new string(' ', 01) + WhiteSpaceChars.Substring(0, 01) + "2abcdefg" }; + yield return new object[] { 040, new string(' ', 02) + WhiteSpaceChars.Substring(0, 02) + "4abcdefg" }; + yield return new object[] { 060, new string(' ', 03) + WhiteSpaceChars.Substring(0, 03) + "6abcdefg" }; + yield return new object[] { 070, new string(' ', 04) + WhiteSpaceChars.Substring(0, 03) + "7abcdefg" }; + yield return new object[] { 080, new string(' ', 04) + WhiteSpaceChars.Substring(0, 04) + "8abcdefg" }; + yield return new object[] { 090, new string(' ', 04) + WhiteSpaceChars.Substring(0, 05) + "9abcdefg" }; + yield return new object[] { 160, new string(' ', 08) + WhiteSpaceChars.Substring(0, 08) + "16abcdefg" }; + yield return new object[] { 320, new string(' ', 16) + WhiteSpaceChars.Substring(0, 16) + "32abcdefg" }; } private static string GenerateInputString(char source, int count, char replaceChar, int replacePos) From e4ab65e4607a8022322f336ad6ede649ffc5c679 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Thu, 23 Jan 2025 06:43:14 +0700 Subject: [PATCH 5/5] Updated tests for nine whitespaces. --- src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs index ea3feb756f3..4a9662c9739 100644 --- a/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs +++ b/src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs @@ -113,7 +113,7 @@ public static IEnumerable IsWhiteSpaceArguments() yield return new object[] { 060, new string(' ', 03) + WhiteSpaceChars.Substring(0, 03) + "6abcdefg" }; yield return new object[] { 070, new string(' ', 04) + WhiteSpaceChars.Substring(0, 03) + "7abcdefg" }; yield return new object[] { 080, new string(' ', 04) + WhiteSpaceChars.Substring(0, 04) + "8abcdefg" }; - yield return new object[] { 090, new string(' ', 04) + WhiteSpaceChars.Substring(0, 05) + "9abcdefg" }; + yield return new object[] { 090, new string(' ', 05) + WhiteSpaceChars.Substring(0, 04) + "9abcdefg" }; yield return new object[] { 160, new string(' ', 08) + WhiteSpaceChars.Substring(0, 08) + "16abcdefg" }; yield return new object[] { 320, new string(' ', 16) + WhiteSpaceChars.Substring(0, 16) + "32abcdefg" }; }