From a50723ff722986caabe3f93a5d9d506cceb1b33a Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 30 Aug 2024 15:07:52 +0300 Subject: [PATCH] *: use cmp.Or where appropriate It's slightly less efficient (all comparisons are always made), but for strings/ints it's negligible performance difference, while the code looks a tiny bit better. Signed-off-by: Roman Khimov --- cli/query/query.go | 9 ++++----- pkg/core/interop/context.go | 9 ++++----- pkg/smartcontract/manifest/abi.go | 9 ++++----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/cli/query/query.go b/cli/query/query.go index 424b473b5..b8456e9b8 100644 --- a/cli/query/query.go +++ b/cli/query/query.go @@ -200,11 +200,10 @@ func queryCandidates(ctx *cli.Context) error { if !a.Active && b.Active { return -1 } - res := cmp.Compare(a.Votes, b.Votes) - if res != 0 { - return res - } - return a.PublicKey.Cmp(&b.PublicKey) + return cmp.Or( + cmp.Compare(a.Votes, b.Votes), + a.PublicKey.Cmp(&b.PublicKey), + ) }) var res []byte res = fmt.Appendf(res, "Key\tVotes\tCommittee\tConsensus\n") diff --git a/pkg/core/interop/context.go b/pkg/core/interop/context.go index 08934238b..d9ff39426 100644 --- a/pkg/core/interop/context.go +++ b/pkg/core/interop/context.go @@ -363,11 +363,10 @@ func (c *ContractMD) AddMethod(md *MethodAndPrice, desc *manifest.Method) { desc.Safe = md.RequiredFlags&(callflag.All^callflag.ReadOnly) == 0 index, _ := slices.BinarySearchFunc(c.methods, *md, func(e, t MethodAndPrice) int { - res := cmp.Compare(e.MD.Name, t.MD.Name) - if res != 0 { - return res - } - return cmp.Compare(len(e.MD.Parameters), len(t.MD.Parameters)) + return cmp.Or( + cmp.Compare(e.MD.Name, t.MD.Name), + cmp.Compare(len(e.MD.Parameters), len(t.MD.Parameters)), + ) }) c.methods = slices.Insert(c.methods, index, *md) diff --git a/pkg/smartcontract/manifest/abi.go b/pkg/smartcontract/manifest/abi.go index 67e28b073..de85abcae 100644 --- a/pkg/smartcontract/manifest/abi.go +++ b/pkg/smartcontract/manifest/abi.go @@ -63,11 +63,10 @@ func (a *ABI) IsValid() error { } } if sliceHasDups(a.Methods, func(a, b Method) int { - res := cmp.Compare(a.Name, b.Name) - if res != 0 { - return res - } - return cmp.Compare(len(a.Parameters), len(b.Parameters)) + return cmp.Or( + cmp.Compare(a.Name, b.Name), + cmp.Compare(len(a.Parameters), len(b.Parameters)), + ) }) { return errors.New("duplicate method specifications") }