keys: add equality benchmark

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.
This commit is contained in:
Roman Khimov 2021-08-25 15:18:26 +03:00
parent ad5acf4873
commit 217d7bdf44

View file

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