这种填充方式能解决 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
}
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 |
2
1423 2022-09-02 23:22:44 +08:00
啊,请忽略上条
|
3
qwe678 2022-09-03 12:38:11 +08:00
struct PaddedStruct {
int a; char cache_line_pad[64]; int b; }; |
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 |
7
lysS 2022-09-04 11:35:29 +08:00
|