Registry Mirror
Skip to content

Conversation

@uweigand

Copy link
Copy Markdown
Contributor
  • 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

@ghost

ghost commented Mar 18, 2021

Copy link
Copy Markdown

Tagging subscribers to this area: @tarekgh, @buyaa-n, @krwq
See info in area-owners.md if you want to be subscribed.

Issue Details
  • 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

Author: uweigand
Assignees: -
Labels:

area-System.Resources

Milestone: -

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.

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.

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.

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?

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.

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.)

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.

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.

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.

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.

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.

Patch updated as per your suggestion, thanks!

* Byte-swap characters read from memory-mapped resources
  on big-endian systems
@jkotas
jkotas merged commit 32689cc into dotnet:main Mar 19, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Apr 18, 2021
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants