Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Memory Pool for Randao Mixes #4896

Merged
merged 4 commits into from Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions beacon-chain/state/BUILD.bazel
Expand Up @@ -20,6 +20,7 @@ go_library(
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/memorypool:go_default_library",
"//shared/params:go_default_library",
"//shared/stateutil:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
Expand Down
5 changes: 3 additions & 2 deletions beacon-chain/state/getters.go
Expand Up @@ -2,11 +2,12 @@ package state

import (
"fmt"
"github.com/prysmaticlabs/go-bitfield"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-bitfield"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/memorypool"
)

// EffectiveBalance returns the effective balance of the
Expand Down Expand Up @@ -452,7 +453,7 @@ func (b *BeaconState) RandaoMixes() [][]byte {
b.lock.RLock()
defer b.lock.RUnlock()

mixes := make([][]byte, len(b.state.RandaoMixes))
mixes := memorypool.GetDoubleByteSlice(len(b.state.RandaoMixes))
for i, r := range b.state.RandaoMixes {
tmpRt := make([]byte, len(r))
copy(tmpRt, r)
Expand Down
6 changes: 5 additions & 1 deletion beacon-chain/state/types.go
Expand Up @@ -12,6 +12,7 @@ import (
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/memorypool"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/stateutil"
)
Expand Down Expand Up @@ -141,8 +142,11 @@ func (b *BeaconState) Copy() *BeaconState {

// Finalizer runs when dst is being destroyed in garbage collection.
runtime.SetFinalizer(dst, func(b *BeaconState) {
for _, v := range b.sharedFieldReferences {
for field, v := range b.sharedFieldReferences {
v.refs--
if field == randaoMixes && v.refs == 0 {
memorypool.PutDoubleByteSlice(b.state.RandaoMixes)
}
}
})

Expand Down
14 changes: 14 additions & 0 deletions shared/memorypool/BUILD.bazel
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["memorypool.go"],
importpath = "github.com/prysmaticlabs/prysm/shared/memorypool",
visibility = ["//visibility:public"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visibility

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anything wrong with it ? it makes sense to make this public as it is in shared

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah sorry, thought it was in beacon-chain

)

go_test(
name = "go_default_test",
srcs = ["memorypool_test.go"],
embed = [":go_default_library"],
)
27 changes: 27 additions & 0 deletions shared/memorypool/memorypool.go
@@ -0,0 +1,27 @@
package memorypool

import "sync"

// DoubleByteSlicePool represents the memory pool
// for 2d byte slices
nisdas marked this conversation as resolved.
Show resolved Hide resolved
var DoubleByteSlicePool = new(sync.Pool)

// GetDoubleByteSlice retrieves the 2d byte slice of
// the desired size from the memory pool.
func GetDoubleByteSlice(size int) [][]byte {
rawObj := DoubleByteSlicePool.Get()
if rawObj == nil {
return make([][]byte, size)
}
byteSlice := rawObj.([][]byte)
if len(byteSlice) >= size {
return byteSlice[:size]
}
return append(byteSlice, make([][]byte, size-len(byteSlice))...)
}

// PutDoubleByteSlice places the provided 2d byte slice
// in the memory pool
nisdas marked this conversation as resolved.
Show resolved Hide resolved
func PutDoubleByteSlice(data [][]byte) {
DoubleByteSlicePool.Put(data)
}
16 changes: 16 additions & 0 deletions shared/memorypool/memorypool_test.go
@@ -0,0 +1,16 @@
package memorypool

import (
"testing"
)

func TestRoundTripMemoryRetrieval(t *testing.T) {
byteSlice := make([][]byte, 1000)
PutDoubleByteSlice(byteSlice)
newSlice := GetDoubleByteSlice(1000)

if len(newSlice) != 1000 {
t.Errorf("Wanted same slice object, but got different object. "+
"Wanted slice with length %d but got length %d", 1000, len(newSlice))
}
}