A variable-length integer is a variable-length encoded representation of an integer value using a sequence of bytes. The number of bytes that is needed depends on the value being represented, with larger magnitudes using more bytes than smaller ones. A variable-length integer has no inherent bound on the range of values that can be expressed, unless such a bound is imposed in a particular application. Such schemes have been in use in applications at least since the 1983 specification of the MIDI file format. Variations exist for representing signed and unsigned integers. Variable-length integers are used in applications where it is important to be able to support a large range of integer values and in which smaller numbers are more common than larger ones. Several variations of such formats exist. Early versions used in the MIDI file format and Abstract Syntax Notation One (ASN.1) used big-endian representation. A later-developed little-endian variation is called LEB128. Most variable-length integer representation methods have some built-in redundancy, such that values can be represented using more bytes than necessary, unless the longer representations are prohibited in a particular application (e.g., by prohibiting certain leading or trailing byte values).
Applications and history Various names exist for formats encoding variable-length integers, such as Varint, VInt, VB (Variable Byte), VByte, EncInt, etc. A variable-length quantity (VLQ) was defined for use in the standard MIDI file format, version 1.0 of which was published in 1983. It is also used in the later Extensible Music Format (XMF), first published in 2001. Base-128 is also used in the basic encoding rules of Abstract Syntax Notation One (ASN.1) to encode tag numbers and object identifiers (first published in 1984). It is also used in the (now-obsolete) WAP environment introduced in 1999, where it is called variable length unsigned integer or uintvar. RFC 6256, issued in May 2011, defines the same format and refers to it as a self-delimiting numeric value (SDNV) for use in delay-tolerant networking protocols. 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 it is effectively 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.
Benefits The primary benefits of variable-length integer encoding are its compactness and its flexibility to support any range of values. In most applications, smaller integers are more commonly encountered than very large ones. With variable-length integer encoding, small numbers use fewer bytes, which typically results in using fewer bytes on average if a large quantity of integer values are encoded this way. At the same time, variable-length integer encoding can also represent arbitrarily large integers, unlike fixed-wordlength representations such as 8-bit bytes, 16-bit words, 32-bit integers, or 64-bit long integers. There is no inherent upper bound on the range of values that can be represented by a variable-length integer encoding. In contrast, fixed-length integer representations would typically have either an inadequate range of representable values if a short wordlength is used, or can use too much data to represent each integer if a longer wordlength is used. Variable-length integer encoding is also simpler to encode and decode than most other variable-length codes such as Huffman codes, since VLQ codes use whole-byte chunks of data. More general variable-length codes typically require much more bit-oriented manipulation for encoding and decoding.
General structure 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. B is a 7-bit number [0x00, 0x7F], and n is the position of the VLQ octet where B0 is the least significant. The VLQ octets are arranged most significant first in a stream.
Variants 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.
Signed numbers
Sign bit 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 octet has the seventh bit reserved to indicate whether the encoded integer is positive or negative. Any consecutive VLQ octet follows the general structure.
… excerpt ends here. Continue reading the full article.
