Skip to content

Commit

Permalink
perf: Optimize code with minimal fields (#6)
Browse files Browse the repository at this point in the history
* perf: Reduce struct field by using key in map

* perf: Optimize field using bool instead of int
  • Loading branch information
raeperd authored Sep 21, 2024
1 parent f1f57f8 commit 110c625
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,29 @@ func run(pass *analysis.Pass) (any, error) {
return
}

var st *structType
st, ok = structs[recv.Name]
st, ok := structs[recv.Name]
if !ok {
structs[recv.Name] = &structType{recv: recv.Name}
structs[recv.Name] = &structType{}
st = structs[recv.Name]
}

if isStar {
st.numStarMethod++
st.starUsed = true
} else {
st.numTypeMethod++
st.typeUsed = true
}
})

for _, st := range structs {
if st.numStarMethod > 0 && st.numTypeMethod > 0 {
pass.Reportf(pass.Pkg.Scope().Lookup(st.recv).Pos(), "the methods of %q use pointer receiver and non-pointer receiver.", st.recv)
for recv, st := range structs {
if st.starUsed && st.typeUsed {
pass.Reportf(pass.Pkg.Scope().Lookup(recv).Pos(), "the methods of %q use pointer receiver and non-pointer receiver.", recv)
}
}

return nil, nil
}

type structType struct {
recv string
numStarMethod int
numTypeMethod int
starUsed bool
typeUsed bool
}

0 comments on commit 110c625

Please sign in to comment.