Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ProbabilisticMap performance for small value sets #85202

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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