1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package fieldalignment defines an Analyzer that detects structs that would use less
// memory if their fields were sorted.
package fieldalignment
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/token"
"go/types"
"sort"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const Doc = `find structs that would use less memory if their fields were sorted
This analyzer find structs that can be rearranged to use less memory, and provides
a suggested edit with the most compact order.
Note that there are two different diagnostics reported. One checks struct size,
and the other reports "pointer bytes" used. Pointer bytes is how many bytes of the
object that the garbage collector has to potentially scan for pointers, for example:
struct { uint32; string }
have 16 pointer bytes because the garbage collector has to scan up through the string's
inner pointer.
struct { string; *uint32 }
has 24 pointer bytes because it has to scan further through the *uint32.
struct { string; uint32 }
has 8 because it can stop immediately after the string pointer.
Be aware that the most compact order is not always the most efficient.
In rare cases it may cause two variables each updated by its own goroutine
to occupy the same CPU cache line, inducing a form of memory contention
known as "false sharing" that slows down both goroutines.
Unlike most analyzers, which report likely mistakes, the diagnostics
produced by fieldanalyzer very rarely indicate a significant problem,
so the analyzer is not included in typical suites such as vet or
gopls. Use this standalone command to run it on your code:
$ go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
$ fieldalignment [packages]
`
var Analyzer = &analysis.Analyzer{
Name: "fieldalignment",
Doc: Doc,
URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment",
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.StructType)(nil),
}
inspect.Preorder(nodeFilter, func(node ast.Node) {
var s *ast.StructType
var ok bool
if s, ok = node.(*ast.StructType); !ok {
return
}
if tv, ok := pass.TypesInfo.Types[s]; ok {
fieldalignment(pass, s, tv.Type.(*types.Struct))
}
})
return nil, nil
}
var unsafePointerTyp = types.Unsafe.Scope().Lookup("Pointer").(*types.TypeName).Type()
func fieldalignment(pass *analysis.Pass, node *ast.StructType, typ *types.Struct) {
wordSize := pass.TypesSizes.Sizeof(unsafePointerTyp)
maxAlign := pass.TypesSizes.Alignof(unsafePointerTyp)
s := gcSizes{wordSize, maxAlign}
optimal, indexes := optimalOrder(typ, &s)
optsz, optptrs := s.Sizeof(optimal), s.ptrdata(optimal)
var message string
if sz := s.Sizeof(typ); sz != optsz {
message = fmt.Sprintf("struct of size %d could be %d", sz, optsz)
} else if ptrs := s.ptrdata(typ); ptrs != optptrs {
message = fmt.Sprintf("struct with %d pointer bytes could be %d", ptrs, optptrs)
} else {
// Already optimal order.
return
}
// Flatten the ast node since it could have multiple field names per list item while
// *types.Struct only have one item per field.
// TODO: Preserve multi-named fields instead of flattening.
var flat []*ast.Field
for _, f := range node.Fields.List {
// TODO: Preserve comment, for now get rid of them.
// See https://github.com/golang/go/issues/20744
f.Comment = nil
f.Doc = nil
if len(f.Names) <= 1 {
flat = append(flat, f)
continue
}
for _, name := range f.Names {
flat = append(flat, &ast.Field{
Names: []*ast.Ident{name},
Type: f.Type,
})
}
}
// Sort fields according to the optimal order.
var reordered []*ast.Field
for _, index := range indexes {
reordered = append(reordered, flat[index])
}
newStr := &ast.StructType{
Fields: &ast.FieldList{
List: reordered,
},
}
// Write the newly aligned struct node to get the content for suggested fixes.
var buf bytes.Buffer
if err := format.Node(&buf, token.NewFileSet(), newStr); err != nil {
return
}
pass.Report(analysis.Diagnostic{
Pos: node.Pos(),
End: node.Pos() + token.Pos(len("struct")),
Message: message,
SuggestedFixes: []analysis.SuggestedFix{{
Message: "Rearrange fields",
TextEdits: []analysis.TextEdit{{
Pos: node.Pos(),
End: node.End(),
NewText: buf.Bytes(),
}},
}},
})
}
func optimalOrder(str *types.Struct, sizes *gcSizes) (*types.Struct, []int) {
nf := str.NumFields()
type elem struct {
index int
alignof int64
sizeof int64
ptrdata int64
}
elems := make([]elem, nf)
for i := 0; i < nf; i++ {
field := str.Field(i)
ft := field.Type()
elems[i] = elem{
i,
sizes.Alignof(ft),
sizes.Sizeof(ft),
sizes.ptrdata(ft),
}
}
sort.Slice(elems, func(i, j int) bool {
ei := &elems[i]
ej := &elems[j]
// Place zero sized objects before non-zero sized objects.
zeroi := ei.sizeof == 0
zeroj := ej.sizeof == 0
if zeroi != zeroj {
return zeroi
}
// Next, place more tightly aligned objects before less tightly aligned objects.
if ei.alignof != ej.alignof {
return ei.alignof > ej.alignof
}
// Place pointerful objects before pointer-free objects.
noptrsi := ei.ptrdata == 0
noptrsj := ej.ptrdata == 0
if noptrsi != noptrsj {
return noptrsj
}
if !noptrsi {
// If both have pointers...
// ... then place objects with less trailing
// non-pointer bytes earlier. That is, place
// the field with the most trailing
// non-pointer bytes at the end of the
// pointerful section.
traili := ei.sizeof - ei.ptrdata
trailj := ej.sizeof - ej.ptrdata
if traili != trailj {
return traili < trailj
}
}
// Lastly, order by size.
if ei.sizeof != ej.sizeof {
return ei.sizeof > ej.sizeof
}
return false
})
fields := make([]*types.Var, nf)
indexes := make([]int, nf)
for i, e := range elems {
fields[i] = str.Field(e.index)
indexes[i] = e.index
}
return types.NewStruct(fields, nil), indexes
}
// Code below based on go/types.StdSizes.
type gcSizes struct {
WordSize int64
MaxAlign int64
}
func (s *gcSizes) Alignof(T types.Type) int64 {
// For arrays and structs, alignment is defined in terms
// of alignment of the elements and fields, respectively.
switch t := T.Underlying().(type) {
case *types.Array:
// spec: "For a variable x of array type: unsafe.Alignof(x)
// is the same as unsafe.Alignof(x[0]), but at least 1."
return s.Alignof(t.Elem())
case *types.Struct:
// spec: "For a variable x of struct type: unsafe.Alignof(x)
// is the largest of the values unsafe.Alignof(x.f) for each
// field f of x, but at least 1."
max := int64(1)
for i, nf := 0, t.NumFields(); i < nf; i++ {
if a := s.Alignof(t.Field(i).Type()); a > max {
max = a
}
}
return max
}
a := s.Sizeof(T) // may be 0
// spec: "For a variable x of any type: unsafe.Alignof(x) is at least 1."
if a < 1 {
return 1
}
if a > s.MaxAlign {
return s.MaxAlign
}
return a
}
var basicSizes = [...]byte{
types.Bool: 1,
types.Int8: 1,
types.Int16: 2,
types.Int32: 4,
types.Int64: 8,
types.Uint8: 1,
types.Uint16: 2,
types.Uint32: 4,
types.Uint64: 8,
types.Float32: 4,
types.Float64: 8,
types.Complex64: 8,
types.Complex128: 16,
}
func (s *gcSizes) Sizeof(T types.Type) int64 {
switch t := T.Underlying().(type) {
case *types.Basic:
k := t.Kind()
if int(k) < len(basicSizes) {
if s := basicSizes[k]; s > 0 {
return int64(s)
}
}
if k == types.String {
return s.WordSize * 2
}
case *types.Array:
return t.Len() * s.Sizeof(t.Elem())
case *types.Slice:
return s.WordSize * 3
case *types.Struct:
nf := t.NumFields()
if nf == 0 {
return 0
}
var o int64
max := int64(1)
for i := 0; i < nf; i++ {
ft := t.Field(i).Type()
a, sz := s.Alignof(ft), s.Sizeof(ft)
if a > max {
max = a
}
if i == nf-1 && sz == 0 && o != 0 {
sz = 1
}
o = align(o, a) + sz
}
return align(o, max)
case *types.Interface:
return s.WordSize * 2
}
return s.WordSize // catch-all
}
// align returns the smallest y >= x such that y % a == 0.
func align(x, a int64) int64 {
y := x + a - 1
return y - y%a
}
func (s *gcSizes) ptrdata(T types.Type) int64 {
switch t := T.Underlying().(type) {
case *types.Basic:
switch t.Kind() {
case types.String, types.UnsafePointer:
return s.WordSize
}
return 0
case *types.Chan, *types.Map, *types.Pointer, *types.Signature, *types.Slice:
return s.WordSize
case *types.Interface:
return 2 * s.WordSize
case *types.Array:
n := t.Len()
if n == 0 {
return 0
}
a := s.ptrdata(t.Elem())
if a == 0 {
return 0
}
z := s.Sizeof(t.Elem())
return (n-1)*z + a
case *types.Struct:
nf := t.NumFields()
if nf == 0 {
return 0
}
var o, p int64
for i := 0; i < nf; i++ {
ft := t.Field(i).Type()
a, sz := s.Alignof(ft), s.Sizeof(ft)
fp := s.ptrdata(ft)
o = align(o, a)
if fp != 0 {
p = o + fp
}
o += sz
}
return p
}
panic("impossible")
}
|