Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve System.Collections.BitArray #115069

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 5 commits into
base: main
Choose a base branch
from
Open

Conversation

tfenise
Copy link
Contributor

@tfenise tfenise commented Apr 25, 2025

Use xplat SIMD intrinsics in BitArray.CopyTo. #114818 (comment)

Other minor improvements.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Apr 25, 2025
Copy link
Contributor

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

@@ -348,23 +348,23 @@ public BitArray And(BitArray value)
ref int right = ref MemoryMarshal.GetArrayDataReference<int>(valueArray);
if (Vector512.IsHardwareAccelerated && (uint)count >= Vector512<int>.Count)
{
for (; i < (uint)count - (Vector512<int>.Count - 1u); i += (uint)Vector512<int>.Count)
for (; i < (uint)count - ((uint)Vector512<int>.Count - 1u); i += (uint)Vector512<int>.Count)
Copy link
Member

Choose a reason for hiding this comment

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

why is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mixing int and uint causes both to be extended to long, and 64bit arithmetic is slow in 32bit processes.

Copy link
Member

Choose a reason for hiding this comment

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

The question is more "why use uint at all here"?

Just use the standard int i = 0 instead and if then do LoadUnsafe(ref left, (uint)i) if you want to zero-extend the load.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems #33749 decided to use uint i instead of int i to avoid potential overflows. I don't see any potential overflow to necessitate uint i in the current version of BitArray.cs, though. Should I change all those uint i to int i?

Copy link
Member

Choose a reason for hiding this comment

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

There shouldn't be any risk to overflow even in the previous implementation. The loop should never be incrementing past Length in the first place.

Using int tends to work better for the JIT and unlocks more overall optimizations. It will also improve readability here.

Comment on lines +888 to +893
else
{
shuffled = Vector512.Shuffle(bits.AsByte(),
Vector512.Create(0x03030303_03030303, 0x0A0A0A0A_0A0A0A0A, 0x11111111_11111111, 0x18181818_18181818,
0x27272727_27272727, 0x2E2E2E2E_2E2E2E2E, 0x35353535_35353535, 0x3C3C3C3C_3C3C3C3C).AsByte());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there's any big endian platform that's supported and also supports vectors, I'd recommend just guarding the SIMD paths on LE here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have already wrote these lines, so why not keep them

Copy link
Contributor

Choose a reason for hiding this comment

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

I have already wrote these lines, so why not keep them

Cause having dead, untested code both complicates the algorithm and can cause unexpected bugs if those paths get enabled later.

Copy link
Contributor Author

@tfenise tfenise Apr 28, 2025

Choose a reason for hiding this comment

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

The original AdvSimd.Arm64 path also has a big endian sub-path.

can cause unexpected bugs if those paths get enabled later.

If any big endian platform with vectors gets supported, all these libraries that come with the runtime itself will be recompiled and their tests will be run. It will also be a use case and test case for VectorXXX.Shuffle.

@tfenise tfenise marked this pull request as ready for review May 1, 2025 17:00
Vector256<byte> isFalse = Vector256.Equals(vector, Vector256<byte>.Zero);

uint result = isFalse.ExtractMostSignificantBits();
m_array[i / 32u] = (int)(~result);
m_array[i / 32] = (int)~(isFalse.ExtractMostSignificantBits());
}
}
else if (Vector128.IsHardwareAccelerated)
Copy link
Contributor

Choose a reason for hiding this comment

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

All the other similar code dropped the else for this branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here, the loop conditions for Vector256 and Vector128 paths are basically same (i <= values.Length - 32), so there is no point executing the Vector128 path if the Vector256 path is already executed.

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.

5 participants