Miscellaneous fixes for big-endian systems #48393
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
Changes from all commits
File filter
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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])); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, maybe |
||
| ptr += sizeof(ushort); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)