Skip to content

Commit

Permalink
stackalloc for BytesToDecimal (#31190)
Browse files Browse the repository at this point in the history
  • Loading branch information
Poppyto authored Jan 25, 2024
1 parent 7be0b85 commit 31bd863
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/EFCore/Storage/ValueConversion/NumberToBytesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,23 +299,22 @@ private static readonly MethodInfo ToDecimalMethod
[EntityFrameworkInternal]
public static decimal BytesToDecimal(byte[] bytes)
{
var gotBytes = bytes;
Span<byte> gotBytes = BitConverter.IsLittleEndian ? stackalloc byte[16] : bytes;
if (BitConverter.IsLittleEndian)
{
gotBytes = new byte[16];
Array.Copy(bytes, gotBytes, 16);
Array.Reverse(gotBytes, 0, 4);
Array.Reverse(gotBytes, 4, 4);
Array.Reverse(gotBytes, 8, 4);
Array.Reverse(gotBytes, 12, 4);
bytes.CopyTo(gotBytes);
gotBytes.Slice(0, 4).Reverse();
gotBytes.Slice(4, 4).Reverse();
gotBytes.Slice(8, 4).Reverse();
gotBytes.Slice(12, 4).Reverse();
}

var specialBits = BitConverter.ToUInt32(gotBytes, 0);
var specialBits = BitConverter.ToUInt32(gotBytes);

return new decimal(
BitConverter.ToInt32(gotBytes, 12),
BitConverter.ToInt32(gotBytes, 8),
BitConverter.ToInt32(gotBytes, 4),
BitConverter.ToInt32(gotBytes.Slice(12)),
BitConverter.ToInt32(gotBytes.Slice(8)),
BitConverter.ToInt32(gotBytes.Slice(4)),
(specialBits & 0x80000000) != 0,
(byte)((specialBits & 0x00FF0000) >> 16));
}
Expand Down

0 comments on commit 31bd863

Please sign in to comment.