Help with Span/ReadOnlySpan, Memory/ReadOnlyMemory

I’ve been migrating a lot of code to use ReadOnlySpan<char> when accessing segments of strings. And with read-only access to arrays, ReadOnlySpan<T> seems like a better choice some times.

But the deeper I dive, the less confident I become about:

When to use?

When not to use?

Example Problem:

Given a Regex match that has N number of matches. Instead of getting ‘copies’ of string contents, I will extract ReadOnlySpan<char> in the following manner:

var group = match.Groups[1]; var span = originalString.AsSpan().Slice(group.Index, group.Length);

Seems simple enough, but I wonder if after repeat processing if I’m trading memory for extra processing or some other detrimental effect. I know .NET Core 3 is much better at this, but I’m more curious about my own misconceptions.

submitted by /u/electricessence
[link] [comments]

Leave a Reply