Conversation
|
@stephentoub Do you know who would be the appropriate reviewer(s) for this PR? |
There was a problem hiding this comment.
This is allocating an array on every access. If we want to do that for the big endian code path for now, ok, but we can't add such allocation to the little endian code path.
There was a problem hiding this comment.
OK, changed to keep the logic and just handle endianness directly.
There was a problem hiding this comment.
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.
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.)
There was a problem hiding this comment.
Anywhere we're adding allocations that we weren't previously allocating is a red flag. We shouldn't be doing this here.
There was a problem hiding this comment.
Changed to keep the logic on little-endian and just add extra swaps for the big-endian code path.
|
Added a couple reviewers based on areas touched. |
* Base64Encoder needs to take byte order into accout when packing characters into an int. * First three fields of GUID data structure are held in native byte order (not always little-endian). * UnmanagedMemoryAccessor needs to access internal fields of decimal type correctly in native byte order. * When accessing a memory-mapped resource file, strings may need to be converted to native byte order. * The metadata blob writer did not handle writing UTF16 characters correctly on big-endian systems. * Endian bug in PEBuilder checksum computation.
| // we need to fix up the first three fields (int, short, short) | ||
| if (!BitConverter.IsLittleEndian) | ||
| { | ||
| byte tmp = guidPtr[0]; |
There was a problem hiding this comment.
Can we use ReverseEndianness oor Read*LittleEndian to better handle this?
There was a problem hiding this comment.
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 ...
| { | ||
| checksum = AggregateChecksum(checksum, *(ushort*)ptr); | ||
| // little-endian encoding: | ||
| checksum = AggregateChecksum(checksum, (ushort)(ptr[1] << 8 | ptr[0])); |
There was a problem hiding this comment.
Same here, maybe BinaryPrimitives.ReadUInt16LittleEndian?
|
Ping? If there is anything else I can do to make progress here, please let me know. |
|
@uweigand I'd also extract the controversial bit where @stephentoub had perf concerns into a separate PR so that we don't need to block this one over it. |
Next steps are for area reviewers to chime in and sign off. Paging owners: |
|
To better separate concerns and hopefully simplify the review process, I've now split this up into multiple separate PRs: |
Base64Encoder needs to take byte order into accout
when packing characters into an int.
First three fields of GUID data structure are held in
native byte order (not always little-endian).
UnmanagedMemoryAccessor needs to access internal fields
of decimal type correctly in native byte order.
When accessing a memory-mapped resource file, strings
may need to be converted to native byte order.
The metadata blob writer did not handle writing UTF16
characters correctly on big-endian systems.
Endian bug in PEBuilder checksum computation.