-
Notifications
You must be signed in to change notification settings - Fork 18
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
refactor(shares): use more efficient make([]byte, N) not bytes.Repeat([]byte{0}, N) #42
refactor(shares): use more efficient make([]byte, N) not bytes.Repeat([]byte{0}, N) #42
Conversation
…, N) This change uses Go's memory model guarantees that the zero value memory shall be zeroed and hence to create a slice of 0 bytes aka padding, we can simply use make([]byte, N) which is much more efficient than bytes.Repeat([]byte{0}, N) and the benchmarks show this improvement: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta PaddingVsRepeat-8 674ns ± 1% 538ns ± 0% -20.22% (p=0.000 n=9+9) name old alloc/op new alloc/op delta PaddingVsRepeat-8 1.31kB ± 0% 0.83kB ± 0% -36.59% (p=0.000 n=10+10) name old allocs/op new allocs/op delta PaddingVsRepeat-8 12.0 ± 0% 11.0 ± 0% -8.33% (p=0.000 n=10+10) ``` Fixes celestiaorg#41
3d3d637
to
a58175c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I updated the PR title to conform to conventional commits to pass a CI check
@@ -31,8 +31,7 @@ func zeroPadIfNecessary(share []byte, width int) (padded []byte, bytesOfPadding | |||
} | |||
|
|||
missingBytes := width - oldLen | |||
padByte := []byte{0} | |||
padding := bytes.Repeat(padByte, missingBytes) | |||
padding := make([]byte, missingBytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM because Test_zeroPadIfNecessary
still passes
@@ -20,7 +19,7 @@ func NamespacePaddingShare(ns namespace.Namespace, shareVersion uint8) (Share, e | |||
if err := b.WriteSequenceLen(0); err != nil { | |||
return Share{}, err | |||
} | |||
padding := bytes.Repeat([]byte{0}, FirstSparseShareContentSize) | |||
padding := make([]byte, FirstSparseShareContentSize) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM b/c TestNamespacePaddingShare
still passes
Thank you for the review and merge! |
This change uses Go's memory model guarantees that the zero value memory shall be zeroed and hence to create a slice of 0 bytes aka padding, we can simply use make([]byte, N) which is much more efficient than bytes.Repeat([]byte{0}, N) and the benchmarks show this improvement:
Fixes #41