false sharing in Rust

How to optimize the program below to run faster?

use std::{thread, sync::Arc};
use std::sync::atomic::AtomicU64;

struct Block {
    d1: AtomicU64,
}

fn worker(blks: Arc<[Block]>, index: usize) {
    for _ in 0..100_000_000 {
        blks[index].d1.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }
}

fn main() {
    let blks = [
        Block { d1: AtomicU64::new(0) },
        Block { d1: AtomicU64::new(1) },
    ];
    let blks = Arc::new(blks);

    let blks1 = blks.clone();
    let blks2 = blks.clone();

    let h1 = thread::spawn(move || worker(blks1, 0));
    let h2 = thread::spawn(move || worker(blks2, 1));

    h1.join().unwrap();
    h2.join().unwrap();
}

The answer is: Change the alignment of Block to 64 bytes.

#[repr(align(64))]
struct Block {
    d1: AtomicU64,
}

Reference

Detect false sharing

perf c2c record

diff between falsesharing.txt and nofalsesharing.txt
image

posted on 2024-08-26 03:11  winter-loo  阅读(4)  评论(0编辑  收藏  举报

导航