Add hint::spin_loop() into Atomic*::*update() - #159121
Conversation
`Atomic*::update()` and `Atomic*::try_update()` are implemented using a CAS loop and thus spin loop hint seems appropriate here.
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @aapoalas (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
Thanks for the pull request! I'd argue against this: the CAS will only fail if the value is concurrently being changed but That's also why e.g. |
|
Ah... So |
|
I was under the impression that Either way, I would expect that if these shouldn't have spin-loop hints, there should be a comment explaining why. |
And probably a doc comment explaining that you should not use this if you do need a spin loop hint. |
|
Hmm. To me this seems reasonable, but if it's also not normally used then it does sound like Rust shouldn't add it either... I feel like I don't have the expertise to make this decision alone so I'll nominate for libs. |
|
I agree with @joboet and @ChayimFriedman2. The typical/expected use of It’s theoretically possible to contort |
|
copied from the private zulip thread since I realized my comment would be more useful when it's public: from what I understand, the spin loop hint is to tell the CPU that it'll be spinning waiting until the value in memory is changed by another CPU, so doing lots of loads really fast doesn't really help since it uses more power and won't make the other CPU change the value in memory any faster. so the hint will make the CPU slow down and check less often instead of e.g. checking the value 4-5 billion times a second. for |
|
I agree, this is not a good idea. The spin loop hint is for the case where the loop fails because another thread has not taken a certain action yet. But for a CAS loop the success/failure scenario is reversed. It exits when no other thread taking an action during the loop. |
|
Thank you for the explanation! Closing this because |
|
Opened #159679 with an attempt to make the docs more clear about this. |
Atomic*::update()andAtomic*::try_update()are implemented using a CAS loop and thus spin loop hint seems appropriate here.Question: are there any relevant benchmarks to run to check that this patch does not regress anything? For example, I see that
try_update()is used to implementRwLock. Are there any benchmarks forRwLock?