Add whitespace handling methods to MemoryExtensions#111439
Add whitespace handling methods to MemoryExtensions#111439AlexRadch wants to merge 10 commits intodotnet:mainfrom
Conversation
Updated the MemoryExtensions class with new methods for efficient whitespace handling, including ContainsAnyWhiteSpace, IndexOfAnyWhiteSpace, and LastIndexOfAnyWhiteSpace. Optimized the IsWhiteSpace method to utilize SearchValues.WhiteSpaces. Added corresponding test cases in StringSearchValues.cs to validate the new functionality.
|
Note regarding the |
1 similar comment
|
Note regarding the |
src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.Globalization.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.cs
Outdated
Show resolved
Hide resolved
MihaZupan
left a comment
There was a problem hiding this comment.
A concern we had with the implementation here was whether it's worth paying the overhead of taking a vectorized path if we expect the match to commonly be close to the start.
For example it's not obvious why span.IsWhiteSpace should use a different strategy compared to string.IsNullOrWhiteSpace, where the 99% case is that you're validating an argument and expect it have non-whitespace chars (often already with the first character).
Have you done any perf measurements for such cases?
src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.Globalization.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.Globalization.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
I haven't conducted performance measurements for such cases yet, but I’d be happy to write and run performance tests if we can determine the most relevant data set for the evaluation. Could you clarify what types of inputs or scenarios would best reflect the use cases you're concerned about? For example, should we focus on inputs where the match is typically close to the start, or would you like a broader range of cases? Your guidance will help ensure the tests provide meaningful insights. |
I write benchmarks for the ROSpan IsWhiteSpace method. On my computer, I got the next results |
src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.cs
Outdated
Show resolved
Hide resolved
`string.SearchValuesStorage.WhiteSpaceChars` in the `MemoryExtensions` class methods.
This commit removes the `using System.Buffers;` directive from the `MemoryExtensions.Globalization.cs` file, indicating that the functionality provided by the `System.Buffers` namespace is no longer needed or has been replaced by alternative implementations.
| @@ -0,0 +1,261 @@ | |||
| // Licensed to the .NET Foundation under one or more agreements. | |||
| // The .NET Foundation licenses this file to you under the MIT license. | |||
|
|
|||
src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.Globalization.cs
Show resolved
Hide resolved
|
The benchmarks show that the non-whitespace starting char case ( |
|
@PranavSenthilnathan request review |
huoyaoyuan
left a comment
There was a problem hiding this comment.
There are pending comments that need to be resolved.
| } | ||
| return true; | ||
| } | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] |
There was a problem hiding this comment.
In don't think these trivial methods require AggressiveInlining.
Co-authored-by: Pranav Senthilnathan <pranav.senthilnathan@live.com>
Part of #77959 except
ContainsAnyExceptWhiteSpaceUpdated the MemoryExtensions class with new methods for efficient whitespace handling, including ContainsAnyWhiteSpace, IndexOfAnyWhiteSpace, and LastIndexOfAnyWhiteSpace.
Optimized the IsWhiteSpace method to utilize SearchValues.WhiteSpaces.
Added corresponding test cases in StringSearchValues.cs to validate the new functionality.