VLQ的介绍

本文介绍了VLQ(Variable-length Quantity)编码,这是一种用于高效存储整数的变长字节数组编码方式。该技术被应用于protobuf中,提升序列化效率。文中提供了解码和编码VLQ的Go语言实现代码,供读者深入理解其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文来介绍一下VLQ(variable-length quantity)

Wiki:Variable-length quantity

用变长字节数组来对整形进行编码,极大提高存储效率。

其实,这样的优点也被应用到了protobuf中,所以才有较高的序列化效率。

Varints in protobuf

具体实现代码如下:

package vlq

import (
	"math"
	"math/bits"
)

// DecodeVarint takes a variable length VLQ based integer and
// decodes it into a 32 bit integer.
func DecodeVarint(input []byte) (uint32, error) {
	const lastBitSet = 0x80 // 1000 0000

	var d uint32
	var bitPos int

	for i := len(input) - 1; i >= 0; i-- {
		n := uint8(input[i])

		// Process the first 7 bits and ignore the 8th.
		for checkBit := 0; checkBit < 7; checkBit++ {

			// Rotate the last bit off and move it to the back.
			// Before: 0000 0001
			// After:  1000 0000
			n = bits.RotateLeft8(n, -1)

			// Calculate based on only those 1 bits that were rotated.
			// Convert the bitPos to base 10.
			if n >= lastBitSet {
				switch {
				case bitPos == 0:
					d++
				default:
					base10 := math.Pow(2, float64(bitPos))
					d += uint32(base10)
				}
			}

			// Move the bit position.
			bitPos++
		}
	}

	return d, nil
}

// EncodeVarint takes a 32 bit integer and encodes it into
// a variable length VLQ based integer.
func EncodeVarint(n uint32) []byte {
	const maxBytes = 4
	const eightBitSet = 0x80      // 1000 0000
	const lastBitSet = 0x80000000 // 1000 0000 0000 0000

	encoded := make([]byte, maxBytes)

	for bytePos := maxBytes - 1; bytePos >= 0; bytePos-- {
		var d uint8

		// Process the next 7 bits.
		for checkBit := 0; checkBit < 7; checkBit++ {

			// Rotate the last bit off and move it to the back.
			// Before: 0000 0000 0000 0001
			// After:  1000 0000 0000 0000
			n = bits.RotateLeft32(n, -1)

			// Calculate based on only those 1 bits that were
			// rotated. Convert the bit position to base 10.
			if n >= lastBitSet {
				switch {
				case checkBit == 0:
					d++
				default:
					base10 := math.Pow(2, float64(checkBit))
					d += uint8(base10)
				}
			}
		}

		// These values need the 8th bit to be set as 1.
		if bytePos < 3 {
			d += eightBitSet
		}

		// Store the value in reserve order.
		encoded[bytePos] = d
	}

	// Remove leading zero values by finding values that only
	// have their eight bit set.
	for bytePos, b := range encoded {
		if b == eightBitSet {
			continue
		}
		encoded = encoded[bytePos:]
		break
	}

	return encoded
}

在此抛砖引玉,有兴趣的同学,可以自行研究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JYCJ_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值