Background and motivation
In order to do custom, high-performance comparisons on the bits in a BitArray, I would like to be able to get a span to the data. This then enables that span to be used in operations like those on TensorPrimitives.
API Proposal
namespace System.Collections;
public class BitArray
{
+ public ReadOnlySpan<byte> AsBytes();
}
API Usage
BitArray array1 = ..., array2 = ...;
int distance = TensorPrimitives.HammingBitDistance(array1.AsBytes(), array2.AsBytes());
Alternative Designs
No response
Risks
The BitArray length isn't necessarily divisible by 8. For cases where it isn't, a consumer might need to mask off bits in the final byte, depending on the operation being performed. We would make this very clear in the docs for the method.
BitArray also exposes a settable Length, which could result in a new array being allocated. In that case, the span would be referencing an array that's no longer used by the BitArray. If that's deemed an issue, we could put the API onto CollectionsMarshal instead, moving BitArray down into corelib.
Background and motivation
In order to do custom, high-performance comparisons on the bits in a BitArray, I would like to be able to get a span to the data. This then enables that span to be used in operations like those on TensorPrimitives.
API Proposal
namespace System.Collections; public class BitArray { + public ReadOnlySpan<byte> AsBytes(); }API Usage
Alternative Designs
No response
Risks
The BitArray length isn't necessarily divisible by 8. For cases where it isn't, a consumer might need to mask off bits in the final byte, depending on the operation being performed. We would make this very clear in the docs for the method.
BitArray also exposes a settable Length, which could result in a new array being allocated. In that case, the span would be referencing an array that's no longer used by the BitArray. If that's deemed an issue, we could put the API onto CollectionsMarshal instead, moving BitArray down into corelib.