Adding Span IndexOf that takes an index and count#16787
Conversation
| @@ -87,6 +87,7 @@ public static class SpanExtensions | |||
| { | |||
| public static int IndexOf<T>(this Span<T> span, T value) where T:struct, IEquatable<T> { throw null; } | |||
There was a problem hiding this comment.
Where is the approved API proposal for this? What about the other overloads?
There was a problem hiding this comment.
If this is just a temporary hack until the Span codegen is fixed, you should keep it in corefxlab.
There was a problem hiding this comment.
Yeah, I agree corfxlab might be a better place for it. corfx APIs should represent what we are confident in shipping.
There was a problem hiding this comment.
That's fine, will slicing + indexof ever be as fast as passing an index and count? I have some doubts about that.
while (span.Length > 0)
{
int index = span.IndexOf((byte)'\n');
if (index == -1) break;
var line = span.Slice(0, index);
span = span.Slice(index + 1);
}VS
int index = 0;
while (index < span.Length)
{
int lineIndex = span.IndexOf((byte)'\n', index, span.Length - index);
if (lineIndex == -1) break;
var line = span.Slice(index, lineIndex - index + 1);
index += length.Length
}Will that ever be within the ballpark?
There was a problem hiding this comment.
Yes, it should be pretty equivalent - once the JIT team implements the planned struct enregistration that handles Span well.
There was a problem hiding this comment.
Wow, I look forward to seeing this. @ahsonkhan Can you make sure we have a benchmark for this scenario? It's pretty fundamental.
There was a problem hiding this comment.
I have created it in the PR in corefxlab:
result = bytes.IndexOf(lookupVal, startIndex, count);VS.
result = bytes.Slice(startIndex, count).IndexOf(lookupVal);| return -1; | ||
| } | ||
|
|
||
| public static int IndexOf(ref byte searchSpace, byte value, int startIndex, int count, int spanLength) |
There was a problem hiding this comment.
I do not think it makes sense to replicate the worker method for this - you should use the existing public static int IndexOf(ref byte searchSpace, byte value, int length) method.
|
This PR can be closed. Opened one in corefxlab - dotnet/corefxlab#1278 |
No description provided.