From 110c6253f69359c1a4dbec3bd2cbdc3ad5474ada Mon Sep 17 00:00:00 2001 From: raeperd <41039751+raeperd@users.noreply.github.com> Date: Sat, 21 Sep 2024 10:18:50 +0900 Subject: [PATCH] perf: Optimize code with minimal fields (#6) * perf: Reduce struct field by using key in map * perf: Optimize field using bool instead of int --- analyzer.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/analyzer.go b/analyzer.go index e80dfc5..2cf3294 100644 --- a/analyzer.go +++ b/analyzer.go @@ -39,23 +39,22 @@ 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) } } @@ -63,7 +62,6 @@ func run(pass *analysis.Pass) (any, error) { } type structType struct { - recv string - numStarMethod int - numTypeMethod int + starUsed bool + typeUsed bool }