-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Use Unsafe.As
for generic cast
#118684
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
Use Unsafe.As
for generic cast
#118684
Conversation
Tagging subscribers to this area: @dotnet/area-system-collections |
@@ -161,7 +161,7 @@ private void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> enumerable) | |||
} | |||
else if (enumerable.GetType() == typeof(List<KeyValuePair<TKey, TValue>>)) | |||
{ | |||
span = CollectionsMarshal.AsSpan((List<KeyValuePair<TKey, TValue>>)enumerable); | |||
span = CollectionsMarshal.AsSpan(Unsafe.As<List<KeyValuePair<TKey, TValue>>>(enumerable)); |
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.
Thanks, but I do not want to introduce unsafe code for things like this. If it's material, the cast should be elided by the JIT, if it's not already.
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.
@EgorBo The diffs show the cast is not elided.
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.
Thanks, but I do not want to introduce unsafe code for things like this.
We already use the unsafe method CollectionsMarshal.AsSpan
here.
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.
Thanks, but I do not want to introduce unsafe code for things like this.
We already use the unsafe method
CollectionsMarshal.AsSpan
here.
That doesn't mean we need to add more. They're also "unsafe" in very different ways. Unsafe.As lets you completely break type safety. AsSpan is on the marshal class only because there's a potential for someone to make changes that get lost if they get the span, then edit the list in a way that causes it to grow, and then edit the span; it's not a memory safety issue.
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.
@EgorBo The diffs show the cast is not elided.
Yes, this is a known issue, I can't find the exact github issue, but I do remember it exists. Basically, the same repro I mentioned here: #118655 (comment)
I'll try to fix it in .NET 11.0. In case if you're looking for an inspiration on what to contribute to dotnet/runtime repo, anything that replaces unsafe code with safe (ideally, with zero overhead) would be more than appreciated 🙂
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.
For reference, this is the issue:
Improvements diffs expected.
Related: #118655