This commit is contained in:
Aliaksandr Valialkin 2024-05-11 02:13:05 +02:00
parent 93c0cffda6
commit d098d5d1ea
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -165,16 +165,19 @@ func marshalVarInt64sSlow(dst []byte, vs []int64) []byte {
return dst return dst
} }
// UnmarshalVarInt64 returns unmarshaled int64 from src and returns // UnmarshalVarInt64 returns unmarshaled int64 from src and returns the remaining tail from src.
// the remaining tail from src.
func UnmarshalVarInt64(src []byte) ([]byte, int64, error) { func UnmarshalVarInt64(src []byte) ([]byte, int64, error) {
var tmp [1]int64 // TODO substitute binary.Uvarint with binary.Varint when benchmark results will show it is faster.
tail, err := UnmarshalVarInt64s(tmp[:], src) // It is slower on amd64/linux Go1.22.
return tail, tmp[0], err u64, offset := binary.Uvarint(src)
if offset <= 0 {
return src, 0, fmt.Errorf("cannot unmarshal varint")
}
i64 := int64(int64(u64>>1) ^ (int64(u64<<63) >> 63))
return src[offset:], i64, nil
} }
// UnmarshalVarInt64s unmarshals len(dst) int64 values from src to dst // UnmarshalVarInt64s unmarshals len(dst) int64 values from src to dst and returns the remaining tail from src.
// and returns the remaining tail from src.
func UnmarshalVarInt64s(dst []int64, src []byte) ([]byte, error) { func UnmarshalVarInt64s(dst []int64, src []byte) ([]byte, error) {
if len(src) < len(dst) { if len(src) < len(dst) {
return src, fmt.Errorf("too small len(src)=%d; it must be bigger or equal to len(dst)=%d", len(src), len(dst)) return src, fmt.Errorf("too small len(src)=%d; it must be bigger or equal to len(dst)=%d", len(src), len(dst))
@ -358,18 +361,16 @@ func marshalVarUint64sSlow(dst []byte, us []uint64) []byte {
return dst return dst
} }
// UnmarshalVarUint64 returns unmarshaled uint64 from src and returns // UnmarshalVarUint64 returns unmarshaled uint64 from src and returns the remaining tail from src.
// the remaining tail from src.
func UnmarshalVarUint64(src []byte) ([]byte, uint64, error) { func UnmarshalVarUint64(src []byte) ([]byte, uint64, error) {
u64, offset := binary.Uvarint(src) u64, offset := binary.Uvarint(src)
if offset <= 0 { if offset <= 0 {
return src, 0, fmt.Errorf("cannot read varuint64") return src, 0, fmt.Errorf("cannot read varuint")
} }
return src[offset:], u64, nil return src[offset:], u64, nil
} }
// UnmarshalVarUint64s unmarshals len(dst) uint64 values from src to dst // UnmarshalVarUint64s unmarshals len(dst) uint64 values from src to dst and returns the remaining tail from src.
// and returns the remaining tail from src.
func UnmarshalVarUint64s(dst []uint64, src []byte) ([]byte, error) { func UnmarshalVarUint64s(dst []uint64, src []byte) ([]byte, error) {
if len(src) < len(dst) { if len(src) < len(dst) {
return src, fmt.Errorf("too small len(src)=%d; it must be bigger or equal to len(dst)=%d", len(src), len(dst)) return src, fmt.Errorf("too small len(src)=%d; it must be bigger or equal to len(dst)=%d", len(src), len(dst))