-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: MemoryExtensions.Replace with separate source/destination buffers #81829
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivationWe recently added: public static void Replace<T>(this Span<T> span, T oldValue, T newValue) where T : IEquatable<T>? and we've been able to use it ourselves in several places. However, there are several places that actually want to be able to perform a copy/replace operation together. For example, string.Replace needs to copy into a separate buffer and perform the replace in that buffer, and https://github.com/dotnet/aspnetcore/blob/287d7eab1c644bce2d4c96751bc3d323362b1bb7/src/Shared/QueryStringEnumerable.cs#L166-L229 similarly copies from a We already have the implementation for this as part of string.Replace, so this is effectively just exposing that through an additional Replace overload, which can then be used in other places, like in ASP.NET. API Proposalnamespace System
public static class MemoryExtensions
{
public static void Replace<T>(this Span<T> span, T oldValue, T newValue) where T : IEquatable<T>?
+ public static void Replace<T>(this ReadOnlySpan<T> span, Span<T>, T oldValue, T newValue) where T : IEquatable<T>?
} API Usage// Create a string from the ReadOnlySpan<char>, replacing all pluses with spaces.
return string.Create(source.Length, (IntPtr)(&source), (dest, rosPtr) =>
{
ReadOnlySpan<char> source = *(ReadOnlySpan<char>*)rosPtr;
source.Replace(dest, '+', ' ');
}); Alternative DesignsNo response RisksNo response
|
Likely meant |
Fixed |
Looks good as proposed. We discussed whether or not the method warranted an "-Into" suffix, given the existing overload, and decided it didn't. The method probably should reject source and destination in a partial overlap. Given that the "existing" Replace method was added in this release, consider whether it is still warranted, or should be removed in favor of this one. The concern was also raised that a namespace System
public static partial class MemoryExtensions
{
public static void Replace<T>(this ReadOnlySpan<T> source, Span<T> destination, T oldValue, T newValue) where T : IEquatable<T>?
} |
Background and motivation
We recently added:
and we've been able to use it ourselves in several places, e.g. in
StringBuilder.Replace
. However, there are several places that actually want to be able to perform a copy/replace operation together, and doing so as a copy followed by a replace can be significantly slower (upwards of 2x) than a combined operation. For example, string.Replace needs to copy into a separate buffer and perform the replace in that buffer, and https://github.com/dotnet/aspnetcore/blob/287d7eab1c644bce2d4c96751bc3d323362b1bb7/src/Shared/QueryStringEnumerable.cs#L166-L229 similarly copies from aReadOnlySpan<char>
to aSpan<char>
doing a replace in the process.We already have the implementation for this as part of string.Replace, so this is effectively just exposing that through an additional Replace overload, which can then be used in other places, like in ASP.NET.
API Proposal
namespace System public static class MemoryExtensions { public static void Replace<T>(this Span<T> span, T oldValue, T newValue) where T : IEquatable<T>? + public static void Replace<T>(this ReadOnlySpan<T> source, Span<T> destination, T oldValue, T newValue) where T : IEquatable<T>? }
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: