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 Span.Count #103728

Merged
merged 2 commits into from
Jun 21, 2024
Merged

Improve Span.Count #103728

merged 2 commits into from
Jun 21, 2024

Conversation

MihaZupan
Copy link
Member

We're currently calculating the number of remaining elements after the vectorized loop and falling back to the scalar path if we have less than half a vector's worth of data remaining.

It's simpler and faster if we just unconditionally process the last vector.

public class CountBenchmarks<T> where T : struct, INumber<T>
{
    private T[][] _numbers;

    [GlobalSetup]
    public void Setup()
    {
        _numbers = new T[10_000][];

        var rng = new Random(42);

        for (int i = 0; i < _numbers.Length; i++)
        {
            _numbers[i] = new T[rng.Next(0, 256 / Unsafe.SizeOf<T>())];
            rng.NextBytes(MemoryMarshal.AsBytes(_numbers[i].AsSpan()));
        }
    }

    [Benchmark]
    public int Count()
    {
        int count = 0;
        foreach (T[] numbers in _numbers) count += numbers.AsSpan().Count(T.CreateChecked(42));
        return count;
    }
}
Method Toolchain Type Mean Error Ratio
Count main byte 175.4 us 0.82 us 1.00
Count pr byte 100.1 us 0.25 us 0.57
Count main short 147.6 us 0.24 us 1.00
Count pr short 102.7 us 0.17 us 0.70
Count main int 143.01 us 0.277 us 1.00
Count pr int 98.10 us 0.262 us 0.69
Count main long 134.13 us 2.157 us 1.00
Count pr long 99.87 us 0.618 us 0.74

@gfoidl this goes against your numbers in #82687, presumably because you were testing with a single Length at a time and all the branches were predictable?

@MihaZupan MihaZupan added this to the 9.0.0 milestone Jun 19, 2024
@MihaZupan MihaZupan self-assigned this Jun 19, 2024
Copy link
Contributor

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

@gfoidl
Copy link
Member

gfoidl commented Jun 20, 2024

this goes against your numbers in #82687, presumably because you were testing with a single Length at a time and all the branches were predictable?

Probably. At least your benchmark (w/ different length) seems better. So a nice improvement.

@MihaZupan MihaZupan merged commit fc70f8b into dotnet:main Jun 21, 2024
140 of 146 checks passed
rzikm pushed a commit to rzikm/dotnet-runtime that referenced this pull request Jun 24, 2024
* Improve Span.Count

* Fix indentation
@github-actions github-actions bot locked and limited conversation to collaborators Jul 23, 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.

3 participants