-
Notifications
You must be signed in to change notification settings - Fork 18
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
Replace UnsafeCell with MaybeUnit #53
Conversation
Thanks for taking the time to investigate and create this PR. We will have to also add a Drop implementation with |
This leads to slightly better code generation in some cases where the mutex contents are accessed multiple times in a row.
8cb9ec9
to
f1e3b70
Compare
I'm not sure we should do this. The only advantage AFAICT is better codegen when the contents are inmutable. I.e. When the mutex content does have interior mutability (like So there's no codegen improvements for the way Mutex is actually used in the wild, is there? and there are a few downsides:
|
f1e3b70
to
ea9408d
Compare
It can actually improve the generated code in case In that case, the compiler can sometimes avoid a load instruction: https://godbolt.org/z/x8j4zn4Mb
Indeed I don't know if this is a common pattern.
Sure, it's a workaround to say "don't use niches in here". Is there a better way.
How could it not be sound? UnsafeCell is only needed for interior mutability. For any given T, one of two things must be true:
|
Alternatively, the comment https://github.com/rust-embedded/critical-section/pull/53/files#diff-71560e745278a3bdedc2c5684fa201e28b80663af84e25e82352cb8f5315610bR78-R85 would also fit for the current implementation, just replace |
If the pointer is Copy, this is a rather weak protection, it's easy to bypass by accident with I could imagine a HAL struct that contains the pointer, and has
UnsafeCell also does that :)
yeah, maybe! It's just that UnsafeCell has so much extra spooky magic that scares me. UnsafeCell is needed to obtain a So, is UnsafeCell needed to obtain a |
Sure - exactly as shown in the example in the compiler exporer link:
🤷
I know, but it's not a "better way". It has the same disadvantage that it's meant for something else (interior mutability) that we don't need here, and is only used for this side-effect.
That's essentially the same reason why @thalesfragoso wanted to keep the On the one hand, it's of course wise to not take risks for negligible gains. And even if there was a clear specification that said omitting |
done in #54 |
This leads to slightly better code generation in some cases where the mutex contents are accessed multiple times in a row.
Closes: #52