### 10 releases (breaking)

✓ Uses Rust 2018 edition

0.8.1 | Jul 29, 2019 |
---|---|

0.7.0 | Jun 26, 2019 |

0.6.0 | Feb 4, 2019 |

0.5.1 | Jun 26, 2018 |

0.2.0 | Mar 29, 2018 |

#**16** in Compression

**1,252** downloads per month

Used in **9** crates (2 directly)

**MIT**license

83KB

1.5K
SLoC

# Fast Bitpacking algorithms

This crate is a **Rust port of Daniel Lemire's simdcomp C library**.

It makes it possible to compress/decompress :

- sequence of small integers
- sequences of increasing integers

⭐️ It is fast. Expect > 4 billions integers per seconds.

# How to compile ?

compiles on stable rust but require rust > 1.27 to compile.`bitpacking`

Just add to your

:`Cargo .toml`

`bitpacking ``=` `"`0.5`"`

For some bitpacking flavor and for some platform, the bitpacking crate may benefit from some specific simd instruction set.

In this case, it will always ship an alternative scalar implementation and will fall back to the scalar implementation at runtime.

In other words, your do not need to configure anything. Your program will run correctly, and at the fastest speed available for your CPU.

# Documentation

# What is bitpacking ?

Traditional compression schemes like LZ4 are not really suited to address this problem efficiently. Instead, there are different families of solutions to this problem.

One of the most straightforward and efficient ones is

:`bitpacking`

- Integers are first grouped into blocks of constant size (e.g.

when using the SSE2 implementation).`128` - If not available implicitely, compute the minimum number of bits

that makes it possible to represent all of the integers. In other words, the smallest`b`

such that all integers in the block are stricly smaller than 2`b`^{b}. - The bitpacked representation is then some variation of the concatenation of the integers restricted to their least significant

-bits.`b`

For instance, assuming a block of

, when encoding `4`

. Assuming that the highest value in the block is 9, `4``,` `9``,` `3``,` `2`

. All values will then be encoded over 4 bits as follows.`b = 4`

original number | binary representation |
---|---|

4 | 0100 |

9 | 1001 |

3 | 0011 |

2 | 0010 |

... | ... |

As a result, each integer of this block will only require 4 bits.

# Choosing between BitPacker1x, BitPacker4x and BitPacker8x.

⚠️

, `BitPacker1x`

, and `BitPacker4x`

produce different formats,
and are incompatible one with another.`BitPacker8x`

and `BitPacker4x`

are designed specifically to leverage `BitPacker8x`

and `SSE3`

instructions respectively.`AVX2`

It will safely fallback at runtime to a scalar implementation of these format if these instruction sets are not available on the running CPU.

👌 I recommend using

if you are in doubt.`BitPacker4x`

## BitPacker1x

is what you would expect from a bitpacker.
The integer representation over `BitPacker1x`

bits are simply concatenated one
after the other. One block must contain `b`

.`32` integers

## BitPacker4x

bits ordering works in layers of 4 integers. This gives an opportunity
to leverage `BitPacker4x`

instructions to encode and decode the stream.
One block must contain `SSE3`

.`128` integers

### BitPacker8x

bits ordering works in layers of 8 integers. This gives an opportunity
to leverage `BitPacker8x`

instructions to encode and decode the stream.
One block must contain `AVX2`

.`256` integers

# Compressing small integers

`extern` `crate` bitpacking`;`
`use` `bitpacking``::``{`BitPacker4x`,` BitPacker`}``;`
`//` Detects if `SSE3` is available on the current computed
`//` and uses the best available implementation accordingly.
`let` bitpacker `=` `BitPacker4x``::`new`(``)``;`
`//` Computes the number of bits used for each integers in the blocks.
`//` my_data is assumed to have a len of 128 for `BitPacker4x`.
`let` num_bits`:` `u8` `=` bitpacker`.``num_bits``(``&`my_data`)``;`
`//` The compressed array will take exactly `num_bits * BitPacker4x::BLOCK_LEN / 8`.
`//` But it is ok to have an output with a different len as long as it is larger
`//` than this.
`let` `mut` compressed `=` `vec!``[``0``u8``;` `4` `*` `BitPacker4x``::``BLOCK_LEN``]``;`
`//` Compress returns the len.
`let` compressed_len `=` bitpacker`.``compress``(``&`my_data`,` `&``mut` compressed`[``..``]``,` num_bits`)``;`
`assert_eq!``(``(`num_bits `as` `usize``)` `*` `BitPacker4x``::``BLOCK_LEN` `/` `8``,` compressed_len`)``;`
`//` Decompressing
`let` `mut` decompressed `=` `vec!``[``0``u32``;` `BitPacker4x``::``BLOCK_LEN``]``;`
bitpacker`.``decompress``(``&`compressed`[``..`compressed_len`]``,` `&``mut` decompressed`[``..``]``,` num_bits`)``;`
`assert_eq!``(``&`my_data`,` `&`decompressed`)``;`

# Benchmark

The following benchmarks have been run on one thread on my laptop's CPU: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz.

You can get accurate figures on your hardware by running the following command.

`cargo`` +nightly bench`` --`features unstable

## BitPacker1x

operation | throughput |
---|---|

compress | 1.4 billions int/s |

compress_delta | 1.0 billions int/s |

decompress | 1.8 billions int/s |

decompress_delta | 1.4 billions int/s |

# BitPacker4x (assuming SSE3 instructions are available)

operation | throughput |
---|---|

compress | 5.3 billions int/s |

compress_delta | 2.8 billions int/s |

decompress | 5.5 billions int/s |

decompress_delta | 5 billions int/s |

# BitPacker8x (assuming AVX2 instructions are available)

operation | throughput |
---|---|

compress | 7 billions int/s |

compress_delta | 600 millions int/s |

decompress | 6.5 billions int/s |

decompress_delta | 5.6 billions int/s |

# Reference

# Other crates you might want to check out

- stream vbyte A Stream-VByte implementation
- mayda Another crate implementation the same algorithms.