Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let -v print out the suggested field order #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions maligned.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go/types"
"log"
"sort"
"strings"

"github.com/kisielk/gotool"
"golang.org/x/tools/go/loader"
Expand All @@ -21,6 +22,7 @@ import (
var fset = token.NewFileSet()

func main() {
flagVerbose := flag.Bool("v", false, "verbose: print field order suggestions")
flag.Parse()

importPaths := gotool.ImportPaths(flag.Args())
Expand All @@ -42,15 +44,15 @@ func main() {
for _, file := range pkg.Files {
ast.Inspect(file, func(node ast.Node) bool {
if s, ok := node.(*ast.StructType); ok {
malign(node.Pos(), pkg.Types[s].Type.(*types.Struct))
malign(node.Pos(), pkg.Types[s].Type.(*types.Struct), *flagVerbose)
}
return true
})
}
}
}

func malign(pos token.Pos, str *types.Struct) {
func malign(pos token.Pos, str *types.Struct, verbose bool) {
wordSize := int64(8)
maxAlign := int64(8)
switch build.Default.GOARCH {
Expand All @@ -61,13 +63,30 @@ func malign(pos token.Pos, str *types.Struct) {
}

s := gcSizes{wordSize, maxAlign}
sz, opt := s.Sizeof(str), optimalSize(str, &s)
if sz != opt {
sz := s.Sizeof(str)
opt, fields := optimalSize(str, &s, verbose)
if sz == opt {
return
}
if !verbose {
fmt.Printf("%s: struct of size %d could be %d\n", fset.Position(pos), sz, opt)
return
}
fmt.Printf("%s: struct of size %d could be %d with struct{\n", fset.Position(pos), sz, opt)
var w int
for _, f := range fields {
if n := len(f.Name()); n > w {
w = n
}
}
spaces := strings.Repeat(" ", w)
for _, f := range fields {
fmt.Printf("\t%s%s\t%s,\n", f.Name(), spaces[len(f.Name()):], f.Type().String())
}
fmt.Println("}")
}

func optimalSize(str *types.Struct, sizes *gcSizes) int64 {
func optimalSize(str *types.Struct, sizes *gcSizes, stable bool) (int64, []*types.Var) {
nf := str.NumFields()
fields := make([]*types.Var, nf)
alignofs := make([]int64, nf)
Expand All @@ -78,8 +97,12 @@ func optimalSize(str *types.Struct, sizes *gcSizes) int64 {
alignofs[i] = sizes.Alignof(ft)
sizeofs[i] = sizes.Sizeof(ft)
}
sort.Sort(&byAlignAndSize{fields, alignofs, sizeofs})
return sizes.Sizeof(types.NewStruct(fields, nil))
if stable { // Stable keeps as much of the order as possible, but slower
sort.Stable(&byAlignAndSize{fields, alignofs, sizeofs})
} else {
sort.Sort(&byAlignAndSize{fields, alignofs, sizeofs})
}
return sizes.Sizeof(types.NewStruct(fields, nil)), fields
}

type byAlignAndSize struct {
Expand Down