Skip to content

Commit 46101c4

Browse files
authored
Improve ProbabilisticMap performance for small value sets (#85202)
1 parent 19fde3f commit 46101c4

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAnyCharValuesProbabilistic.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Runtime.CompilerServices;
55
using System.Runtime.InteropServices;
6+
using System.Runtime.Intrinsics;
67

78
namespace System.Buffers
89
{
@@ -11,8 +12,18 @@ internal sealed class IndexOfAnyCharValuesProbabilistic : IndexOfAnyValues<char>
1112
private ProbabilisticMap _map;
1213
private readonly string _values;
1314

14-
public unsafe IndexOfAnyCharValuesProbabilistic(ReadOnlySpan<char> values)
15+
public IndexOfAnyCharValuesProbabilistic(scoped ReadOnlySpan<char> values)
1516
{
17+
if (Vector128.IsHardwareAccelerated && values.Length < 8)
18+
{
19+
// ProbabilisticMap does a Span.Contains check to confirm potential matches.
20+
// If we have fewer than 8 values, pad them with existing ones to make the verification faster.
21+
Span<char> newValues = stackalloc char[8];
22+
newValues.Fill(values[0]);
23+
values.CopyTo(newValues);
24+
values = newValues;
25+
}
26+
1627
_values = new string(values);
1728
_map = new ProbabilisticMap(_values);
1829
}

0 commit comments

Comments
 (0)