V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
docxs
V2EX  ›  Go 编程语言

cacheline 填充的意义

  •  
  •   docxs · 2022-09-02 21:20:28 +08:00 · 1146 次点击
    这是一个创建于 607 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这种填充方式能解决 false sharing 吗,

    type PaddedStruct struct {
        _ CacheLinePad
        n int
    }
    
    type CacheLinePad struct {
        _ [CacheLinePadSize]byte
    }
    
    const CacheLinePadSize = 64
    

    CacheLinePad 后面的字段,是怎么避免和其他结构体不共享一个 Cache 块了,把 CacheLinePad 放在第一个字段,只能避免和它前面的数据不在一个 cache 块吧,是不是应该这样

    type PaddedStruct struct {
        _ CacheLinePad
        n int
        _ CacheLinePad
    }
    
    10 条回复    2022-09-04 19:26:17 +08:00
    1423
        1
    1423  
       2022-09-02 23:19:51 +08:00
    On ARM, 386, and 32-bit MIPS, ........ The first word in an allocated struct, array, or slice; in a global variable; or in a local variable can be relied upon to be 64-bit aligned.

    https://pkg.go.dev/sync/atomic
    1423
        2
    1423  
       2022-09-02 23:22:44 +08:00
    啊,请忽略上条
    qwe678
        3
    qwe678  
       2022-09-03 12:38:11 +08:00
    struct PaddedStruct {
    int a;
    char cache_line_pad[64];
    int b;
    };
    docxs
        4
    docxs  
    OP
       2022-09-03 16:38:46 +08:00 via iPhone
    @qwe678 这个结构体的 a 会不会和另一个结构体的尾部合成一个 cache 块了
    lysS
        5
    lysS  
       2022-09-04 11:15:30 +08:00
    @docxs #4 当然会,和 struct 无关,只关心是否两个变量是否同在一个 Cache Block 里面
    lysS
        6
    lysS  
       2022-09-04 11:30:28 +08:00
    我测出来了,性能差了 3 倍以上,https://go.dev/play/p/WF-CoUIe4bd

    goos: windows
    goarch: amd64
    pkg: btest
    cpu: 11th Gen Intel(R) Core(TM) i5-11320H @ 3.20GHz
    BenchmarkD1
    BenchmarkD1-8
    582 1844166 ns/op 66 B/op 3 allocs/op
    BenchmarkD2
    BenchmarkD2-8
    2710 387049 ns/op 59 B/op 3 allocs/op
    BenchmarkC1
    BenchmarkC1-8
    583 1974648 ns/op 53 B/op 2 allocs/op
    BenchmarkC2
    BenchmarkC2-8
    2938 396749 ns/op 42 B/op 2 allocs/op
    PASS
    ok btest 4.985s
    lysS
        7
    lysS  
       2022-09-04 11:35:29 +08:00
    docxs
        8
    docxs  
    OP
       2022-09-04 15:54:08 +08:00 via iPhone
    @lysS 嗯,我的意思是,要解决一个 struct 中的变量和其他 struct 中的变量造成 false sharing ,是不是应该在那个变量的前后都加上 cachelinepad ,只在前或后一端加都没法避免,我看很多的文章讲避免 false sharing ,都拿一端填充的例子讲,所以比较疑惑
    lysS
        9
    lysS  
       2022-09-04 18:31:54 +08:00   ❤️ 1
    @docxs #8 理论上是的,但是这种优化不是绝对的,把高频操作的变量分开就可以了
    docxs
        10
    docxs  
    OP
       2022-09-04 19:26:17 +08:00 via iPhone
    @lysS 好的,多谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   916 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:57 · PVG 06:57 · LAX 15:57 · JFK 18:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.