You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
shares: NamespacePaddingShare can take advantage of Go's memory guarantees to zero memory and use make([]byte, LEN) instead of bytes.Repeat([]byte{0}, LEN) which is less efficient
#41
Closed
4 tasks
odeke-em opened this issue
Feb 29, 2024
· 0 comments
· Fixed by #42
Those code snippets try to create zero byte slices of LEN. Go guarantees that all memory shall be zeroed per https://go.dev/ref/spec#The_zero_value and even better, make is made much more efficient than bytes.Repeat and quick benchmarks can prove this
…, 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)
```
Fixescelestiaorg#41
Summary of Bug
While auditing this repository I noticed these code snippets
go-square/shares/padding.go
Line 23 in 4135f35
and
go-square/shares/utils.go
Lines 34 to 35 in 4135f35
Insights
Those code snippets try to create zero byte slices of LEN. Go guarantees that all memory shall be zeroed per https://go.dev/ref/spec#The_zero_value and even better, make is made much more efficient than bytes.Repeat and quick benchmarks can prove this
Benchmarks
with this benchmark
which shows the improvements
/cc @elias-orijtech @liamsi
Version
4135f35
Steps to Reproduce
Please see the details above
For Admin Use
The text was updated successfully, but these errors were encountered: