Registry Mirror
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,14 @@ private static unsafe uint Encode(byte* threeBytes, ref byte encodingMap)
uint i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));
uint i3 = Unsafe.Add(ref encodingMap, (IntPtr)(i & 0x3F));

return i0 | (i1 << 8) | (i2 << 16) | (i3 << 24);
if (BitConverter.IsLittleEndian)
{
return i0 | (i1 << 8) | (i2 << 16) | (i3 << 24);
}
else
{
return (i0 << 24) | (i1 << 16) | (i2 << 8) | i3;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -501,7 +508,14 @@ private static unsafe uint EncodeAndPadOne(byte* twoBytes, ref byte encodingMap)
uint i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
uint i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));

return i0 | (i1 << 8) | (i2 << 16) | (EncodingPad << 24);
if (BitConverter.IsLittleEndian)
{
return i0 | (i1 << 8) | (i2 << 16) | (EncodingPad << 24);
}
else
{
return (i0 << 24) | (i1 << 16) | (i2 << 8) | EncodingPad;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -514,7 +528,14 @@ private static unsafe uint EncodeAndPadTwo(byte* oneByte, ref byte encodingMap)
uint i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 10));
uint i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 4) & 0x3F));

return i0 | (i1 << 8) | (EncodingPad << 16) | (EncodingPad << 24);
if (BitConverter.IsLittleEndian)
{
return i0 | (i1 << 8) | (EncodingPad << 16) | (EncodingPad << 24);
}
else
{
return (i0 << 24) | (i1 << 16) | (EncodingPad << 8) | EncodingPad;
}
}

private const uint EncodingPad = '='; // '=', for padding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,28 @@ public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWr
DecomposedGuid guidAsBytes = default;
guidAsBytes.Guid = value;

// When a GUID is blitted, the first three components are little-endian, and the last component is big-endian.
// When a GUID is blitted, the first three components are native-endian, and the last component is big-endian.

// The line below forces the JIT to hoist the bounds check for the following segment.
// The JIT will optimize away the read, but it cannot optimize away the bounds check
// because it may have an observable side effect (throwing).
// We use 8 instead of 7 so that we also capture the dash if we're asked to insert one.

{ _ = destination[8]; }
HexConverter.ToBytesBuffer(guidAsBytes.Byte03, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte02, destination, 2, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte01, destination, 4, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte00, destination, 6, HexConverter.Casing.Lower);
if (BitConverter.IsLittleEndian)
{
HexConverter.ToBytesBuffer(guidAsBytes.Byte03, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte02, destination, 2, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte01, destination, 4, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte00, destination, 6, HexConverter.Casing.Lower);
}
else
{
HexConverter.ToBytesBuffer(guidAsBytes.Byte00, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte01, destination, 2, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte02, destination, 4, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte03, destination, 6, HexConverter.Casing.Lower);
}

if (flags < 0 /* use dash? */)
{
Expand All @@ -133,8 +143,16 @@ public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWr
}

{ _ = destination[4]; }
HexConverter.ToBytesBuffer(guidAsBytes.Byte05, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte04, destination, 2, HexConverter.Casing.Lower);
if (BitConverter.IsLittleEndian)
{
HexConverter.ToBytesBuffer(guidAsBytes.Byte05, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte04, destination, 2, HexConverter.Casing.Lower);
}
else
{
HexConverter.ToBytesBuffer(guidAsBytes.Byte04, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte05, destination, 2, HexConverter.Casing.Lower);
}

if (flags < 0 /* use dash? */)
{
Expand All @@ -147,8 +165,16 @@ public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWr
}

{ _ = destination[4]; }
HexConverter.ToBytesBuffer(guidAsBytes.Byte07, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte06, destination, 2, HexConverter.Casing.Lower);
if (BitConverter.IsLittleEndian)
{
HexConverter.ToBytesBuffer(guidAsBytes.Byte07, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte06, destination, 2, HexConverter.Casing.Lower);
}
else
{
HexConverter.ToBytesBuffer(guidAsBytes.Byte06, destination, 0, HexConverter.Casing.Lower);
HexConverter.ToBytesBuffer(guidAsBytes.Byte07, destination, 2, HexConverter.Casing.Lower);
}

if (flags < 0 /* use dash? */)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ public void Write(long position, decimal value)
int* valuePtr = (int*)(&value);
int flags = *valuePtr;
int hi = *(valuePtr + 1);
int lo = *(valuePtr + 2);
int mid = *(valuePtr + 3);
int lo = *(valuePtr + (BitConverter.IsLittleEndian ? 2 : 3));
int mid = *(valuePtr + (BitConverter.IsLittleEndian ? 3 : 2));

byte* pointer = null;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,7 @@ private unsafe string AllocateStringForNameIndex(int index, out int dataOffset)
if (_ums.Position > _ums.Length - byteLen)
throw new BadImageFormatException(SR.Format(SR.BadImageFormat_ResourcesIndexTooLong, index));

string? s = null;
char* charPtr = (char*)_ums.PositionPointer;

s = new string(charPtr, 0, byteLen / 2);
string s = Encoding.Unicode.GetString(_ums.PositionPointer, byteLen);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the perf impact of this change? Previously it was a memcpy; now it's going through Encoding.Unicode.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In this case, the change looked like a potential correctness (or at least consistency) fix even on little-endian to me. This routine has two code paths, with a special case where the resource file is provided as an UnmanagedMemoryStream. In all other cases, the existing code already used Encoding.Unicode.GetString, only the UMS case used the direct copy.

These are semantically different e.g. in the case when surrogate characters are present. It seems strange to have this semantic difference triggered by simply whether the input stream is a UMS or not.

If this different behavior is indeed intentional, I guess I can instead add a simple byte-swapping loop in the big-endian case.

(As to perf impact, I don't know what would be a good benchmark for that. Looking at the Encoding.Unicode source code, there is a fast path for the case where no surrogates are present, but that is of course still slower than just a memcpy.)


_ums.Position += byteLen;
dataOffset = _store.ReadInt32();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -956,10 +956,9 @@ public unsafe void WriteUTF16(char[] value)
}
else
{
byte[] bytes = Encoding.Unicode.GetBytes(value);
fixed (byte* ptr = &bytes[0])
for (int i = 0; i < value.Length; i++)
{
WriteBytesUnchecked((byte*)ptr, bytes.Length);
WriteUInt16((ushort)value[i]);
}
}
}
Expand Down Expand Up @@ -990,10 +989,9 @@ public unsafe void WriteUTF16(string value)
}
else
{
byte[] bytes = Encoding.Unicode.GetBytes(value);
fixed (byte* ptr = bytes)
for (int i = 0; i < value.Length; i++)
{
WriteBytesUnchecked((byte*)ptr, bytes.Length);
WriteUInt16((ushort)value[i]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ public static unsafe BlobContentId FromHash(byte[] hashCode)
guidPtr[7] = (byte)((guidPtr[7] & 0x0f) | (4 << 4));
guidPtr[8] = (byte)((guidPtr[8] & 0x3f) | (2 << 6));

// the code above assumed the host is little-endian; if this is not the case,
// we need to fix up the first three fields (int, short, short)
if (!BitConverter.IsLittleEndian)
{
byte tmp = guidPtr[0];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use ReverseEndianness oor Read*LittleEndian to better handle this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It appears during CI this code is also built against older .NET versions that do not have the BinaryPrimitives features. This has caused failures with earlier versions of the PR, which is why I went back to open-coding everything ...

guidPtr[0] = guidPtr[3];
guidPtr[3] = tmp;
tmp = guidPtr[1];
guidPtr[1] = guidPtr[2];
guidPtr[2] = tmp;
tmp = guidPtr[4];
guidPtr[4] = guidPtr[5];
guidPtr[5] = tmp;
tmp = guidPtr[6];
guidPtr[6] = guidPtr[7];
guidPtr[7] = tmp;
}

// compute a random-looking stamp from the remaining bits, but with the upper bit set
uint stamp = 0x80000000u | ((uint)hashCode[19] << 24 | (uint)hashCode[18] << 16 | (uint)hashCode[17] << 8 | hashCode[16]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,19 @@ public void WriteUTF16(char[] value)
return;
}

fixed (char* ptr = &value[0])
if (BitConverter.IsLittleEndian)
{
WriteBytesUnchecked((byte*)ptr, value.Length * sizeof(char));
fixed (char* ptr = &value[0])
{
WriteBytesUnchecked((byte*)ptr, value.Length * sizeof(char));
}
}
else
{
for (int i = 0; i < value.Length; i++)
{
WriteUInt16((ushort)value[i]);
}
}
}

Expand All @@ -398,9 +408,19 @@ public void WriteUTF16(string value)
Throw.ArgumentNull(nameof(value));
}

fixed (char* ptr = value)
if (BitConverter.IsLittleEndian)
{
WriteBytesUnchecked((byte*)ptr, value.Length * sizeof(char));
fixed (char* ptr = value)
{
WriteBytesUnchecked((byte*)ptr, value.Length * sizeof(char));
}
}
else
{
for (int i = 0; i < value.Length; i++)
{
WriteUInt16((ushort)value[i]);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ private static unsafe uint CalculateChecksum(IEnumerable<Blob> blobs)

while (ptr < end)
{
checksum = AggregateChecksum(checksum, *(ushort*)ptr);
// little-endian encoding:
checksum = AggregateChecksum(checksum, (ushort)(ptr[1] << 8 | ptr[0]));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here, maybe BinaryPrimitives.ReadUInt16LittleEndian?

ptr += sizeof(ushort);
}
}
Expand Down