Skip to content

Add ReadOnlySpan constructors to BitArray - #131500

Open
joshuajyue wants to merge 5 commits into
dotnet:mainfrom
joshuajyue:bitarray-readonlyspan-constructors
Open

Add ReadOnlySpan constructors to BitArray#131500
joshuajyue wants to merge 5 commits into
dotnet:mainfrom
joshuajyue:bitarray-readonlyspan-constructors

Conversation

@joshuajyue

@joshuajyue joshuajyue commented Jul 28, 2026

Copy link
Copy Markdown
Member

Summary

  • Add ReadOnlySpan<bool>, ReadOnlySpan<byte>, and ReadOnlySpan<int> constructors to BitArray.
  • Route the existing array constructors through the new span overloads while preserving null and overflow behavior.
  • Preserve Boolean SIMD packing, scalar remainder handling, and integer endianness behavior.
  • Add coverage for parity, slices, stack-allocated spans, source independence, SIMD boundaries, and noncanonical Boolean values.

Fixes #80263

Validation

  • Checked CoreCLR and libraries baseline build
  • Checked CoreLib rebuild and testhost refresh
  • 216 BitArray_CtorTests passed with default hardware intrinsics
  • 216 BitArray_CtorTests passed with DOTNET_EnableAVX2=0
  • 216 BitArray_CtorTests passed with DOTNET_EnableHWIntrinsic=0
  • 33,944 System.Collections tests passed

Performance

Full BenchmarkDotNet results from Windows ARM64 Release:

  • Direct span construction was 33–51% faster than the existing span.ToArray() workaround across the measured sizes.
  • Allocations were approximately 50% lower for byte and integer inputs and up to 89% lower for Boolean inputs.
  • Existing array-constructor allocations were unchanged, with no observed regression for medium or large inputs.
  • At 32 elements, delegating the existing Boolean and byte array constructors added approximately 1 ns. If that overhead is considered significant, those constructors could retain dedicated implementations at the cost of duplicated logic.

Note

This pull request description was generated with GitHub Copilot.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4236475d-0243-488d-93d8-cd78e94d532b
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds new ReadOnlySpan<T>-based constructors to System.Collections.BitArray to enable allocation-free construction from span-backed inputs, while preserving existing array-constructor behaviors by routing them through the new span overloads. This extends the public System.Collections contract and provides test coverage for span-specific scenarios (slices, stackalloc, copy semantics, and SIMD boundary safety).

Changes:

  • Added BitArray(ReadOnlySpan<bool>), BitArray(ReadOnlySpan<byte>), and BitArray(ReadOnlySpan<int>) constructors and routed existing array constructors through them.
  • Updated the System.Collections reference assembly to expose the new public constructors.
  • Expanded unit tests to validate span parity with array constructors, slicing/stackalloc usage, source independence, non-canonical bool handling, and vector boundary safety.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Private.CoreLib/src/System/Collections/BitArray.cs Implements the new span constructors and reuses them from existing array constructors without changing established behaviors (null/overflow handling, packing semantics).
src/libraries/System.Collections/tests/BitArray/BitArray_CtorTests.cs Adds coverage for span constructors (slices, stackalloc, copy semantics, non-canonical bools, and BoundedMemory OOB checks).
src/libraries/System.Collections/ref/System.Collections.cs Adds the new public constructor signatures to the ref surface for System.Collections.BitArray.

Copilot AI review requested due to automatic review settings July 29, 2026 20:49
@joshuajyue
joshuajyue marked this pull request as ready for review July 29, 2026 20:49
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Comment thread src/libraries/System.Private.CoreLib/src/System/Collections/BitArray.cs Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4236475d-0243-488d-93d8-cd78e94d532b
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4236475d-0243-488d-93d8-cd78e94d532b
Copilot AI review requested due to automatic review settings July 30, 2026 08:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 30, 2026 19:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@jeffhandley jeffhandley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions for comments on the tests to make them easier to maintain over time (probably applies to existing tests too), but non-blocking.

Comment on lines +268 to +269
bool[] boolValues = [false, true, false, true, true, false];
AssertBitArray(new BitArray(boolValues.AsSpan(1, 4)), [true, false, true, true]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a comment that declares if/why the ordering of these values is intentional so that future maintainers know what can and cannot be changed without leaking a regression.

This could also be accomplished with a more descriptive test name.

}

[Fact]
public static void Ctor_StackAllocatedSpans()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: More descriptive test name or a comment here too

[InlineData(64)]
[InlineData(67)]
[InlineData(100)]
public static void Ctor_BoolSpan_NonCanonicalTrueValues(int length)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Assert.Equal(bitArray.Length, clone.Length);
}

private static void AssertBitArray(BitArray bitArray, ReadOnlySpan<bool> expected)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea refactoring this out

/// "<paramref name="values"/>[0] &amp; 1" represents bit 0, "<paramref name="values"/>[0] &amp; 2" represents bit 1,
/// "<paramref name="values"/>[0] &amp; 4" represents bit 2, and so on.
///
/// This constructor is an <c>O(n)</c> operation, where <c>n</c> is the number of elements in <paramref name="values"/>.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: System.Collection.BitArray - New constructors to pass a ReadOnlySpan<T> where T is int, bool or byte

5 participants