-
Notifications
You must be signed in to change notification settings - Fork 27
/
structslop.go
302 lines (272 loc) · 7.65 KB
/
structslop.go
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
// Copyright 2020 Orijtech, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package structslop
import (
"bytes"
"fmt"
"go/ast"
"go/build"
"go/format"
"go/parser"
"go/token"
"go/types"
"os"
"sort"
"strings"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
var (
includeTestFiles bool
verbose bool
apply bool
generated bool
)
func init() {
Analyzer.Flags.BoolVar(&includeTestFiles, "include-test-files", includeTestFiles, "also check test files")
Analyzer.Flags.BoolVar(&verbose, "verbose", verbose, "print all information, even when struct is not sloppy")
Analyzer.Flags.BoolVar(&apply, "apply", apply, "apply suggested fixes (using -fix won't work)")
Analyzer.Flags.BoolVar(&generated, "generated", generated, "report issues in generated code")
}
const Doc = `check for structs that can be rearrange fields to provide for maximum space/allocation efficiency`
// Analyzer describes struct slop analysis function detector.
var Analyzer = &analysis.Analyzer{
Name: "structslop",
Doc: Doc,
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
// Use custom sizes instance, which implements types.Sizes for calculating struct size.
// go/types and gc does not agree about the struct size.
// See https://github.com/golang/go/issues/14909#issuecomment-199936232
pass.TypesSizes = &sizes{
stdSizes: types.SizesFor(build.Default.Compiler, build.Default.GOARCH),
maxAlign: pass.TypesSizes.Alignof(types.Typ[types.UnsafePointer]),
}
dec := decorator.NewDecorator(pass.Fset)
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.File)(nil),
(*ast.StructType)(nil),
}
fileDiags := make(map[string][]byte)
var af *ast.File
var df *dst.File
// Track generated files unless -generated is set.
genFiles := make(map[*token.File]bool)
if !generated {
files:
for _, f := range pass.Files {
for _, c := range f.Comments {
for _, l := range c.List {
if strings.HasPrefix(l.Text, "// Code generated ") && strings.HasSuffix(l.Text, " DO NOT EDIT.") {
file := pass.Fset.File(f.Pos())
genFiles[file] = true
continue files
}
}
}
}
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
file := pass.Fset.File(n.Pos())
if strings.HasSuffix(file.Name(), "_test.go") && !includeTestFiles {
return
}
// Skip generated structs if instructed.
if !generated && genFiles[file] {
return
}
if f, ok := n.(*ast.File); ok {
af = f
df, _ = dec.DecorateFile(af)
return
}
atyp := n.(*ast.StructType)
styp, ok := pass.TypesInfo.Types[atyp].Type.(*types.Struct)
// Type information may be incomplete.
if !ok {
return
}
if !verbose && styp.NumFields() < 2 {
return
}
r := checkSloppy(pass, styp)
if !verbose && !r.sloppy() {
return
}
var buf bytes.Buffer
expr, err := parser.ParseExpr(formatStruct(r.optStruct, pass.Pkg.Path()))
if err != nil {
return
}
if err := format.Node(&buf, token.NewFileSet(), expr.(*ast.StructType)); err != nil {
return
}
var msg string
switch {
case r.oldGcSize == r.newGcSize:
msg = fmt.Sprintf("struct has size %d (size class %d)", r.oldGcSize, r.oldRuntimeSize)
case r.oldGcSize != r.newGcSize:
msg = fmt.Sprintf(
"struct has size %d (size class %d), could be %d (size class %d), optimal fields order:\n%s\n",
r.oldGcSize,
r.oldRuntimeSize,
r.newGcSize,
r.newRuntimeSize,
buf.String(),
)
if r.sloppy() {
msg = fmt.Sprintf(
"struct has size %d (size class %d), could be %d (size class %d), you'll save %.2f%% if you rearrange it to:\n%s\n",
r.oldGcSize,
r.oldRuntimeSize,
r.newGcSize,
r.newRuntimeSize,
r.savings(),
buf.String(),
)
}
}
dtyp := dec.Dst.Nodes[atyp].(*dst.StructType)
fields := make([]*dst.Field, 0, len(r.optIdx))
dummy := &dst.Field{}
for _, f := range dtyp.Fields.List {
fields = append(fields, f)
if len(f.Names) == 0 {
continue
}
for range f.Names[1:] {
fields = append(fields, dummy)
}
}
optFields := make([]*dst.Field, 0, len(r.optIdx))
for _, i := range r.optIdx {
f := fields[i]
if f == dummy {
continue
}
optFields = append(optFields, f)
}
dtyp.Fields.List = optFields
var suggested bytes.Buffer
if err := decorator.Fprint(&suggested, df); err != nil {
return
}
pass.Report(analysis.Diagnostic{
Pos: n.Pos(),
End: n.End(),
Message: msg,
SuggestedFixes: nil,
})
f := pass.Fset.File(n.Pos())
fileDiags[f.Name()] = suggested.Bytes()
})
if !apply {
return nil, nil
}
for fn, content := range fileDiags {
fi, err := os.Open(fn)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to open file: %v", err)
}
st, err := fi.Stat()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to get file stat: %v", err)
}
if err := fi.Close(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to close file: %v", err)
}
if err := os.WriteFile(fn, content, st.Mode()); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to write suggested fix to file: %v", err)
}
}
return nil, nil
}
type result struct {
oldGcSize int64
newGcSize int64
oldRuntimeSize int64
newRuntimeSize int64
optStruct *types.Struct
optIdx []int
}
func (r result) sloppy() bool {
return r.oldRuntimeSize > r.newRuntimeSize
}
func (r result) savings() float64 {
return float64(r.oldRuntimeSize-r.newRuntimeSize) / float64(r.oldRuntimeSize) * 100
}
func mapFieldIdx(s *types.Struct) map[*types.Var]int {
m := make(map[*types.Var]int, s.NumFields())
for i := 0; i < s.NumFields(); i++ {
m[s.Field(i)] = i
}
return m
}
func checkSloppy(pass *analysis.Pass, origStruct *types.Struct) result {
m := mapFieldIdx(origStruct)
optStruct := optimalStructArrangement(pass.TypesSizes, m)
idx := make([]int, optStruct.NumFields())
for i := range idx {
idx[i] = m[optStruct.Field(i)]
}
r := result{
oldGcSize: pass.TypesSizes.Sizeof(origStruct),
newGcSize: pass.TypesSizes.Sizeof(optStruct),
optStruct: optStruct,
optIdx: idx,
}
r.oldRuntimeSize = int64(roundUpSize(uintptr(r.oldGcSize)))
r.newRuntimeSize = int64(roundUpSize(uintptr(r.newGcSize)))
return r
}
func optimalStructArrangement(sizes types.Sizes, m map[*types.Var]int) *types.Struct {
fields := make([]*types.Var, len(m))
for v, i := range m {
fields[i] = v
}
sort.Slice(fields, func(i, j int) bool {
ti, tj := fields[i].Type(), fields[j].Type()
si, sj := sizes.Sizeof(ti), sizes.Sizeof(tj)
if si == 0 && sj != 0 {
return true
}
if sj == 0 && si != 0 {
return false
}
ai, aj := sizes.Alignof(ti), sizes.Alignof(tj)
if ai != aj {
return ai > aj
}
if si != sj {
return si > sj
}
return false
})
return types.NewStruct(fields, nil)
}
func formatStruct(styp *types.Struct, curPkgPath string) string {
qualifier := func(p *types.Package) string {
if p.Path() == curPkgPath {
return ""
}
return p.Name()
}
return types.TypeString(styp, qualifier)
}