celestiaorg / go-square

A library for encoding blobs into a 2D square of evenly sized chunks designed for sampling and reconstruction
Apache License 2.0
13 stars 17 forks source link

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

Closed odeke-em closed 8 months ago

odeke-em commented 8 months ago

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 commented 8 months ago

Thank you for the review and merge!