Skip to content

Apply hardware intrinsics to BitArray.*Shift #113299

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

kzrnm
Copy link
Contributor

@kzrnm kzrnm commented Mar 9, 2025

Related #63722, #113005

benchmark


BenchmarkDotNet v0.14.0, Windows 11 (10.0.26100.3194)
13th Gen Intel Core i5-13500, 1 CPU, 20 logical and 14 physical cores
.NET SDK 10.0.100-preview.3.25125.5
  [Host]     : .NET 10.0.0 (10.0.25.12411), X64 RyuJIT AVX2
  Job-UQJRRD : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX2
  Job-IYICYK : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX2


Method Toolchain N Mean Ratio Allocated Alloc Ratio
LeftShift \main\corerun.exe 256 45.553 ns 1.00 - NA
LeftShift \pr\corerun.exe 256 10.037 ns 0.22 - NA
RightShift \main\corerun.exe 256 56.281 ns 1.00 - NA
RightShift \pr\corerun.exe 256 9.565 ns 0.17 - NA
LeftShift \main\corerun.exe 65536 10,493.384 ns 1.00 - NA
LeftShift \pr\corerun.exe 65536 1,699.862 ns 0.16 - NA
RightShift \main\corerun.exe 65536 11,729.467 ns 1.00 - NA
RightShift \pr\corerun.exe 65536 1,860.553 ns 0.16 - NA
LeftShift \main\corerun.exe 16777216 3,054,162.161 ns 1.00 - NA
LeftShift \pr\corerun.exe 16777216 1,051,349.065 ns 0.34 - NA
RightShift \main\corerun.exe 16777216 3,341,553.776 ns 1.00 - NA
RightShift \pr\corerun.exe 16777216 639,720.483 ns 0.19 - NA
Benchmark code
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using System.Collections;

[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
public class ShiftTest
{
    [Params([
        1 << 8,
        1 << 16,
        1 << 24,
    ])]
    public int N;
    const int shiftSize = 13;
    BitArray b;

    [GlobalSetup]
    public void Setup()
    {
        var bytes = new byte[N];
        new Random(227).NextBytes(bytes);
        b = new BitArray(bytes);
    }

    [Benchmark] public BitArray LeftShift() => b.LeftShift(shiftSize);
    [Benchmark] public BitArray RightShift() => b.RightShift(shiftSize);
}

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 9, 2025
@eiriktsarpalis
Copy link
Member

Hi @kzrnm, apologies for the delay in reviewing this. Is there a chance you could rebase your changes on top of the latest main? I don't have write permissions in your PR branch to do it myself.

@eiriktsarpalis eiriktsarpalis added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jul 1, 2025
@kzrnm
Copy link
Contributor Author

kzrnm commented Jul 3, 2025

I updated to match the latest implementation.
In commit e96a2b8, the implementation for shifting as a 32-bit integer has been updated to apply a mask within the byte range, aligning it with the behavior of the Apply method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Collections community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants