Skip to content

Commit c10541a

Browse files
committed
go/analysis/passes/fieldalignment: document "false sharing"
...and don't claim that the most compact field order is optimal. The exception is relatively obscure, but the fact that it exists is important because it means it is not safe to apply the code transformation unconditionally. Change-Id: I391fbc1872b578d5340dd7c8fded48be30b820e0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/415057 Reviewed-by: Robert Findley <[email protected]>
1 parent 7743d1d commit c10541a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

go/analysis/passes/fieldalignment/fieldalignment.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
const Doc = `find structs that would use less memory if their fields were sorted
2424
2525
This analyzer find structs that can be rearranged to use less memory, and provides
26-
a suggested edit with the optimal order.
26+
a suggested edit with the most compact order.
2727
2828
Note that there are two different diagnostics reported. One checks struct size,
2929
and the other reports "pointer bytes" used. Pointer bytes is how many bytes of the
@@ -41,6 +41,11 @@ has 24 pointer bytes because it has to scan further through the *uint32.
4141
struct { string; uint32 }
4242
4343
has 8 because it can stop immediately after the string pointer.
44+
45+
Be aware that the most compact order is not always the most efficient.
46+
In rare cases it may cause two variables each updated by its own goroutine
47+
to occupy the same CPU cache line, inducing a form of memory contention
48+
known as "false sharing" that slows down both goroutines.
4449
`
4550

4651
var Analyzer = &analysis.Analyzer{

0 commit comments

Comments
 (0)