Skip to content

Use Unsafe.BitCast in System.Array #116404

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

Merged
merged 2 commits into from
Jun 8, 2025
Merged

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Jun 7, 2025

Doesn't depend upon implementation specific behaviour, i.e. that data is properly aligned.

Split from #113293.

Doesn't depend upon implementation specific behaviour, i.e. that data is properly aligned.

Split from dotnet#113293.
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 7, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 7, 2025
@@ -949,7 +949,7 @@ public static int BinarySearch(Array array, int index, int length, object? value
return (result >= 0) ? (index + result) : ~(index + ~result);

static int GenericBinarySearch<T>(Array array, int adjustedIndex, int length, object value) where T : struct, IComparable<T>
=> UnsafeArrayAsSpan<T>(array, adjustedIndex, length).BinarySearch(Unsafe.As<byte, T>(ref value.GetRawData()));
=> UnsafeArrayAsSpan<T>(array, adjustedIndex, length).BinarySearch(Unsafe.ReadUnaligned<T>(ref value.GetRawData()));
Copy link
Member

Choose a reason for hiding this comment

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

aren't array's elements always aligned to T 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.

doesn't hurt to be obviously correct, also it's 5 bytes less IL

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 wasn't sure about the data alignment of e.g. double on all 32 bit platforms.

Copy link
Member

Choose a reason for hiding this comment

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

I wasn't sure about the data alignment of e.g. double on all 32 bit platforms.

Good point, I guess since #115985 it's not guaranteed

Copy link
Member

Choose a reason for hiding this comment

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

doesn't hurt to be obviously correct, also it's 5 bytes less IL

The problem of using ReadUnaligned where it's not needed is a theoretical serious performance regression on platforms where ReadUnaligned is implemented using memcpy-like routine (due to slow/faulty unaligned access)

Copy link
Member

Choose a reason for hiding this comment

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

There is no alignment problem here.

doesn't hurt to be obviously correct

I think it is confusing to use ReadUnaligned in place when there are no alignment issues.

I guess since #115985 it's not guaranteed

doubles are not 8-byte aligned on x86 and you do not need to worry about it. Regular double loads cannot use instructions that except 8-byte alignment on x86.

Copy link
Member

Choose a reason for hiding this comment

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

Could you please revert these unnecessary ReadUnaligned? The BitCast changes are good.

@xtqqczze xtqqczze changed the title Replace Unsafe.As in System.Array Use Unsafe.BitCast in System.Array Jun 7, 2025
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks

@jkotas
Copy link
Member

jkotas commented Jun 8, 2025

/ba-g timeouts

@jkotas jkotas merged commit a7f68b8 into dotnet:main Jun 8, 2025
139 of 143 checks passed
@xtqqczze xtqqczze deleted the unsafe-as-array branch June 8, 2025 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates that the PR has been added by a community member needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants