This commit is contained in:
Aliaksandr Valialkin 2024-04-28 22:03:27 +02:00
parent 6d3b506dcf
commit d724da794f
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 31 additions and 7 deletions

View file

@ -83,9 +83,22 @@ func UnmarshalInt64(src []byte) int64 {
// MarshalVarInt64 appends marshalsed v to dst and returns the result. // MarshalVarInt64 appends marshalsed v to dst and returns the result.
func MarshalVarInt64(dst []byte, v int64) []byte { func MarshalVarInt64(dst []byte, v int64) []byte {
var tmp [1]int64 u := uint64((v << 1) ^ (v >> 63))
tmp[0] = v
return MarshalVarInt64s(dst, tmp[:]) 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. // 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. // MarshalVarUint64 appends marshaled u to dst and returns the result.
func MarshalVarUint64(dst []byte, u uint64) []byte { 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 var tmp [1]uint64
tmp[0] = u tmp[0] = u
return MarshalVarUint64s(dst, tmp[:]) return MarshalVarUint64s(dst, tmp[:])

View file

@ -233,10 +233,10 @@ func benchmarkUnmarshalVarInt64s(b *testing.B, maxValue int64) {
func BenchmarkMarshalVarUint64(b *testing.B) { func BenchmarkMarshalVarUint64(b *testing.B) {
b.Run("small-ints", func(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) { 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) { func BenchmarkMarshalVarInt64(b *testing.B) {
b.Run("small-ints", func(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) { 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})
}) })
} }