Is there a way to align data on a cache line boundary?

So, I have recently published my first synchronization primitive on crates.io, and now I would like to optimize it in order to reduce false sharing.

This requires aligning some parts of my data structures on a cache line boundary, which is obviously a hardware dependent process. As with any hardware dependent optimization, I would definitely love my compiler friend to lend me a hand here, so as to make my library portable to current and future hardware architectures with minimal code modifications.

Does Rust/rustc offer stable support for this? Or even nightly support? Otherwise, how should I go about requesting it upstream?

There's an accepted RFC adding a #[repr(align = "N)] attribute to control alignment requirements: https://github.com/rust-lang/rfcs/blob/master/text/1358-repr-align.md. From looking at the tracking issue, it hasn't yet been implemented, but sounds like it's getting close?

2 Likes

This could be one half of what I want, the other half being a way to query the compiler for the CPU cache line size on the target architecture (think std::hardware_destructive_interference_size in C++17). I see that there is also an RFC proposal for this one, though it doesn't seem to have gotten very far yet: https://github.com/rust-lang/rfcs/issues/1756.

Because @HadrienG is to polite to advertise it himself, the "first crate" is triple buffer, discussed here on this forum.