Skip to content
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.BitCast for generic span casting #109136

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Oct 23, 2024

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 23, 2024
xtqqczze

This comment was marked as off-topic.

@xtqqczze
Copy link
Contributor Author

@MihuBot

@huoyaoyuan
Copy link
Member

huoyaoyuan commented Oct 23, 2024

This was problematic for Mono. See #102998 and #103915 . I'm not sure if anything has changed.

@xtqqczze
Copy link
Contributor Author

This was problematic for Mono. See #102998 and #103915 . I'm not sure if anything has changed.

#105150

cc: @fanyang-mono

@xtqqczze
Copy link
Contributor Author

@MihuBot -mono

@MihaZupan
Copy link
Member

The bot doesn't support mono

@xtqqczze xtqqczze marked this pull request as ready for review October 25, 2024 22:43
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

@xtqqczze
Copy link
Contributor Author

@EgorBo Build analysis is green. I do not believe this is problematic for Mono since #105150.

@EgorBo
Copy link
Member

EgorBo commented Dec 23, 2024

@EgorBo Build analysis is green. I do not believe this is problematic for Mono since #105150.

One of our main focuses currently is to reduce unsafe code in the repo (ideally, with zero performance impact, but we might tolerate small regressions in favor of memory safety), see #94941. This change seems to replace one unsafe pattern with another. Or in case of MemoryMarshal.Cast -> Unsafe.Bitcast it seems to be even more unsafe now? Could you please clarify the motivation behind it?

@xtqqczze
Copy link
Contributor Author

@MihuBot

@xtqqczze
Copy link
Contributor Author

One of our main focuses currently is to reduce unsafe code in the repo (ideally, with zero performance impact, but we might tolerate small regressions in favor of memory safety), see #94941. This change seems to replace one unsafe pattern with another. Or in case of MemoryMarshal.Cast -> Unsafe.Bitcast it seems to be even more unsafe now? Could you please clarify the motivation behind it?

Unsafe.BitCast is only being used to cast from a generic type to the actual type, therefore is safe. Additionally, Unsafe.BitCast provides more clarity than MemoryMarshal.Cast or MemoryMarshal.AsBytes.

@stephentoub
Copy link
Member

therefore is safe

All of our uses of "unsafe" constructs are safe; otherwise they'd be bugs to be fixed. The desire to reduce the reliance on such constructs is to reduce the possibility that we might make a mistake, allowing normal runtime protections to be in effect.

MichalPetryka added a commit to MichalPetryka/runtime that referenced this pull request Dec 23, 2024
@MichalPetryka
Copy link
Contributor

I've tried just making MemoryMarshal use BitCast internally but that gives 0 diffs anyway so not sure either of the changes makes sense.

@xtqqczze
Copy link
Contributor Author

I've tried just making MemoryMarshal use BitCast internally but that gives 0 diffs anyway so not sure either of the changes makes sense.

Something similar has already been tried: #100750

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Meta community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants