Skip to content
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

stackalloc for BytesToDecimal #31190

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/EFCore/Storage/ValueConversion/NumberToBytesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,22 @@ private static readonly MethodInfo ToDecimalMethod

private 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