Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Memory Pool for Randao Mixes (#4896)
* add mem pool * use mem pool * Update shared/memorypool/memorypool.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * Update shared/memorypool/memorypool.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
- Loading branch information
1 parent
b4881e3
commit 3d12322
Showing
6 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["memorypool_test.go"], | ||
embed = [":go_default_library"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package memorypool | ||
|
||
import "sync" | ||
|
||
// DoubleByteSlicePool represents the memory pool | ||
// for 2d byte slices. | ||
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. | ||
func PutDoubleByteSlice(data [][]byte) { | ||
DoubleByteSlicePool.Put(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
} |