*: 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 <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2024-08-30 15:07:52 +03:00
parent 357bc76882
commit a50723ff72
3 changed files with 12 additions and 15 deletions

View file

@ -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")

View file

@ -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)

View file

@ -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")
}