Skip to content

Commit

Permalink
Improve ProbabilisticMap performance for small value sets (#85202)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Apr 23, 2023
1 parent 19fde3f commit 46101c4
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;

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

public unsafe IndexOfAnyCharValuesProbabilistic(ReadOnlySpan<char> values)
public IndexOfAnyCharValuesProbabilistic(scoped ReadOnlySpan<char> values)
{
if (Vector128.IsHardwareAccelerated && values.Length < 8)
{
// ProbabilisticMap does a Span.Contains check to confirm potential matches.
// If we have fewer than 8 values, pad them with existing ones to make the verification faster.
Span<char> newValues = stackalloc char[8];
newValues.Fill(values[0]);
values.CopyTo(newValues);
values = newValues;
}

_values = new string(values);
_map = new ProbabilisticMap(_values);
}
Expand Down

0 comments on commit 46101c4

Please sign in to comment.