Resharper should propose to convert ReadOnlyMemory<T> to Span<T> when adequate
Hello,
Here is a simple method:
public void DoSomething (ReadOnlyMemory <Char> text, TextWriter writer)
{
writer.Write(text);
}
What's the problem with this code?
The overload that is selected is `TextWriter.Write(Object?)`. This method converts the parameter to a String, usind `ToString()` and writes the string.
So, each time the method is called, a copy of the string is created is memory, and must be GC'ed later.
The solution
If I write `writer.Write(text.Span)`, the overload that is called is `TextWriter.Write(ReadOnlySpan<Char>)`. In this case, TextWriter simply writes the chars to the stream. The only data allocation is a `Span<T>`, which is allocated on the stack, so no GC work!
The request
When a method is called with a parameter of type `ReadOnlyMemory<T>`, the method has an overload with a `ReadOnlySpan<T>` parameter, but no overload with `ReadOnlyMemory<T>` parameter; you must suggest to add `.Span`, in order to reduce memory use (and CPU cycles).
Please sign in to leave a comment.
Hello Patrick Lanz, thank you for your request. I've submitted a new feature request to our bug tracker. Please comment or vote for it to get notifications about status changes. Thank you!