gpt4 book ai didi

algorithm - 在 Jeff 的幻灯片中查找有关 "Group varint encoding/decoding"的更多详细信息

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:54:23 25 4
gpt4 key购买 nike

我注意到 Jeff 的幻灯片“构建大规模信息检索系统的挑战”,也可以在这里下载:http://research.google.com/people/jeff/WSDM09-keynote.pdf ,提到了一种称为“group varint encoding”的整数压缩方法。据说比每字节 7 位整数编码快得多(多 2 倍)。我对此非常感兴趣,正在寻找它的实现,或者任何可以帮助我自己实现它的更多细节。

我不是专业人士,也不是新手,欢迎任何帮助!

最佳答案

这是指“可变整数编码”,其中序列化时用于存储整数的位数不固定为 4 个字节。 varint in the protocol buffer documentation 有很好的描述.

用于编码Google's protocol buffers , 您可以浏览 protocol buffer source code .

CodedOutputStream 包含确切的编码函数 WriteVarint32FallbackToArrayInline :

inline uint8* CodedOutputStream::WriteVarint32FallbackToArrayInline(
uint32 value, uint8* target) {
target[0] = static_cast<uint8>(value | 0x80);
if (value >= (1 << 7)) {
target[1] = static_cast<uint8>((value >> 7) | 0x80);
if (value >= (1 << 14)) {
target[2] = static_cast<uint8>((value >> 14) | 0x80);
if (value >= (1 << 21)) {
target[3] = static_cast<uint8>((value >> 21) | 0x80);
if (value >= (1 << 28)) {
target[4] = static_cast<uint8>(value >> 28);
return target + 5;
} else {
target[3] &= 0x7F;
return target + 4;
}
} else {
target[2] &= 0x7F;
return target + 3;
}
} else {
target[1] &= 0x7F;
return target + 2;
}
} else {
target[0] &= 0x7F;
return target + 1;
}
}

级联 if 只会在 target 数组的末尾添加额外的字节,前提是 value 的大小保证了这些额外的字节。 0x80 屏蔽正在写入的字节,value 向下移动。据我所知,0x7f 掩码使它表示“编码的最后一个字节”。 (当 OR'ing 0x80 时,最高位将始终为 1,然后最后一个字节清除最高位(通过 AND'ing 0x7f )。因此,在读取 varint 时,您会一直读取到最高位为零的字节。

我刚刚意识到您专门询问了“Group VarInt encoding”。抱歉,该代码是关于基本 VarInt 编码的(仍然比 7 位快)。基本思路看起来很相似。不幸的是,它不是用于在 Protocol Buffer 中存储 64 位数字的东西。不过,如果该代码在某处开源,我也不会感到惊讶。

使用 varint 中的想法和幻灯片中的“Group varint”图表,自己制作应该不会太难:)

这是描述 Group VarInt compression 的另一页,其中包含解码代码。不幸的是,他们提到了公开可用的实现,但没有提供引用。

void DecodeGroupVarInt(const byte* compressed, int size, uint32_t* uncompressed) {
const uint32_t MASK[4] = { 0xFF, 0xFFFF, 0xFFFFFF, 0xFFFFFFFF };
const byte* limit = compressed + size;
uint32_t current_value = 0;
while (compressed != limit) {
const uint32_t selector = *compressed++;
const uint32_t selector1 = (selector & 3);
current_value += *((uint32_t*)(compressed)) & MASK[selector1];
*uncompressed++ = current_value;
compressed += selector1 + 1;
const uint32_t selector2 = ((selector >> 2) & 3);
current_value += *((uint32_t*)(compressed)) & MASK[selector2];
*uncompressed++ = current_value;
compressed += selector2 + 1;
const uint32_t selector3 = ((selector >> 4) & 3);
current_value += *((uint32_t*)(compressed)) & MASK[selector3];
*uncompressed++ = current_value;
compressed += selector3 + 1;
const uint32_t selector4 = (selector >> 6);
current_value += *((uint32_t*)(compressed)) & MASK[selector4];
*uncompressed++ = current_value;
compressed += selector4 + 1;
}
}

关于algorithm - 在 Jeff 的幻灯片中查找有关 "Group varint encoding/decoding"的更多详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2982957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com