I noticed that in Jeff’s slides “Challenges in Building Large-Scale Information Retrieval Systems”, which can also be downloaded here: http://research.google.com/people/jeff/WSDM09-keynote.pdf, a method of integers compression called “group varint encoding” was mentioned. It was said much faster than 7 bits per byte integer encoding (2X more). I am very interested in this and looking for an implementation of this, or any more details that could help me implement this by myself.
I am not a pro and new to this, and any help is welcome!
That’s referring to “variable integer encoding”, where the number of bits used to store an integer when serialized is not fixed at 4 bytes. There is a good description of varint in the protocol buffer documentation.
It is used in encoding Google’s protocol buffers, and you can browse the protocol buffer source code.
The
CodedOutputStreamcontains the exact encoding function WriteVarint32FallbackToArrayInline:The cascading
ifs will only add additional bytes onto the end of thetargetarray if the magnitude ofvaluewarrants those extra bytes. The0x80masks the byte being written, and thevalueis shifted down. From what I can tell, the0x7fmask causes it to signify the “last byte of encoding”. (When OR’ing0x80, the highest bit will always be1, then the last byte clears the highest bit (by AND’ing0x7f). So, when reading varints you read until you get a byte with a zero in the highest bit.I just realized you asked about “Group VarInt encoding” specifically. Sorry, that code was about basic VarInt encoding (still faster than 7-bit). The basic idea looks to be similar. Unfortunately, it’s not what’s being used to store 64bit numbers in protocol buffers. I wouldn’t be surprised if that code was open sourced somewhere though.
Using the ideas from
varintand the diagrams of “Group varint” from the slides, it shouldn’t be too too hard to cook up your own 🙂Here is another page describing Group VarInt compression, which contains decoding code. Unfortunately they allude to publicly available implementations, but they don’t provide references.