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

Add ContainsAny{Except} path to SearchValues #96924

Merged
merged 1 commit into from
Jan 19, 2024

Conversation

MihaZupan
Copy link
Member

Followup after #87621 to add dedicated ContainsAny{Except} paths for SearchValues.
This PR adds such logic for SearchValues that go through IndexOfAnyAsciiSearcher.

I tried to avoid code duplication between IndexOfAny and ContainsAny by abusing generics some more. Don't know if that could lead to any downsides.

#96588 is renaming some files so if/after that is merged, we can wire up ContainsAny{Except} for single-value SearchValues to call into Contains helpers as well.

Method Toolchain Match Length Mean Error Ratio
ContainsAny main False 16 3.210 ns 0.0605 ns 1.00
ContainsAny pr False 16 2.480 ns 0.0098 ns 0.78
ContainsAnyExcept main False 16 1.924 ns 0.0134 ns 1.00
ContainsAnyExcept pr False 16 2.008 ns 0.0075 ns 1.05
ContainsAny main True 16 4.332 ns 0.0214 ns 1.00
ContainsAny pr True 16 1.857 ns 0.0064 ns 0.43
ContainsAnyExcept main True 16 2.512 ns 0.0261 ns 1.00
ContainsAnyExcept pr True 16 1.928 ns 0.0085 ns 0.77
Benchmark code
public class SearchValuesContainsAny
{
    private static readonly SearchValues<char> s_values = SearchValues.Create("ABCDabcd1234");

    private string _text = null!;
    private string _textExcept = null!;

    [Params(false, true)]
    public bool Match;

    [Params(16)]
    public int Length;

    [GlobalSetup]
    public void Setup()
    {
        var text = new string('\n', Length).ToCharArray();
        var textExcept = new string('a', Length).ToCharArray();

        if (Match)
        {
            text[0] = 'a';
            textExcept[0] = '\n';
        }

        _text = new string(text);
        _textExcept = new string(textExcept);
    }

    [Benchmark]
    public bool ContainsAny() => _text.AsSpan().ContainsAny(s_values);

    [Benchmark]
    public bool ContainsAnyExcept() => _textExcept.AsSpan().ContainsAnyExcept(s_values);
}

@MihaZupan MihaZupan added this to the 9.0.0 milestone Jan 12, 2024
@MihaZupan MihaZupan self-assigned this Jan 12, 2024
@ghost
Copy link

ghost commented Jan 12, 2024

Tagging subscribers to this area: @dotnet/area-system-buffers
See info in area-owners.md if you want to be subscribed.

Issue Details

Followup after #87621 to add dedicated ContainsAny{Except} paths for SearchValues.
This PR adds such logic for SearchValues that go through IndexOfAnyAsciiSearcher.

I tried to avoid code duplication between IndexOfAny and ContainsAny by abusing generics some more. Don't know if that could lead to any downsides.

#96588 is renaming some files so if/after that is merged, we can wire up ContainsAny{Except} for single-value SearchValues to call into Contains helpers as well.

Method Toolchain Match Length Mean Error Ratio
ContainsAny main False 16 3.210 ns 0.0605 ns 1.00
ContainsAny pr False 16 2.480 ns 0.0098 ns 0.78
ContainsAnyExcept main False 16 1.924 ns 0.0134 ns 1.00
ContainsAnyExcept pr False 16 2.008 ns 0.0075 ns 1.05
ContainsAny main True 16 4.332 ns 0.0214 ns 1.00
ContainsAny pr True 16 1.857 ns 0.0064 ns 0.43
ContainsAnyExcept main True 16 2.512 ns 0.0261 ns 1.00
ContainsAnyExcept pr True 16 1.928 ns 0.0085 ns 0.77
Benchmark code
public class SearchValuesContainsAny
{
    private static readonly SearchValues<char> s_values = SearchValues.Create("ABCDabcd1234");

    private string _text = null!;
    private string _textExcept = null!;

    [Params(false, true)]
    public bool Match;

    [Params(16)]
    public int Length;

    [GlobalSetup]
    public void Setup()
    {
        var text = new string('\n', Length).ToCharArray();
        var textExcept = new string('a', Length).ToCharArray();

        if (Match)
        {
            text[0] = 'a';
            textExcept[0] = '\n';
        }

        _text = new string(text);
        _textExcept = new string(textExcept);
    }

    [Benchmark]
    public bool ContainsAny() => _text.AsSpan().ContainsAny(s_values);

    [Benchmark]
    public bool ContainsAnyExcept() => _textExcept.AsSpan().ContainsAnyExcept(s_values);
}
Author: MihaZupan
Assignees: MihaZupan
Labels:

area-System.Buffers

Milestone: 9.0.0

@MihaZupan MihaZupan merged commit 2939fde into dotnet:main Jan 19, 2024
174 of 178 checks passed
tmds pushed a commit to tmds/runtime that referenced this pull request Jan 23, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants