Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 016659c

Browse files
gfoidljkotas
authored andcommitted
SpanHelpers IndexOfAny-methods use sentinel-value (#25970)
* SpanHelpers IndexOfAny-methods use sentinel-value Instead of having _valueLength_-times an if-check in the loop, a sentinel value is used and hence only one final checks needs to done. Perf-win depends on the size of _valueLength_, the greater the more win. * LastIndexOf initializes index with -1 instead of int.MinValue As of PR-feedback. * Better checks based on if instead of ternary operator As of PR-feedback. * SpanHelpers.byte.cs IndexOfAny can avoid int.MaxValue due to unit-trick As of PR-Feedback
1 parent c9eb1e3 commit 016659c

File tree

2 files changed

+3
-12
lines changed

2 files changed

+3
-12
lines changed

src/System.Memory/src/System/SpanHelpers.T.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ public static int LastIndexOfAny<T>(ref T searchSpace, int searchSpaceLength, re
6363
for (int i = 0; i < valueLength; i++)
6464
{
6565
var tempIndex = LastIndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
66-
if (tempIndex != -1)
67-
{
68-
index = (index == -1 || index < tempIndex) ? tempIndex : index;
69-
}
66+
if (tempIndex > index) index = tempIndex;
7067
}
7168
return index;
7269
}

src/System.Memory/src/System/SpanHelpers.byte.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ public static int IndexOfAny(ref byte searchSpace, int searchSpaceLength, ref by
6464
for (int i = 0; i < valueLength; i++)
6565
{
6666
var tempIndex = IndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
67-
if (tempIndex != -1)
68-
{
69-
index = (index == -1 || index > tempIndex) ? tempIndex : index;
70-
}
67+
if ((uint)tempIndex < (uint)index) index = tempIndex;
7168
}
7269
return index;
7370
}
@@ -84,10 +81,7 @@ public static int LastIndexOfAny(ref byte searchSpace, int searchSpaceLength, re
8481
for (int i = 0; i < valueLength; i++)
8582
{
8683
var tempIndex = LastIndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
87-
if (tempIndex != -1)
88-
{
89-
index = (index == -1 || index < tempIndex) ? tempIndex : index;
90-
}
84+
if (tempIndex > index) index = tempIndex;
9185
}
9286
return index;
9387
}

0 commit comments

Comments
 (0)