-
Notifications
You must be signed in to change notification settings - Fork 118
Add OnceNonZeroUsize::get_unchecked
.
#274
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
Conversation
a5beabc
to
5afb427
Compare
Wow. |
It's fine to bump rust-version to 1.70! We support at least 8 past versions! |
The comment needs to be updated to directly address this, and probably other aspects of the memory model: "The most important aspect of this model is that data races are undefined behavior. A data race is defined as conflicting non-synchronized accesses where at least one of the accesses is non-atomic. Here, accesses are conflicting if they affect overlapping regions of memory and at least one of them is a write. They are non-synchronized if neither of them happens-before the other, according to the happens-before order of the memory model." (from https://doc.rust-lang.org/std/sync/atomic/index.html). |
My project doesn't yet, though! :) |
I am fine with this PR applying some work-around to get it work on older compilers, but, in general no promises that future versions of OnceCell will support 1.70.0! While I won't proactively bump to stable-8 just because, I will bump as soon as compat becomes inconvenient |
5afb427
to
5d7ca30
Compare
Comparing https://doc.rust-lang.org/1.74.0/core/sync/atomic/index.html and https://doc.rust-lang.org/1.75.0/core/sync/atomic/index.html shows that 1.75.0 may be the earliest version where we can fully reason about atomics and UB, as that was the first version that attempted to document the memory model for them. |
Also, https://doc.rust-lang.org/1.83.0/core/sync/atomic/index.html is the first version that talks about conflicting accesses. It says
That leads to the question, is a |
According to https://doc.rust-lang.org/1.60.0/core/sync/atomic/struct.AtomicUsize.html, In later documentation, it is clarified that the alignment of |
Yes, I agree with this reasoning, it should be valid to cast a pointer to atomic usize to a pointer to a normal usize |
Just FYI, I was able to implement a workaround to deal with the added reloading when switching from Nonetheless, I am open to continuing to work on this. |
rust-lang/rust#138000 proposes changing the documentation for a future Rust release. However, the question will be whether we have to wait until Rust 1.87 or whatever to rely on that. |
Note that that is a docs-only change. So it does apply retroactively, in a sense. However it'd be good to wait until it actually lands, just to be sure. (In theory it could get backed out again but that seems unlikely.) |
5d7ca30
to
34e2264
Compare
Each call to `OnceNonZeroUsize::get()` results in an atomic load as the compiler will not cache and reuse the result of an atomic load. Provide an analogous `get_unchecked()` that uses a non-atomic load. The compiler will be able to cache and reuse the result to avoid redundant loads.
34e2264
to
15154b0
Compare
OnceNonZeroUsize::get_unchecked
for x86/x86_64.OnceNonZeroUsize::get_unchecked
.
I have updated this PR based on the above comments (thanks RalfJung). |
I filed a follow-up at rust-lang/rust#138246. Also, we still have the issue that |
Note that the MSRV check is failing because of the use of
Did you intend to merge this? |
Life without bors is hard! I did intend to merge this, but I certainly hoped that tests would not fail! I think it's fine to use |
Each call to
OnceNonZeroUsize::get()
results in an atomic load as the compiler will not cache and reuse the result of an atomic load.Provide an analogous
get_unchecked()
that uses a non-atomic load. The compiler will be able to cache and reuse the result to avoid redundant loads.