Varlen Encoding

From Lazarus wiki
Revision as of 02:29, 23 March 2019 by CuriousKit (talk | contribs) (Suitability)
Jump to navigationJump to search

Varlen Encoding (short for "variable length") is a way of storing a QWord so that small values take as few bytes as possible.


The inspiration and design of Varlen encoding came from two sources:

Some data fields are statistically more likely to contain small values, such as those used for sequential indices and string lengths, but whose upper limit may be the size of a LongWord or a QWord. If many hundreds of these values are stored, space can be quickly wasted when most of the bytes that make up the full integer value are zero, and this can be problematic on media with limited capacity. The design of Varlen Encoding is an attempt to reduce this wastage with minimal performance impact.


A Varlen-encoded integer (henceforth just called a Varlen) takes between 1 and 9 bytes to store. The first byte, known as a Lead Byte, encodes a byte count and the most significant bits of the number, stored in big-endian order. The bit pattern of the Lead Byte dictates the number of Data Bytes that follow:

Lead Byte Value Range Description
No data bytes; #'s encode a 7-bit value between $00 to $7F (0 to 127)
1 data byte follows; #'s encode a 14-bit value offset by $80 (add $80 to the value to obtain the actual integer)
2 data bytes follow; #'s encode a 21-bit value offset by $4080
3 data bytes follow; #'s encode a 28-bit value offset by $204080
$00 10204080..$08 1020407F
4 data bytes follow; #'s encode a 35-bit value offset by $10204080
$0008 10204080..$0408 1020407F
5 data bytes follow; #'s encode a 42-bit value offset by $08 10204080
$000408 10204080..$020408 1020407F
6 data bytes follow; #'s encode a 49-bit value offset by $0408 10204080
$00020408 10204080..$01020408 1020407F
7 data bytes follow; #'s encode a 56-bit value offset by $020408 10204080
$01020408 10204080..$FFFFFFFF FFFFFFFF
8 data bytes follow; #'s encode a 64-bit value offset by $01020408 10204080

It is permissible to remove the offsets without sacrificing the full QWord domain. The use of offsets remove multiple encodings for the same value (e.g. 00000000 and 10000000 00000000 for zero) and slightly increase the chance that an integer is stored in fewer bytes (e.g. 16,384, or $4000, would otherwise take 3 bytes to store without the offset (as 11000000 00000000 00000000), whereas with the offset, it only takes 2 bytes (as 10111111 10000000)).


As stated earlier, Varlen Encoding is best suited for situations where the field is more likely to contain a small value over a large one. As a result, not all LongWord and QWord values should be stored as Varlens because an extra byte is required to store the largest of values. For example, the following field types are good candidates for Varlen Encoding:

  • Index values
  • String and buffer lengths
  • Counts

Conversely, the following would make for poor candidates:

  • 32-bit pointers
  • Hashes
  • Signed integers (since negative numbers map onto the largest positive integers)