A variable-length quantity (VLQ) is a universal code that uses an arbitrary number of binary octets (eight-bit bytes) to represent an arbitrarily large integer. A VLQ is essentially a base-128 representation of an unsigned integer with the addition of the eighth bit to mark continuation of bytes. VLQ is identical to LEB128 except in endianness. See the example below.
Applications and history
Base-128 compression is known by many names – VB (Variable Byte), VByte, Varint, VInt, EncInt etc.
A variable-length quantity (VLQ) was defined for use in the standard MIDI file format to save additional space for a resource-constrained system, and is also used in the later Extensible Music Format (XMF).
Base-128 is also used in ASN.1 BER encoding to encode tag numbers and object identifiers. It is also used in the WAP environment, where it is called variable length unsigned integer or uintvar. The DWARF debugging format defines a variant called LEB128 (or ULEB128 for unsigned numbers), where the least significant group of 7 bits is encoded in the first byte, and the most significant bits are in the last byte (so effectively it is the little-endian analog of a VLQ). Google Protocol Buffers use a similar format to have compact representation of integer values, as does Oracle Portable Object Format (POF) and the Microsoft .NET Framework "7-bit encoded int" in the BinaryReader and BinaryWriter classes.
It is also used extensively in web browsers for source mapping – which contain a lot of integer line and column number mappings – to keep the size of the map to a minimum.
Variable-width integers in LLVM use a similar principle. The encoding chunks are little-endian and need not be 8 bits in size. The LLVM documentation describes a field that uses 4-bit chunk, with each chunk consisting of 1 bit continuation and 3 bits payload.
The encoding assumes an octet (an 8-bit byte) where the most significant bit (MSB), also commonly known as the sign bit, is reserved to indicate whether another VLQ octet follows.
If A is 0, then this is the last VLQ octet of the integer. If A is 1, then another VLQ octet follows.
The general VLQ encoding is simple, but in basic form is only defined for unsigned integers (nonnegative, positive or zero), and is somewhat redundant, since prepending 0x80 octets corresponds to zero padding. There are various signed number representations to handle negative numbers, and techniques to remove the redundancy.
Group Varint Encoding
Google developed Group Varint Encoding (GVE) after observing that traditional VLQ encoding incurs many CPU branches during decompression. GVE uses a single byte as a header for 4 variable-length uint32 values. The header byte has 4 2-bit numbers representing the storage length of each of the following 4 uint32s. Such a layout eliminates the need to check and remove VLQ continuation bits. Data bytes can be copied directly to their destination. This layout reduces CPU branches, making GVE faster than VLQ on modern pipelined CPUs.
PrefixVarint is a similar design but with a uint64 maximum. It is said to have "been invented multiple times independently". It is possible to be changed into a chained version with infinitely many continuations.
Negative numbers can be handled using a sign bit, which only needs to be present in the first octet.
In the data format for Unreal Packages used by the Unreal Engine, a variable-length quantity scheme called Compact Indices is used. The only difference in this encoding is that the first VLQ has the sixth bit reserved to indicate whether the encoded integer is positive or negative. Any consecutive VLQ octet follows the general structure.
|First VLQ octet||Other VLQ octets|
|A||B||C0||B||Cn (n > 0)|
If A is 0, then the VLQ represents a positive integer. If A is 1, then the VLQ represents a negative number.
If B is 0, then this is the last VLQ octet of the integer. If B is 1, then another VLQ octet follows.
An alternative way to encode negative numbers is to use the least significant bit for sign. This is notably done for Google Protocol Buffers, and is known as a zigzag encoding for signed integers. One can encode the numbers so that encoded 0 corresponds to 0, 1 to −1, 10 to 1, 11 to −2, 100 to 2, etc.: counting up alternates between nonnegative (starting at 0) and negative (since each step changes the least-significant bit, hence the sign), whence the name "zigzag encoding". Concretely, transform the integer as
(n << 1) ^ (n >> k - 1) for fixed k-bit integers.
LEB128 uses two's complement to represent signed numbers. In this scheme of representation, n bits encode a range from −2n to 2n − 1, and all negative numbers start with a 1 in the most significant bit. In Signed LEB128, the input is sign-extended so that its length is a multiple of 7 bits. From there the encoding proceeds as usual.
In LEB128, the stream is arranged least significant first.
With the VLQ encoding described above, any number that can be encoded with N octets can also be encoded with more than N octets simply by prepending additional 0x80 octets as zero-padding. For example, the decimal number 358 can be encoded as the 2-octet VLQ 0x8266, or the number 0358 can be encoded as 3-octet VLQ 0x808266, or 00358 as the 4-octet VLQ 0x80808266 and so forth.
However, the VLQ format used in Git removes this prepending redundancy and extends the representable range of shorter VLQs by adding an offset to VLQs of 2 or more octets in such a way that the lowest possible value for such an (N + 1)-octet VLQ becomes exactly one more than the maximum possible value for an N-octet VLQ. In particular, since a 1-octet VLQ can store a maximum value of 127, the minimum 2-octet VLQ (0x8000) is assigned the value 128 instead of 0. Conversely, the maximum value of such a 2-octet VLQ (0xFF7F) is 16511 instead of just 16383. Similarly, the minimum 3-octet VLQ (0x808000) has a value of 16512 instead of zero, which means that the maximum 3-octet VLQ (0xFFFF7F) is 2113663 instead of just 2097151.
In this way, there is one and only one encoding of each integer, making this a base-128 bijective numeration.
Here is a worked-out example for the decimal number 137:
- Represent the value in binary notation (e.g. 137 as 10001001)
- Break it up in groups of 7 bits starting from the lowest significant bit (e.g. 137 as 0000001 0001001). This is equivalent to representing the number in base 128.
- Take the lowest 7 bits, and that gives you the least significant byte (0000 1001). This byte comes last.
- For all the other groups of 7 bits (in the example, this is 000 0001), set the MSB to 1 (which gives 1000 0001 in our example). Thus 137 becomes 1000 0001 0000 1001, where the bits in boldface are something we added. These added bits denote whether there is another byte to follow or not. Thus, by definition, the very last byte of a variable-length integer will have 0 as its MSB.
Another way to look at this is to represent the value in base-128 and then set the MSB of all but the last base-128 digit to 1.
|Integer (decimal)||Integer (hexadecimal)||Integer (binary)||Variable-length quantity (hexadecimal)||Variable-length quantity (binary)|
00000000 00000000 00000000 00000000
00000000 00000000 00000000 01111111
00000000 00000000 00000000 10000000
00000000 00000000 00100000 00000000
00000000 00000000 00111111 11111111
00000000 00000000 01000000 00000000
|0x81 0x80 0x00|
10000001 10000000 00000000
00000000 00011111 11111111 11111111
|0xFF 0xFF 0x7F|
11111111 11111111 01111111
00000000 00100000 00000000 00000000
|0x81 0x80 0x80 0x00|
10000001 10000000 10000000 00000000
00001000 00000000 00000000 00000000
|0xC0 0x80 0x80 0x00|
11000000 10000000 10000000 00000000
00001111 11111111 11111111 11111111
|0xFF 0xFF 0xFF 0x7F|
11111111 11111111 11111111 01111111
- Jianguo Wang; Chunbin Lin; Yannis Papakonstantinou; Steven Swanson. "An Experimental Study of Bitmap Compression vs. Inverted List Compression". 2017. doi:10.1145/3035918.3064007.
- MIDI File Format: Variable Quantities.
- "ITU-T Recommendation X.690" (PDF). International Telecommunication Union. 2002.
- DWARF Standard.
- Google Protocol Buffers.
- Oracle Portable Object Format (POF).
- System.IO.BinaryWriter.Write7BitEncodedInt(int) method and System.IO.BinaryReader.Read7BitEncodedInt() method.
- "LLVM Bitcode File Format", section "Variable Width Integers". Accessed 2019-10-01.
- Jeff Dean. "Challenges in Building Large-Scale Information Retrieval Systems" (PDF). p. 58. Retrieved 2020-05-30.
- Olesen, Jakob Stoklund (31 May 2020). "stoklund/varint".[dead link]
- "Unreal Packages". 1999-07-21. Archived from the original on 2010-08-20. Retrieved 2021-08-29.
- Protocol Buffers: Encoding: Signed Integers.
- Free Standards Group (December 2005). "DWARF Debugging Information Format Specification Version 3.0" (PDF). p. 70. Retrieved 2009-07-19.
- "Git – fast, scalable, distributed revision control system". 28 October 2021.
- Standard MIDI-File Format Spec. 1.1.