Skip to content

perf(crypto): simplify and speed up Address.Compare #4130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contribs/gnodev/pkg/address/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}

// Check if the association already exist
if oldAddr.Compare(addr) == 0 {
if oldAddr.Compare(&addr) == 0 {

Check warning on line 54 in contribs/gnodev/pkg/address/book.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/pkg/address/book.go#L54

Added line #L54 was not covered by tests
return // nothing to do
}

Expand Down Expand Up @@ -87,7 +87,7 @@

// Find the correct place to insert newEntry using binary search.
i := sort.Search(len(entries), func(i int) bool {
return entries[i].Address.Compare(newEntry.Address) >= 0
return entries[i].Address.Compare(&newEntry.Address) >= 0

Check warning on line 90 in contribs/gnodev/pkg/address/book.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/pkg/address/book.go#L90

Added line #L90 was not covered by tests
})

entries = append(entries[:i], append([]Entry{newEntry}, entries[i:]...)...)
Expand Down
8 changes: 4 additions & 4 deletions contribs/gnodev/pkg/address/book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestAdd(t *testing.T) {
t.Run("get by name", func(t *testing.T) {
addrFromName, ok := bk.GetByName("testname")
assert.True(t, ok)
assert.True(t, addrFromName.Compare(testAddr) == 0)
assert.True(t, addrFromName.Compare(&testAddr) == 0)
})

// Add same address with a new name
Expand All @@ -59,7 +59,7 @@ func TestAdd(t *testing.T) {
require.True(t, ok)
addr2, ok := bk.GetByName("testname2")
require.True(t, ok)
assert.True(t, addr1.Compare(addr2) == 0)
assert.True(t, addr1.Compare(&addr2) == 0)
})
}

Expand All @@ -75,7 +75,7 @@ func TestList(t *testing.T) {
assert.Equal(t, 1, len(entries))
entry := entries[0]

assert.True(t, testAddr.Compare(entry.Address) == 0)
assert.True(t, testAddr.Compare(&entry.Address) == 0)
assert.Equal(t, 1, len(entries[0].Names))
assert.Equal(t, "testname", entries[0].Names[0])
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestGetFromNameOrAddress(t *testing.T) {
require.True(t, ok)
require.Len(t, names, 1)
assert.Equal(t, "testname", names[0])
assert.True(t, resultAddr.Compare(testAddr) == 0)
assert.True(t, resultAddr.Compare(&testAddr) == 0)
})
}
}
2 changes: 1 addition & 1 deletion gno.land/cmd/gnoland/secrets_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func validateValidatorKey(key *privval.FilePVKey) error {

// Make sure the address is derived
// from the public key
if key.PubKey.Address().Compare(key.Address) != 0 {
if pk := key.PubKey.Address(); pk.Compare(&key.Address) != 0 {
return errAddressMismatch
}

Expand Down
4 changes: 3 additions & 1 deletion tm2/pkg/bft/consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ func (vss ValidatorStubsByAddress) Len() int {
}

func (vss ValidatorStubsByAddress) Less(i, j int) bool {
return vss[i].GetPubKey().Address().Compare(vss[j].GetPubKey().Address()) == -1
vai := vss[i].GetPubKey().Address()
vaj := vss[j].GetPubKey().Address()
return vai.Compare(&vaj) == -1
}

func (vss ValidatorStubsByAddress) Swap(i, j int) {
Expand Down
5 changes: 3 additions & 2 deletions tm2/pkg/bft/types/priv_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func (pvs PrivValidatorsByAddress) Len() int {
}

func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
return pvs[i].GetPubKey().Address().Compare(
pvs[j].GetPubKey().Address()) == -1
pai := pvs[i].GetPubKey().Address()
paj := pvs[j].GetPubKey().Address()
return pai.Compare(&paj) == -1
}

func (pvs PrivValidatorsByAddress) Swap(i, j int) {
Expand Down
2 changes: 1 addition & 1 deletion tm2/pkg/bft/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (v *Validator) CompareProposerPriority(other *Validator) *Validator {
case v.ProposerPriority < other.ProposerPriority:
return other
default:
result := v.Address.Compare(other.Address)
result := v.Address.Compare(&other.Address)
switch {
case result < 0:
return v
Expand Down
8 changes: 4 additions & 4 deletions tm2/pkg/bft/types/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (vals *ValidatorSet) Copy() *ValidatorSet {
// otherwise.
func (vals *ValidatorSet) HasAddress(address crypto.Address) bool {
idx := sort.Search(len(vals.Validators), func(i int) bool {
return address.Compare(vals.Validators[i].Address) <= 0
return address.Compare(&vals.Validators[i].Address) <= 0
})
return idx < len(vals.Validators) && vals.Validators[idx].Address == address
}
Expand All @@ -232,7 +232,7 @@ func (vals *ValidatorSet) HasAddress(address crypto.Address) bool {
// itself if found. Otherwise, -1 and nil are returned.
func (vals *ValidatorSet) GetByAddress(address crypto.Address) (index int, val *Validator) {
idx := sort.Search(len(vals.Validators), func(i int) bool {
return address.Compare(vals.Validators[i].Address) <= 0
return address.Compare(&vals.Validators[i].Address) <= 0
})
if idx < len(vals.Validators) && vals.Validators[idx].Address == address {
return idx, vals.Validators[idx].Copy()
Expand Down Expand Up @@ -440,7 +440,7 @@ func (vals *ValidatorSet) applyUpdates(updates []*Validator) {
i := 0

for len(existing) > 0 && len(updates) > 0 {
if existing[0].Address.Compare(updates[0].Address) < 0 { // unchanged validator
if existing[0].Address.Compare(&updates[0].Address) < 0 { // unchanged validator
merged[i] = existing[0]
existing = existing[1:]
} else {
Expand Down Expand Up @@ -813,7 +813,7 @@ func (valz ValidatorsByAddress) Len() int {
}

func (valz ValidatorsByAddress) Less(i, j int) bool {
return valz[i].Address.Compare(valz[j].Address) == -1
return valz[i].Address.Compare(&valz[j].Address) == -1
}

func (valz ValidatorsByAddress) Swap(i, j int) {
Expand Down
2 changes: 1 addition & 1 deletion tm2/pkg/bft/types/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ func (valz validatorsByPriority) Less(i, j int) bool {
if valz[i].ProposerPriority > valz[j].ProposerPriority {
return false
}
return valz[i].Address.Compare(valz[j].Address) < 0
return valz[i].Address.Compare(&valz[j].Address) < 0
}

func (valz validatorsByPriority) Swap(i, j int) {
Expand Down
29 changes: 29 additions & 0 deletions tm2/pkg/crypto/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package crypto

import (
"crypto/rand"
"testing"
)

var sink any = nil

func BenchmarkAddressCompare(b *testing.B) {
var addr1, addr2 Address
rand.Read(addr1[:])
rand.Read(addr2[:])
b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
sink = addr1.Compare(&addr1)
sink = addr1.Compare(&addr2)
sink = addr2.Compare(&addr2)
sink = addr2.Compare(&addr1)
}

if sink == nil {
b.Fatal("Benchmark did not run!")
}

sink = nil
}
8 changes: 2 additions & 6 deletions tm2/pkg/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ func (addr *Address) UnmarshalAmino(b32str string) (err error) {
return nil
}

func (addr Address) Compare(other Address) int {
bz1 := make([]byte, len(addr))
bz2 := make([]byte, len(other))
copy(bz1, addr[:])
copy(bz2, other[:])
return bytes.Compare(bz1, bz2)
func (addr *Address) Compare(other *Address) int {
return bytes.Compare(addr[:], other[:])
}

func (addr Address) IsZero() bool {
Expand Down
4 changes: 3 additions & 1 deletion tm2/pkg/crypto/keys/client/add_multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ func execAddMultisig(cfg *AddMultisigCfg, args []string, io commands.IO) error {
// Check if the keys should be sorted
if !cfg.NoSort {
sort.Slice(publicKeys, func(i, j int) bool {
return publicKeys[i].Address().Compare(publicKeys[j].Address()) < 0
pki := publicKeys[i].Address()
pkj := publicKeys[j].Address()
return pki.Compare(&pkj) < 0
})
}

Expand Down
Loading