Conversation
|
Tagging subscribers to this area: @tarekgh, @buyaa-n, @krwq Issue Details
|
There was a problem hiding this comment.
This is potential breaking change and performance regression. Encoding.Unicode.GetString does validation of the encoding. It will both break cases where there are binary blobs stored as strings in resources, and also it will make reading of the resources slower.
There was a problem hiding this comment.
I can certainly change this to just to a plain byte-swap on big-endian instead. I was just confused because in this same routine a few lines below, we also do Encoding.Unicode.GetString -- the only difference is whether the input stream is a UnmanagedMemoryStream or not. Shouldn't the two cases be otherwise semantically equivalent?
There was a problem hiding this comment.
There are probably other subtle observable differences betwen the UnmanagedMemoryStream and non-UnmanagedMemoryStream paths. I do not think it is worth trying to fix them.
(The UnmanagedMemoryStream path is taken in 99+% of ResourceReader use cases. If we were to fix the differences, it would be probably better to snap to UnmanagedMemoryStream behavior, not the other way around.)
There was a problem hiding this comment.
Patch updated to leave the LE path unchanges and just do byte swaps on BE. I'm not sure the StringBuilder approach is really the most efficient way here -- if you have any better suggestion, please let me know.
There was a problem hiding this comment.
This would be better than StringBuilder:
char[] arr = new char[byteLen / 2];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = (char)BinaryPrimitives.ReverseEndianness((short)charPtr[i])
}
s = new string(arr);
Also, I think it would also fine to just call Encoding.Unicode.GetString on the BE path. It would not be bug-for-bug compatible with the LE path, but it is unlikely to ever bubble up as a problem for the BE platforms.
There was a problem hiding this comment.
Patch updated as per your suggestion, thanks!
* Byte-swap characters read from memory-mapped resources on big-endian systems
Use Encoding.Unicode.GetString when accessing memory-mapped resources
(just as is already done for resource files read from a stream)
Performs byte-swaps on big-endian systems, and also eliminates a
semantic difference between memory-mapped and stream resources
w.r.t. handling surrogate characters