Skip to content

Commit

Permalink
Add bench for custom decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Aug 22, 2022
1 parent 5788bc1 commit 76b2308
Showing 1 changed file with 93 additions and 4 deletions.
97 changes: 93 additions & 4 deletions decoder_bench_test.go
Expand Up @@ -117,7 +117,7 @@ func Benchmark_uintSlice64_Decode_field_make(b *testing.B) {
}
}

func Benchmark_uintSlice64_reflect_noMake(b *testing.B) {
func Benchmark_uintSlice64_readArray_noMake(b *testing.B) {
l := 1024
buf := concatByteSlices(
newUint64SliceEncoded(l),
Expand All @@ -142,7 +142,7 @@ func Benchmark_uintSlice64_reflect_noMake(b *testing.B) {
}
}

func Benchmark_uintSlice64_reflect_make(b *testing.B) {
func Benchmark_uintSlice64_readArray_make(b *testing.B) {
l := 1024
buf := concatByteSlices(
newUint64SliceEncoded(l),
Expand All @@ -167,6 +167,51 @@ func Benchmark_uintSlice64_reflect_make(b *testing.B) {
}
}

type sliceUint64WithCustomDecoder []uint64

// UnmarshalWithDecoder
func (s *sliceUint64WithCustomDecoder) UnmarshalWithDecoder(decoder *Decoder) error {
// read length
l, err := decoder.ReadUint32(LE)
if err != nil {
return err
}
// read data
*s = make([]uint64, l)
for i := 0; i < int(l); i++ {
(*s)[i], err = decoder.ReadUint64(LE)
if err != nil {
return err
}
}
return nil
}

func Benchmark_uintSlice64_Decode_field_withCustomDecoder(b *testing.B) {
l := 1024
buf := concatByteSlices(
// length:
uint32ToBytes(uint32(l), LE),
// data:
newUint64SliceEncoded(l),
)
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
var got sliceUint64WithCustomDecoder

decoder := NewBorshDecoder(buf)
err := got.UnmarshalWithDecoder(decoder)
if err != nil {
b.Error(err)
}
if len(got) != l {
b.Errorf("got %d, want %d", len(got), l)
}
}
}

func newUint32SliceEncoded(l int) []byte {
buf := make([]byte, 0)
for i := 0; i < l; i++ {
Expand Down Expand Up @@ -279,7 +324,7 @@ func Benchmark_uintSlice32_Decode_field_make(b *testing.B) {
}
}

func Benchmark_uintSlice32_reflect_noMake(b *testing.B) {
func Benchmark_uintSlice32_readArray_noMake(b *testing.B) {
l := 1024
buf := concatByteSlices(
newUint32SliceEncoded(l),
Expand All @@ -304,7 +349,7 @@ func Benchmark_uintSlice32_reflect_noMake(b *testing.B) {
}
}

func Benchmark_uintSlice32_reflect_make(b *testing.B) {
func Benchmark_uintSlice32_readArray_make(b *testing.B) {
l := 1024
buf := concatByteSlices(
newUint32SliceEncoded(l),
Expand All @@ -328,3 +373,47 @@ func Benchmark_uintSlice32_reflect_make(b *testing.B) {
}
}
}

type sliceUint32WithCustomDecoder []uint32

// UnmarshalWithDecoder
func (s *sliceUint32WithCustomDecoder) UnmarshalWithDecoder(decoder *Decoder) error {
// read length
l, err := decoder.ReadUint32(LE)
if err != nil {
return err
}
// read data
*s = make([]uint32, l)
for i := 0; i < int(l); i++ {
(*s)[i], err = decoder.ReadUint32(LE)
if err != nil {
return err
}
}
return nil
}
func Benchmark_uintSlice32_Decode_field_withCustomDecoder(b *testing.B) {
l := 1024
buf := concatByteSlices(
// length:
uint32ToBytes(uint32(l), LE),
// data:
newUint32SliceEncoded(l),
)
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
var got sliceUint32WithCustomDecoder

decoder := NewBorshDecoder(buf)
err := got.UnmarshalWithDecoder(decoder)
if err != nil {
b.Error(err)
}
if len(got) != l {
b.Errorf("got %d, want %d", len(got), l)
}
}
}

0 comments on commit 76b2308

Please sign in to comment.