Skip to content

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
@github-actions github-actions bot locked and limited conversation to collaborators Jul 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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