From d724da794fe3fe666e108e7c58273f8d158dbd37 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 28 Apr 2024 22:03:27 +0200 Subject: [PATCH] wip --- lib/encoding/int.go | 30 +++++++++++++++++++++++++++--- lib/encoding/int_timing_test.go | 8 ++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/encoding/int.go b/lib/encoding/int.go index 3295e6ad4..cccc9ebad 100644 --- a/lib/encoding/int.go +++ b/lib/encoding/int.go @@ -83,9 +83,22 @@ func UnmarshalInt64(src []byte) int64 { // MarshalVarInt64 appends marshalsed v to dst and returns the result. func MarshalVarInt64(dst []byte, v int64) []byte { - var tmp [1]int64 - tmp[0] = v - return MarshalVarInt64s(dst, tmp[:]) + u := uint64((v << 1) ^ (v >> 63)) + + if v < (1<<6) && v > (-1<<6) { + return append(dst, byte(u)) + } + if u < (1 << (2 * 7)) { + return append(dst, byte(u|0x80), byte(u>>7)) + } + if u < (1 << (3 * 7)) { + return append(dst, byte(u|0x80), byte((u>>7)|0x80), byte(u>>(2*7))) + } + + // Slow path for big integers + var tmp [1]uint64 + tmp[0] = u + return MarshalVarUint64s(dst, tmp[:]) } // MarshalVarInt64s appends marshaled vs to dst and returns the result. @@ -268,6 +281,17 @@ func unmarshalVarInt64sSlow(dst []int64, src []byte) ([]byte, error) { // MarshalVarUint64 appends marshaled u to dst and returns the result. func MarshalVarUint64(dst []byte, u uint64) []byte { + if u < (1 << 7) { + return append(dst, byte(u)) + } + if u < (1 << (2 * 7)) { + return append(dst, byte(u|0x80), byte(u>>7)) + } + if u < (1 << (3 * 7)) { + return append(dst, byte(u|0x80), byte((u>>7)|0x80), byte(u>>(2*7))) + } + + // Slow path for big integers. var tmp [1]uint64 tmp[0] = u return MarshalVarUint64s(dst, tmp[:]) diff --git a/lib/encoding/int_timing_test.go b/lib/encoding/int_timing_test.go index 12dad83a0..a1b96c9fe 100644 --- a/lib/encoding/int_timing_test.go +++ b/lib/encoding/int_timing_test.go @@ -233,10 +233,10 @@ func benchmarkUnmarshalVarInt64s(b *testing.B, maxValue int64) { func BenchmarkMarshalVarUint64(b *testing.B) { b.Run("small-ints", func(b *testing.B) { - benchmarkMarshalVarUint64(b, []uint64{1,2,3,4,5,67,127}) + benchmarkMarshalVarUint64(b, []uint64{1, 2, 3, 4, 5, 67, 127}) }) b.Run("big-ints", func(b *testing.B) { - benchmarkMarshalVarUint64(b, []uint64{12355,89832432, 8989843, 8989989, 883443, 9891233, 8232434342}) + benchmarkMarshalVarUint64(b, []uint64{12355, 89832432, 8989843, 8989989, 883443, 9891233, 8232434342}) }) } @@ -259,10 +259,10 @@ func benchmarkMarshalVarUint64(b *testing.B, a []uint64) { func BenchmarkMarshalVarInt64(b *testing.B) { b.Run("small-ints", func(b *testing.B) { - benchmarkMarshalVarInt64(b, []int64{1,2,3,-4,5,-60,63}) + benchmarkMarshalVarInt64(b, []int64{1, 2, 3, -4, 5, -60, 63}) }) b.Run("big-ints", func(b *testing.B) { - benchmarkMarshalVarInt64(b, []int64{12355,-89832432, 8989843, -8989989, 883443, -9891233, 8232434342}) + benchmarkMarshalVarInt64(b, []int64{12355, -89832432, 8989843, -8989989, 883443, -9891233, 8232434342}) }) }