stateutil: Reduce allocations by specifying slice capacity eager (#9977)

* Reduce allocations by specifying slice capacity eager

* gofmt
This commit is contained in:
Preston Van Loon 2021-12-03 04:06:05 -05:00 committed by GitHub
parent d3c97da4e1
commit 3767574c77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View File

@ -57,6 +57,7 @@ go_test(
"state_root_test.go", "state_root_test.go",
"stateutil_test.go", "stateutil_test.go",
"trie_helpers_test.go", "trie_helpers_test.go",
"validator_root_test.go",
], ],
embed = [":go_default_library"], embed = [":go_default_library"],
deps = [ deps = [

View File

@ -58,7 +58,7 @@ func ValidatorRootWithHasher(hasher ssz.HashFn, validator *ethpb.Validator) ([32
// a list of uint64 and mixed with registry limit. // a list of uint64 and mixed with registry limit.
func Uint64ListRootWithRegistryLimit(balances []uint64) ([32]byte, error) { func Uint64ListRootWithRegistryLimit(balances []uint64) ([32]byte, error) {
hasher := hash.CustomSHA256Hasher() hasher := hash.CustomSHA256Hasher()
balancesMarshaling := make([][]byte, 0) balancesMarshaling := make([][]byte, 0, len(balances))
for i := 0; i < len(balances); i++ { for i := 0; i < len(balances); i++ {
balanceBuf := make([]byte, 8) balanceBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(balanceBuf, balances[i]) binary.LittleEndian.PutUint64(balanceBuf, balances[i])

View File

@ -0,0 +1,22 @@
package stateutil_test
import (
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
)
func BenchmarkUint64ListRootWithRegistryLimit(b *testing.B) {
balances := make([]uint64, 100000)
for i := 0; i < len(balances); i++ {
balances[i] = uint64(i)
}
b.Run("100k balances", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := stateutil.Uint64ListRootWithRegistryLimit(balances)
if err != nil {
b.Fatal(err)
}
}
})
}