Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align struct to cache lines in Rust

Tags:

rust

Assuming I wanted to (ab)use cache coherency to do lock free reads like FaRM, would it be enough to have a struct with a single 64 byte array as data to guarantee that on an architecture with 64 byte cache lines each struct would occupy exactly one cache line?

like image 646
benjumanji Avatar asked Dec 11 '16 19:12

benjumanji


1 Answers

No, that wouldn't guarantee that the alignment was a cache line.

RFC 1358 added the concept of #[repr(align)], allowing the programmer to specify alignment requirements. This attribute was stabilized in Rust 1.25.

For your specific case, you'd use it like:

#[repr(align(64))]
struct Foo {
    value: u8,
}
like image 67
Chris Emerson Avatar answered Nov 10 '22 21:11

Chris Emerson