forked from prysmaticlabs/prysm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize Copying of Fields (prysmaticlabs#4811)
* add new changes * memory pool * add test * final optimization * preston's review
- Loading branch information
Showing
8 changed files
with
113 additions
and
10 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
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)) | ||
} | ||
} |