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

Commit 6863fc4

Browse files
author
Alexander Gusarov
committed
Move index check inside update operation
1 parent 9234a6f commit 6863fc4

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,16 @@ public static int IndexOfAny<T>(ref T searchSpace, int searchSpaceLength, ref T
300300
return 0; // A zero-length sequence is always treated as "found" at the start of the search space.
301301

302302
int index = -1;
303-
for (int i = 0; i < valueLength && index != 0; i++)
303+
for (int i = 0; i < valueLength; i++)
304304
{
305305
var tempIndex = IndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
306306
if ((uint)tempIndex < (uint)index)
307307
{
308308
index = tempIndex;
309309
// Reduce space for search, cause we don't care if we find the search value after the index of a previously found value
310310
searchSpaceLength = tempIndex;
311+
312+
if (index == 0) break;
311313
}
312314
}
313315
return index;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,16 @@ public static int IndexOfAny(ref byte searchSpace, int searchSpaceLength, ref by
6161
return 0; // A zero-length sequence is always treated as "found" at the start of the search space.
6262

6363
int index = -1;
64-
for (int i = 0; i < valueLength && index != 0; i++)
64+
for (int i = 0; i < valueLength; i++)
6565
{
6666
var tempIndex = IndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
6767
if ((uint)tempIndex < (uint)index)
6868
{
6969
index = tempIndex;
7070
// Reduce space for search, cause we don't care if we find the search value after the index of a previously found value
7171
searchSpaceLength = tempIndex;
72+
73+
if (index == 0) break;
7274
}
7375
}
7476
return index;

src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Compile Include="Perf.Span.Clear.cs" />
1515
<Compile Include="Perf.Span.Fill.cs" />
1616
<Compile Include="Perf.Span.IndexOf.cs" />
17-
<Compile Include="Perf.Span.IndexOfAny.cs" />
17+
<Compile Include="Perf.Span.IndexOfAny.cs" />
1818
<Compile Include="Perf.Span.StartsWith.cs" />
1919
<Compile Include="Perf.MemorySlice.cs" />
2020
<Compile Include="Perf.Utf8Formatter.cs" />

0 commit comments

Comments
 (0)