Skip to content

Commit 4a91a9a

Browse files
Cuong Manh Lecuonglm
authored andcommitted
Show percentage savings in report
Fixes orijtech#23
1 parent f71694b commit 4a91a9a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

structslop.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
6969
curPkgPath := pass.Pkg.Path()
7070
optStyp := formatStruct(r.suggestedStruct, curPkgPath)
7171
msg := fmt.Sprintf(
72-
"struct has size %d (size class %d), could be %d (size class %d), rearrange to %s for optimal size",
72+
"struct has size %d (size class %d), could be %d (size class %d), rearrange to %s for optimal size (%.2f%% savings)",
7373
r.oldGcSize,
7474
r.oldRuntimeSize,
7575
r.newGcSize,
7676
r.newRuntimeSize,
7777
optStyp,
78+
r.savings(),
7879
)
7980
pass.Report(analysis.Diagnostic{
8081
Pos: n.Pos(),
@@ -98,6 +99,10 @@ func (r result) sloppy() bool {
9899
return r.oldRuntimeSize > r.newRuntimeSize
99100
}
100101

102+
func (r result) savings() float64 {
103+
return float64(r.oldRuntimeSize-r.newRuntimeSize) / float64(r.oldRuntimeSize) * 100
104+
}
105+
101106
func checkSloppy(pass *analysis.Pass, origStruct *types.Struct) result {
102107
optStruct := optimalStructArrangement(pass.TypesSizes, origStruct)
103108
r := result{

testdata/src/struct/p.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type s2 struct {
2727
j int
2828
}
2929

30-
type s3 struct { // want `struct has size 24 \(size class 32\), could be 16 \(size class 16\), rearrange to struct{y uint64; x uint32; z uint32} for optimal size`
30+
type s3 struct { // want `struct has size 24 \(size class 32\), could be 16 \(size class 16\), rearrange to struct{y uint64; x uint32; z uint32} for optimal size \(50.00% savings\)`
3131
x uint32
3232
y uint64
3333
z uint32
3434
}
3535

36-
type s4 struct { // want `struct has size 40 \(size class 48\), could be 24 \(size class 32\), rearrange to struct{_ \[0\]func\(\); i1 int; i2 int; a3 \[3\]bool; b bool} for optimal size`
36+
type s4 struct { // want `struct has size 40 \(size class 48\), could be 24 \(size class 32\), rearrange to struct{_ \[0\]func\(\); i1 int; i2 int; a3 \[3\]bool; b bool} for optimal size \(33.33% savings\)`
3737
b bool
3838
i1 int
3939
i2 int
@@ -56,15 +56,15 @@ type s6 struct { // should be good, see #16
5656
index uintptr
5757
}
5858

59-
type s7 struct { // want `struct has size 40 \(size class 48\), could be 32 \(size class 32\), rearrange to struct{y uint64; t \*httptest.Server; w uint64; x uint32; z uint32} for optimal size`
59+
type s7 struct { // want `struct has size 40 \(size class 48\), could be 32 \(size class 32\), rearrange to struct{y uint64; t \*httptest.Server; w uint64; x uint32; z uint32} for optimal size \(33.33% savings\)`
6060
x uint32
6161
y uint64
6262
t *httptest.Server
6363
z uint32
6464
w uint64
6565
}
6666

67-
type s8 struct { // want `struct has size 40 \(size class 48\), could be 32 \(size class 32\), rearrange to struct{y uint64; t \*s; w uint64; x uint32; z uint32} for optimal size`
67+
type s8 struct { // want `struct has size 40 \(size class 48\), could be 32 \(size class 32\), rearrange to struct{y uint64; t \*s; w uint64; x uint32; z uint32} for optimal size \(33.33% savings\)`
6868
x uint32
6969
y uint64
7070
t *s

0 commit comments

Comments
 (0)