Live data from Hacker News

From Rust to reality: The hidden journey of fetch_max

questdb.com

11–20 of 62 posts

Re: From Rust to reality: The hidden journey of fetch_max

#11
post #10

Aarch64 does indeed have a proper atomic max, but even on x86-64 you can get a wait-free atomic max as long as you only need to support integers up to 64. In that case you can simply do a `lock or` with 1 In most cases it's even better to just store a maximum per thread separately and loop over all threads once to compute the current maximum if you really need it.

That’s a neat trick, albeit with limited applicability given the very narrow range. Thanks for sharing!

Re: From Rust to reality: The hidden journey of fetch_max

#15

Was this compiled at O0? The generated code looks unnecessarily long-winded - at the very least I would expect the match jump table to get culled to only the Relaxed implementation.

> Note we did not ask rustc to optimize the code. If we did, the compiler would generate more efficient assembly: No spills to the stack, fewer jumps, no dispatch on memory ordering, etc. But I wanted to keep the output as close to the original IR as possible to make it easier to follow.

RTFA

Re: From Rust to reality: The hidden journey of fetch_max

#18
post #8
post #4

Hi, author here. My superpower is spending unreasonable amounts of time researching things with no practical purpose. Occasionally I blog about it - as a warning to others.

I liked the article. I saw your PS that we added it to the working draft for c++26, we also made it part of OpenMP as of 5.0 I think. It’s sometimes a hardware atomic like on arm, but what made the case was that it’s common to implement it sub-optimally even on x86 or LL-SC architectures. Often the generic cas loop gets used, like in your lambda example, but it lacks an early cutout since you can ignore any input val…

Curious: even with hardware atomics, wouldn't it be a good idea to first perform a non-atomic load to check for whether the store might be necessary (which would require the cache line to be locked), then only run the atomic max if it might change the value?

Re: From Rust to reality: The hidden journey of fetch_max

#19
Related: fetch_max is an instance of what the following SPAA 2013 paper calls an atomic "priority update" or atomic "write-with-max". This type of atomic operation can have much lower contention than its counterparts like atomic increment.

https://doi.org/10.1145/2486159.2486189 https://jshun.csail.mit.edu/contention.pdf

Post reply on HN