more efficient (#10440)

This commit is contained in:
Nishant Das 2022-03-28 20:27:14 +08:00 committed by GitHub
parent 071f6de559
commit ef8bc97d3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 10 deletions

View File

@ -125,6 +125,7 @@ go_library(
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//crypto/rand:go_default_library",
"//cache/lru:go_default_library",
"@com_github_dgraph_io_ristretto//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_supranational_blst//:go_default_library",
@ -227,7 +228,7 @@ go_test(
deps = [
":go_default_library",
"//crypto/bls/common:go_default_library",
"//encoding/bytesutil:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",
],
)

View File

@ -7,19 +7,16 @@ package blst
import (
"fmt"
"github.com/dgraph-io/ristretto"
"github.com/pkg/errors"
lruwrpr "github.com/prysmaticlabs/prysm/cache/lru"
"github.com/prysmaticlabs/prysm/config/features"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/crypto/bls/common"
)
var maxKeys = int64(1000000)
var pubkeyCache, _ = ristretto.NewCache(&ristretto.Config{
NumCounters: maxKeys,
MaxCost: 1 << 26, // ~64mb is cache max size
BufferItems: 64,
})
var maxKeys = 1000000
var pubkeyCache = lruwrpr.New(maxKeys)
// PublicKey used in the BLS signature scheme.
type PublicKey struct {
@ -34,7 +31,8 @@ func PublicKeyFromBytes(pubKey []byte) (common.PublicKey, error) {
if len(pubKey) != params.BeaconConfig().BLSPubkeyLength {
return nil, fmt.Errorf("public key must be %d bytes", params.BeaconConfig().BLSPubkeyLength)
}
if cv, ok := pubkeyCache.Get(string(pubKey)); ok {
newKey := (*[fieldparams.BLSPubkeyLength]byte)(pubKey)
if cv, ok := pubkeyCache.Get(*newKey); ok {
return cv.(*PublicKey).Copy(), nil
}
// Subgroup check NOT done when decompressing pubkey.
@ -49,7 +47,8 @@ func PublicKeyFromBytes(pubKey []byte) (common.PublicKey, error) {
}
pubKeyObj := &PublicKey{p: p}
copiedKey := pubKeyObj.Copy()
pubkeyCache.Set(string(pubKey), copiedKey, 48)
cacheKey := *newKey
pubkeyCache.Add(cacheKey, copiedKey)
return pubKeyObj, nil
}