From 217d7bdf44d8bbfe1c34a9a64baf4696bdad8a35 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 25 Aug 2021 15:18:26 +0300 Subject: [PATCH] keys: add equality benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Go 1.15 provides native (*ecdsa.PublicKey).Equal method, but we can't drop our own Equal because the types are different and there is still code using our Equal (forcing it to convert types is counterproductive), while changing (*PublicKey).Equal to use (*ecdsa.PublicKey).Equal internally with some kind of (*ecdsa.PublicKey)(p).Equal((*ecdsa.PublicKey)(key)) slows it down: name old time/op new time/op delta PublicEqual-8 14.9ns ± 1% 18.4ns ± 2% +23.55% (p=0.000 n=9+10) name old alloc/op new alloc/op delta PublicEqual-8 0.00B 0.00B ~ (all equal) name old allocs/op new allocs/op delta PublicEqual-8 0.00 0.00 ~ (all equal) So leave it as is, but add this micro-bench. Refs. #1319. --- pkg/crypto/keys/publickey_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index 30d992c14..d84f8c605 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -165,7 +165,7 @@ func TestUnique(t *testing.T) { require.Equal(t, 1, unique.Len()) } -func getPubKey(t *testing.T) *PublicKey { +func getPubKey(t testing.TB) *PublicKey { pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4") require.NoError(t, err) return pubKey @@ -212,3 +212,14 @@ func TestUnmarshallJSONBadFormat(t *testing.T) { err := json.Unmarshal([]byte(str), actual) require.Error(t, err) } + +func BenchmarkPublicEqual(t *testing.B) { + k11 := getPubKey(t) + k12 := getPubKey(t) + k2, err := NewPublicKeyFromString("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c") + require.NoError(t, err) + for n := 0; n < t.N; n++ { + _ = k11.Equal(k12) + _ = k11.Equal(k2) + } +}