From ac846f057ad2215f4ab0b3725fa57d75af0ceb82 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 11 May 2024 01:58:49 +0200 Subject: [PATCH] wip --- lib/encoding/int_timing_test.go | 100 ++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-) diff --git a/lib/encoding/int_timing_test.go b/lib/encoding/int_timing_test.go index a1b96c9fe..481316e2e 100644 --- a/lib/encoding/int_timing_test.go +++ b/lib/encoding/int_timing_test.go @@ -79,7 +79,7 @@ func benchmarkMarshalVarUint64s(b *testing.B, maxValue uint64) { var data []uint64 n := maxValue for i := 0; i < numsCount; i++ { - if n <= 0 { + if n > maxValue { n = maxValue } data = append(data, n) @@ -119,7 +119,7 @@ func benchmarkMarshalVarInt64s(b *testing.B, maxValue int64) { var data []int64 n := maxValue for i := 0; i < numsCount; i++ { - if n <= 0 { + if n < -maxValue { n = maxValue } data = append(data, n) @@ -139,6 +139,52 @@ func benchmarkMarshalVarInt64s(b *testing.B, maxValue int64) { }) } +func BenchmarkUnmarshalVarUint64(b *testing.B) { + b.Run("up-to-(1<<7)-1", func(b *testing.B) { + benchmarkUnmarshalVarUint64(b, (1<<7)-1) + }) + b.Run("up-to-(1<<14)-1", func(b *testing.B) { + benchmarkUnmarshalVarUint64(b, (1<<14)-1) + }) + b.Run("up-to-(1<<28)-1", func(b *testing.B) { + benchmarkUnmarshalVarUint64(b, (1<<28)-1) + }) + b.Run("up-to-(1<<64)-1", func(b *testing.B) { + benchmarkUnmarshalVarUint64(b, (1<<64)-1) + }) +} + +func benchmarkUnmarshalVarUint64(b *testing.B, maxValue uint64) { + const numsCount = 8000 + var data []byte + n := maxValue + for i := 0; i < numsCount; i++ { + if n > maxValue { + n = maxValue + } + data = MarshalVarUint64(data, n) + n-- + } + b.ResetTimer() + b.ReportAllocs() + b.SetBytes(numsCount) + b.RunParallel(func(pb *testing.PB) { + var sink uint64 + for pb.Next() { + src := data + for len(src) > 0 { + tail, n, err := UnmarshalVarUint64(src) + if err != nil { + panic(fmt.Errorf("unexpected error: %w", err)) + } + sink += n + src = tail + } + } + Sink.Add(sink) + }) +} + func BenchmarkUnmarshalVarUint64s(b *testing.B) { b.Run("up-to-(1<<7)-1", func(b *testing.B) { benchmarkUnmarshalVarUint64s(b, (1<<7)-1) @@ -159,7 +205,7 @@ func benchmarkUnmarshalVarUint64s(b *testing.B, maxValue uint64) { var data []byte n := maxValue for i := 0; i < numsCount; i++ { - if n <= 0 { + if n > maxValue { n = maxValue } data = MarshalVarUint64(data, n) @@ -185,6 +231,52 @@ func benchmarkUnmarshalVarUint64s(b *testing.B, maxValue uint64) { }) } +func BenchmarkUnmarshalVarInt64(b *testing.B) { + b.Run("up-to-(1<<6)-1", func(b *testing.B) { + benchmarkUnmarshalVarInt64(b, (1<<6)-1) + }) + b.Run("up-to-(1<<13)-1", func(b *testing.B) { + benchmarkUnmarshalVarInt64(b, (1<<13)-1) + }) + b.Run("up-to-(1<<27)-1", func(b *testing.B) { + benchmarkUnmarshalVarInt64(b, (1<<27)-1) + }) + b.Run("up-to-(1<<63)-1", func(b *testing.B) { + benchmarkUnmarshalVarInt64(b, (1<<63)-1) + }) +} + +func benchmarkUnmarshalVarInt64(b *testing.B, maxValue int64) { + const numsCount = 8000 + var data []byte + n := maxValue + for i := 0; i < numsCount; i++ { + if n < -maxValue { + n = maxValue + } + data = MarshalVarInt64(data, n) + n-- + } + b.ResetTimer() + b.ReportAllocs() + b.SetBytes(numsCount) + b.RunParallel(func(pb *testing.PB) { + var sink uint64 + for pb.Next() { + src := data + for len(src) > 0 { + tail, n, err := UnmarshalVarInt64(src) + if err != nil { + panic(fmt.Errorf("unexpected error: %w", err)) + } + sink += uint64(n) + src = tail + } + } + Sink.Add(sink) + }) +} + func BenchmarkUnmarshalVarInt64s(b *testing.B) { b.Run("up-to-(1<<6)-1", func(b *testing.B) { benchmarkUnmarshalVarInt64s(b, (1<<6)-1) @@ -205,7 +297,7 @@ func benchmarkUnmarshalVarInt64s(b *testing.B, maxValue int64) { var data []byte n := maxValue for i := 0; i < numsCount; i++ { - if n <= 0 { + if n < -maxValue { n = maxValue } data = MarshalVarInt64(data, n)