VLQ的介绍

news/2024/10/23 5:44:50/

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

Wiki:Variable-length quantity

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

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

Varints in protobuf

具体实现代码如下:

package vlqimport ("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 0000var d uint32var bitPos intfor 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 0000n = 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 = 4const eightBitSet = 0x80      // 1000 0000const lastBitSet = 0x80000000 // 1000 0000 0000 0000encoded := 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 0000n = 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
}

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


http://www.ppmy.cn/news/470566.html

相关文章

lq到底是什么意思_lq是什么意思

2018-03-27 lq是什么意思 【商业】   导商(Leading Quotient——LQ)。导商即为领导商&#xff0c;是指一个人领导、指导、引导、带领他人或团队组织的智慧和能力的商数。导商既取决于领导理论与方式&#xff0c;又取决于领导环境与气氛&#xff1b;既取决于领导特质与人格&am…

数据结构队列基本实现

队列是一种操作受限的线性表。在这种线性表上&#xff0c;插入操作限定在表的某一端进行&#xff0c;删除限定在表的另一端进行。允许插入的一端为队尾&#xff0c;允许删除的一端为对头。队列按照存储方式分为两种&#xff0c;分别是顺序存储和链式存储。其中顺序存储方式的队…

二、LINQ

文章目录 一、LINQ概述与查询语法二、LINQ方法语法基础三、LINQ聚合操作与元素操作四、数据类型转换 LINQ&#xff08;Language Integrated Query&#xff0c;语言集成查询&#xff09;&#xff0c;可为C#语法提供强大的查询功能。 一、LINQ概述与查询语法 LINQ提供了一种跨数…

L2-2 列车调度

火车站的列车调度铁轨的结构如下图所示。 两端分别是一条入口&#xff08;Entrance&#xff09;轨道和一条出口&#xff08;Exit&#xff09;轨道&#xff0c;它们之间有N条平行的轨道。每趟列车从入口可以选择任意一条轨道进入&#xff0c;最后从出口离开。在图中有9趟列车&am…

RLx2~~

大模型时代&#xff0c;模型压缩和加速显得尤为重要。传统监督学习可通过稀疏神经网络实现模型压缩和加速&#xff0c;那么同样需要大量计算开销的强化学习任务可以基于稀疏网络进行训练吗&#xff1f;本文提出了一种强化学习专用稀疏训练框架&#xff0c;可以节省至多 95% 的训…

DQL2

/* DQL标准语法结构:编写DQL一定要严格按照此语法的顺序来实现&#xff01; SELECT [ALL | DISTINCT] ALL表示查询出所有的内容 DISTINCT 去重 {* | 表名.* | 表名.字段名[ AS 别名][,…]} 指定查询出的字段的 FROM 表名[AS 别名][,表1… AS 别名] [INNER | [LEFT | RIGHT] [OU…

Hql(01)

一&#xff1a;什么是Hql HQL是Hibernate Query Language的缩写&#xff0c;提供更加丰富灵活、更为强大的查询能力&#xff1b;HQL更接近SQL语句查询语法。 二&#xff1a;hql和sql区别/异同&#xff08;面试题&#xff09; HQL S…

HQL(二)

一、写BaseDao 需求&#xff1a; 按名字分页查询对应书籍信息 我们用hql是想我们的需求&#xff0c;是这样的&#xff1a; public List<Book> list1(Book book,PageBean pageBean){Session session SessionFactoryUtils.getSession();Transaction transaction sess…