• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D07-Apr-2015266

LICENSEH A D07-Apr-20151.1 KiB

README.mdH A D07-Apr-20152.9 KiB

govarint.goH A D07-Apr-20155.7 KiB

govarint_test.goH A D07-Apr-20155 KiB

README.md

1# Govarint
2
3This project aims to provide a simple API for the performant encoding and decoding of 32 and 64 bit integers using a variety of algorithms.
4
5[![](http://i.imgur.com/mpgC23U.jpg)](https://www.flickr.com/photos/tsevis/8648521649/)
6
7## Usage
8
9Each integer encoding algorithm conforms to an encoding and decoding interface.
10The interfaces also specify the size of the unsigned integer, either 32 or 64 bits, and will be referred to as XX below.
11To create an encoder:
12
13    NewU32Base128Encoder(w io.Writer)
14    NewU64Base128Encoder(w io.Writer)
15    NewU32GroupVarintEncoder(w io.Writer)
16
17For encoders, the only two commands are `PutUXX` and `Close`.
18`Close` must be called as some integer encoding algorithms write in multiples.
19
20    var buf bytes.Buffer
21    enc := NewU32Base128Encoder(&buf)
22    enc.PutU32(117)
23    enc.PutU32(343)
24    enc.Close()
25
26To create a decoder:
27
28    NewU32Base128Decoder(r io.ByteReader)
29    NewU64Base128Decoder(r io.ByteReader)
30    NewU32GroupVarintDecoder(r io.ByteReader)
31
32For decoders, the only command is `GetUXX`.
33`GetUXX` returns the value and any potential errors.
34When reading is complete, `GetUXX` will return an `EOF` (End Of File).
35
36    dec := NewU32Base128Decoder(&buf)
37    x, err := dec.GetU32()
38
39## Use Cases
40
41Using fixed width integers, such as uint32 and uint64, usually waste large amounts of space, especially when encoding small values.
42Optimally, smaller numbers should take less space to represent.
43
44Using integer encoding algorithms is especially common in specific applications, such as storing edge lists or indexes for search engines.
45In these situations, you have a sorted list of numbers that you want to keep as compactly as possible in memory.
46Additionally, by storing only the difference between the given number and the previous (delta encoding), the numbers are quite small, and thus compress well.
47
48For an explicit example, the Web Data Commons Hyperlink Graph contains 128 billion edges linking page A to page B, where each page is represented by a 32 bit integer.
49By converting all these edges to 64 bit integers (32 | 32), sorting them, and then using delta encoding, memory usage can be reduced from 64 bits per edge down to only 9 bits per edge using the Base128 integer encoding algorithm.
50This figure improves even further if compressed using conventional compression algorithms (3 bits per edge).
51
52## Encodings supported
53
54`govarint` supports:
55
56+ Base128 [32, 64] - each byte uses 7 bits for encoding the integer and 1 bit for indicating if the integer requires another byte
57+ Group Varint [32] - integers are encoded in blocks of four - one byte encodes the size of the following four integers, then the values of the four integers follows
58
59Group Varint consistently beats Base128 in decompression speed but Base128 may offer improved compression ratios depending on the distribution of the supplied integers.
60
61## Tests
62
63    go test -v -bench=.
64
65## License
66
67MIT License, as per `LICENSE`
68