Replace Unsafe.As with Unsafe.Read{Write}Unaligned#113293
Conversation
Doesn't depend upon implementation specific behaviour, i.e. that the array data is properly aligned.
| else | ||
| { | ||
| Unsafe.As<byte, bool>(ref destPtr) = true; | ||
| Unsafe.WriteUnaligned(ref destPtr, true); |
There was a problem hiding this comment.
Bools are one byte long so this is not needed; accessing them is always aligned.
There was a problem hiding this comment.
Here and below, can you replace these byte <-> bool with ternary ops and run MihuBot?
Unsafe.As with Unsafe.ReadUnaligned for loadsUnsafe.As with Unsafe.Read{Write}Unaligned
| // If the allocator is null, then we shouldn't allocate and make a copy, | ||
| // we should return the data as the object it currently is. | ||
| return Unsafe.As<byte, object>(ref data); | ||
| return Unsafe.ReadUnaligned<object>(ref data); |
There was a problem hiding this comment.
GC references are not expected to be not-aligned. We can probably do Unsafe.Read here with a debug assert checking the alignment.
Same in other places
There was a problem hiding this comment.
I think we can introduce an internal helper in Unsafe: ReadAligned that checks alignment in Debug.Assert
|
|
||
| case EETypeElementType.Byte: | ||
| result = (ulong)(long)Unsafe.As<byte, byte>(ref pValue); | ||
| result = (ulong)(long)Unsafe.ReadUnaligned<byte>(ref pValue); |
There was a problem hiding this comment.
pValue is already byte, it doesn't need Unsafe. Same for SByte
| return InternalGetCorElementType() switch | ||
| { | ||
| CorElementType.ELEMENT_TYPE_I1 => Unsafe.As<byte, sbyte>(ref data), | ||
| CorElementType.ELEMENT_TYPE_I1 => Unsafe.ReadUnaligned<sbyte>(ref data), |
There was a problem hiding this comment.
same here: sbyte is read from byte with unsafe
| return InternalGetCorElementType() switch | ||
| { | ||
| CorElementType.ELEMENT_TYPE_I1 => Unsafe.As<byte, sbyte>(ref data).GetHashCode(), | ||
| CorElementType.ELEMENT_TYPE_I1 => Unsafe.ReadUnaligned<sbyte>(ref data).GetHashCode(), |
| { | ||
| case CorElementType.ELEMENT_TYPE_I1: | ||
| return Unsafe.As<byte, sbyte>(ref pThisValue).CompareTo(Unsafe.As<byte, sbyte>(ref pTargetValue)); | ||
| return Unsafe.ReadUnaligned<sbyte>(ref pThisValue).CompareTo(Unsafe.ReadUnaligned<sbyte>(ref pTargetValue)); |
| { | ||
| case EETypeElementType.Char: | ||
| result = (ulong)(long)Unsafe.As<byte, char>(ref pValue); | ||
| result = (ulong)(long)Unsafe.ReadUnaligned<char>(ref pValue); |
There was a problem hiding this comment.
The pValue is expected to be aligned properly. Changes in this method are unnecessary deoptimization.
| { | ||
| case EETypeElementType.Single: | ||
| Unsafe.As<byte, float>(ref dstValue) = srcValue; | ||
| Unsafe.WriteUnaligned(ref dstValue, (float)srcValue); |
There was a problem hiding this comment.
Same here. All changes in this method are unnecessary deoptimizations.
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
|
Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it. |
Doesn't depend upon implementation specific behaviour, i.e. that data is properly aligned. Split from dotnet#113293.
Doesn't depend upon implementation specific behaviour, i.e. that data is properly aligned.
For
System.Private.CoreLib