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

refactor(shares): use more efficient make([]byte, N) not bytes.Repeat([]byte{0}, N) #42

Conversation

odeke-em
Copy link
Contributor

@odeke-em odeke-em commented Feb 29, 2024

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:

$ 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 #41

@odeke-em odeke-em requested review from a team as code owners February 29, 2024 13:07
@odeke-em odeke-em requested review from ramin, staheri14 and distractedm1nd and removed request for a team February 29, 2024 13:07
@celestia-bot celestia-bot requested review from a team February 29, 2024 13:07
…, 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
@odeke-em odeke-em force-pushed the all-use-make-zeroing-instead-of-bytes-repeat-zero-byte branch from 3d3d637 to a58175c Compare February 29, 2024 13:08
@rootulp rootulp changed the title shares: use more efficient make([]byte, N) not bytes.Repeat([]byte{0}, N) refactor(shares): use more efficient make([]byte, N) not bytes.Repeat([]byte{0}, N) Feb 29, 2024
Copy link
Collaborator

@rootulp rootulp left a 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)
Copy link
Collaborator

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)
Copy link
Collaborator

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

@rootulp rootulp merged commit c20eda9 into celestiaorg:main Feb 29, 2024
9 of 11 checks passed
@odeke-em odeke-em deleted the all-use-make-zeroing-instead-of-bytes-repeat-zero-byte branch February 29, 2024 16:55
@odeke-em
Copy link
Contributor Author

Thank you for the review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants