stategen: Pre-populate bls pubkey cache as part of stategen's Resume function (#11482)

* Pre-populate bls pubkey cache as part of state gen's Resume function. This adds some helpers and a benchmark to blst

* Do it async

* fix missing import

* lint

---------

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: rauljordan <raul@prysmaticlabs.com>
This commit is contained in:
Preston Van Loon
2023-05-10 05:44:15 -05:00
committed by GitHub
parent 7d9f36985e
commit 4b4e213a24
6 changed files with 62 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ go_test(
"public_key_test.go",
"secret_key_test.go",
"signature_test.go",
"test_helper_test.go",
],
embed = [":go_default_library"],
deps = [

View File

@@ -12,7 +12,7 @@ import (
"github.com/prysmaticlabs/prysm/v4/crypto/bls/common"
)
var maxKeys = 1000000
var maxKeys = 1_000_000
var pubkeyCache = lruwrpr.New(maxKeys)
// PublicKey used in the BLS signature scheme.

View File

@@ -97,3 +97,27 @@ func TestPublicKeysEmpty(t *testing.T) {
_, err := blst.AggregatePublicKeys(pubs)
require.ErrorContains(t, "nil or empty public keys", err)
}
func BenchmarkPublicKeyFromBytes(b *testing.B) {
priv, err := blst.RandKey()
require.NoError(b, err)
pubkey := priv.PublicKey()
pubkeyBytes := pubkey.Marshal()
b.Run("cache on", func(b *testing.B) {
blst.EnableCaches()
for i := 0; i < b.N; i++ {
_, err := blst.PublicKeyFromBytes(pubkeyBytes)
require.NoError(b, err)
}
})
b.Run("cache off", func(b *testing.B) {
blst.DisableCaches()
for i := 0; i < b.N; i++ {
_, err := blst.PublicKeyFromBytes(pubkeyBytes)
require.NoError(b, err)
}
})
}

View File

@@ -0,0 +1,13 @@
package blst
// Note: These functions are for tests to access private globals, such as pubkeyCache.
// DisableCaches sets the cache sizes to 0.
func DisableCaches() {
pubkeyCache.Resize(0)
}
// EnableCaches sets the cache sizes to the default values.
func EnableCaches() {
pubkeyCache.Resize(maxKeys)
}