Remove unsafe code from System.Collections.BitArray#127846
Draft
EgorBo wants to merge 1 commit intodotnet:mainfrom
Draft
Remove unsafe code from System.Collections.BitArray#127846EgorBo wants to merge 1 commit intodotnet:mainfrom
EgorBo wants to merge 1 commit intodotnet:mainfrom
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
Member
Author
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the BitArray(bool[] values) constructor in System.Private.CoreLib to avoid reinterpreting the bool[] backing storage via Unsafe.As<bool, byte> by instead operating over a ReadOnlySpan<byte> obtained from MemoryMarshal.AsBytes.
Changes:
- Convert the input
bool[]toReadOnlySpan<byte>and use it as the SIMD input source. - Replace
Vector*.LoadUnsafe(ref, offset)loops withVector*.Create(ReadOnlySpan<byte>)plus span slicing to advance through the input. - Adjust loop structure from indexed
forloops towhile (valuesAsBytes.Length >= ...)loops.
Member
Author
|
Note AI-generated benchmark. @EgorBot -arm -amd using System;
using System.Collections;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);
public class Bench
{
private bool[] _values = default!;
[Params(64, 256, 1024, 16384)]
public int Length { get; set; }
[GlobalSetup]
public void Setup()
{
var rng = new Random(42);
_values = new bool[Length];
for (int i = 0; i < Length; i++)
_values[i] = rng.Next(2) == 0;
}
[Benchmark]
public BitArray Construct() => new BitArray(_values);
} |
EgorBo
commented
May 6, 2026
|
|
||
| ref byte arrayRef = ref MemoryMarshal.GetArrayDataReference(_array); | ||
| ref byte value = ref Unsafe.As<bool, byte>(ref MemoryMarshal.GetArrayDataReference<bool>(values)); | ||
| ReadOnlySpan<byte> valuesAsBytes = MemoryMarshal.AsBytes(values.AsSpan()); |
Member
Author
There was a problem hiding this comment.
It's fully safe to bitcast Span<bool> to Span<byte>
eiriktsarpalis
approved these changes
May 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This PR is AI-generated.
No diffs